Replace strcat with strlcat

This commit is contained in:
o9000 2017-12-21 11:42:07 +01:00
parent c96201930b
commit 67e25b8102
11 changed files with 112 additions and 22 deletions

View file

@ -157,6 +157,7 @@ set( SOURCES src/config.c
src/util/timer.c
src/util/cache.c
src/util/color.c
src/util/strlcat.c
src/util/print.c
src/util/gradient.c
src/util/test.c

View file

@ -25,6 +25,7 @@ set(SOURCES ../util/common.c
../util/timer.c
../config.c
../util/server.c
../util/strlcat.c
../launcher/apps-common.c
../launcher/icon-theme-common.c
md4.c

View file

@ -20,6 +20,7 @@
#include "gui.h"
#include "background_gui.h"
#include "gradient_gui.h"
#include "strlcat.h"
GtkWidget *panel_width, *panel_height, *panel_margin_x, *panel_margin_y, *panel_padding_x, *panel_padding_y,
*panel_spacing;
@ -1256,7 +1257,8 @@ gboolean panel_contains(const char *value)
char *get_panel_items()
{
char *result = calloc(1, 256 * sizeof(char));
size_t buf_size = 256;
char *result = calloc(buf_size, 1);
GtkTreeModel *model = GTK_TREE_MODEL(panel_items);
GtkTreeIter i;
@ -1267,7 +1269,7 @@ char *get_panel_items()
while (1) {
gchar *v;
gtk_tree_model_get(model, &i, itemsColValue, &v, -1);
strcat(result, v);
strlcat(result, v, buf_size);
if (!gtk_tree_model_iter_next(model, &i)) {
break;

View file

@ -69,23 +69,23 @@ void config_read_file(const char *path)
if (!config_has_panel_items) {
char panel_items[256];
panel_items[0] = 0;
strcat(panel_items, "T");
strlcat(panel_items, "T", sizeof(panel_items));
if (config_has_battery) {
if (config_battery_enabled)
strcat(panel_items, "B");
strlcat(panel_items, "B", sizeof(panel_items));
} else {
if (no_items_battery_enabled)
strcat(panel_items, "B");
strlcat(panel_items, "B", sizeof(panel_items));
}
if (config_has_systray) {
if (config_systray_enabled)
strcat(panel_items, "S");
strlcat(panel_items, "S", sizeof(panel_items));
} else {
if (no_items_systray_enabled)
strcat(panel_items, "S");
strlcat(panel_items, "S", sizeof(panel_items));
}
if (no_items_clock_enabled)
strcat(panel_items, "C");
strlcat(panel_items, "C", sizeof(panel_items));
set_panel_items(panel_items);
}
}
@ -242,13 +242,13 @@ void config_write_backgrounds(FILE *fp)
char sides[10];
sides[0] = '\0';
if (sideTop)
strcat(sides, "T");
strlcat(sides, "T", sizeof(sides));
if (sideBottom)
strcat(sides, "B");
strlcat(sides, "B", sizeof(sides));
if (sideLeft)
strcat(sides, "L");
strlcat(sides, "L", sizeof(sides));
if (sideRight)
strcat(sides, "R");
strlcat(sides, "R", sizeof(sides));
fprintf(fp, "border_sides = %s\n", sides);
fprintf(fp, "border_content_tint_weight = %d\n", (int)(border_weight));

View file

@ -207,7 +207,7 @@ gboolean update_snapshot(gpointer ignored)
char hash[MD4_HEX_SIZE + 4];
md4hexf(path, hash);
strcat(hash, ".png");
strlcat(hash, ".png", sizeof(hash));
gchar *snap = g_build_filename(g_get_user_cache_dir(), "tint2", hash, NULL);
pixbuf = force_refresh ? NULL : gdk_pixbuf_new_from_file(snap, NULL);

View file

@ -461,9 +461,10 @@ char *expand_tilde(const char *s)
{
const gchar *home = g_get_home_dir();
if (home && (strcmp(s, "~") == 0 || strstr(s, "~/") == s)) {
char *result = calloc(strlen(home) + strlen(s), 1);
strcat(result, home);
strcat(result, s + 1);
size_t buf_size = strlen(home) + strlen(s);
char *result = calloc(buf_size, 1);
strlcat(result, home, buf_size);
strlcat(result, s + 1, buf_size);
return result;
} else {
return strdup(s);
@ -476,14 +477,16 @@ char *contract_tilde(const char *s)
if (!home)
return strdup(s);
char *home_slash = calloc(strlen(home) + 2, 1);
strcat(home_slash, home);
strcat(home_slash, "/");
size_t buf_size = strlen(home) + 2;
char *home_slash = calloc(buf_size, 1);
strlcat(home_slash, home, buf_size);
strlcat(home_slash, "/", buf_size);
if ((strcmp(s, home) == 0 || strstr(s, home_slash) == s)) {
char *result = calloc(strlen(s) - strlen(home) + 2, 1);
strcat(result, "~");
strcat(result, s + strlen(home));
size_t buf_size = strlen(s) - strlen(home) + 2;
char *result = calloc(buf_size, 1);
strlcat(result, "~", buf_size);
strlcat(result, s + strlen(home), buf_size);
free(home_slash);
return result;
} else {

View file

@ -13,6 +13,7 @@
#include <pango/pangocairo.h>
#include "area.h"
#include "colors.h"
#include "strlcat.h"
#define MAX3(a, b, c) MAX(MAX(a, b), c)
#define MIN3(a, b, c) MIN(MIN(a, b), c)

59
src/util/strlcat.c Normal file
View file

@ -0,0 +1,59 @@
/* $NetBSD: strlcat.c,v 1.4 2005/05/16 06:55:48 lukem Exp $ */
/* from NetBSD: strlcat.c,v 1.16 2003/10/27 00:12:42 lukem Exp */
/* from OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stddef.h>
#include <stdint.h>
#include "strlcat.h"
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
size_t
strlcat(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}

16
src/util/strlcat.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef STRLCAT_H
#define STRLCAT_H
#include <stddef.h>
#include <stdint.h>
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
size_t strlcat(char *dst, const char *src, size_t siz);
#endif

View file

@ -58,6 +58,8 @@ src/util/blur.c
src/util/blur.h
src/util/common.c
src/util/common.h
src/util/strlcat.c
src/util/strlcat.h
src/util/timer.c
src/util/timer.h
src/util/window.c

View file

@ -22,6 +22,11 @@
/usr/include/librsvg-2.0
/usr/include/gdk-pixbuf-2.0
/usr/include/startup-notification-1.0
/usr/include/gtk-2.0
/usr/lib/x86_64-linux-gnu/gtk-2.0/include
/usr/include/atk-1.0
/usr/include/gio-unix-2.0
/usr/include/harfbuzz
/usr/include
po
src/tint2conf/po