From 67e25b8102c4c0beabb5ec20fcfad5303b5519b2 Mon Sep 17 00:00:00 2001 From: o9000 Date: Thu, 21 Dec 2017 11:42:07 +0100 Subject: [PATCH] Replace strcat with strlcat --- CMakeLists.txt | 1 + src/tint2conf/CMakeLists.txt | 1 + src/tint2conf/properties.c | 6 ++-- src/tint2conf/properties_rw.c | 20 ++++++------ src/tint2conf/theme_view.c | 2 +- src/util/common.c | 21 +++++++------ src/util/common.h | 1 + src/util/strlcat.c | 59 +++++++++++++++++++++++++++++++++++ src/util/strlcat.h | 16 ++++++++++ tint2.files | 2 ++ tint2.includes | 5 +++ 11 files changed, 112 insertions(+), 22 deletions(-) create mode 100644 src/util/strlcat.c create mode 100644 src/util/strlcat.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d660c1..6682de1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/tint2conf/CMakeLists.txt b/src/tint2conf/CMakeLists.txt index 27c9a02..eefb746 100644 --- a/src/tint2conf/CMakeLists.txt +++ b/src/tint2conf/CMakeLists.txt @@ -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 diff --git a/src/tint2conf/properties.c b/src/tint2conf/properties.c index 33a67b7..d0c19e9 100644 --- a/src/tint2conf/properties.c +++ b/src/tint2conf/properties.c @@ -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; diff --git a/src/tint2conf/properties_rw.c b/src/tint2conf/properties_rw.c index 7c0b95d..62dc719 100644 --- a/src/tint2conf/properties_rw.c +++ b/src/tint2conf/properties_rw.c @@ -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)); diff --git a/src/tint2conf/theme_view.c b/src/tint2conf/theme_view.c index edad023..6c6a369 100644 --- a/src/tint2conf/theme_view.c +++ b/src/tint2conf/theme_view.c @@ -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); diff --git a/src/util/common.c b/src/util/common.c index 614f0d7..ff9c845 100644 --- a/src/util/common.c +++ b/src/util/common.c @@ -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 { diff --git a/src/util/common.h b/src/util/common.h index c29cfb5..a1f3d9d 100644 --- a/src/util/common.h +++ b/src/util/common.h @@ -13,6 +13,7 @@ #include #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) diff --git a/src/util/strlcat.c b/src/util/strlcat.c new file mode 100644 index 0000000..25ee7b3 --- /dev/null +++ b/src/util/strlcat.c @@ -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 + * + * 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 +#include + +#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 */ +} diff --git a/src/util/strlcat.h b/src/util/strlcat.h new file mode 100644 index 0000000..482e7f2 --- /dev/null +++ b/src/util/strlcat.h @@ -0,0 +1,16 @@ +#ifndef STRLCAT_H +#define STRLCAT_H + +#include +#include + +/* + * 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 diff --git a/tint2.files b/tint2.files index 8ed4c36..8d61497 100644 --- a/tint2.files +++ b/tint2.files @@ -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 diff --git a/tint2.includes b/tint2.includes index e81af68..5045aee 100644 --- a/tint2.includes +++ b/tint2.includes @@ -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