From: Timo Kokkonen Date: Sun, 11 Oct 2020 11:08:09 +0000 (+0300) Subject: Replace strncat with _strlcat X-Git-Url: http://git.itanic.dy.fi/?p=rrdd;a=commitdiff_plain;h=79f70e219dd77051935892bd45e223d5ebb0d632 Replace strncat with _strlcat Using strncat is hard. The function expects you to know how many bytes you can write in the buffer instead if figuring out itself. Introduce a _strlcat call which correctly handles the buffer length handling and guarantees the data is null terminated after the operation. Even though the code here was handling the buffer length correctly, the code is much simpler if _strlcat is used instead. Signed-off-by: Timo Kokkonen --- diff --git a/plugin_manager.c b/plugin_manager.c index e916980..e13e01f 100644 --- a/plugin_manager.c +++ b/plugin_manager.c @@ -7,6 +7,7 @@ #include "plugin.h" #include "debug.h" #include "version.h" +#include "utils.h" static char *exec_path; @@ -81,15 +82,15 @@ int load_parser_plugin(const char *name) strncpy(str, name, sizeof(str)); str[sizeof(str) - 1] = '\0'; - strncat(str, parser, sizeof(str) - strlen(str) - 1); + _strlcat(str, parser, sizeof(str)); ret = load_plugin(str); if (!ret) return 0; strncpy(str, "./", sizeof(str)); str[sizeof(str) - 1] = '\0'; - strncat(str, name, sizeof(str) - strlen(str) - 1); - strncat(str, parser, sizeof(str) - strlen(str) - 1); + _strlcat(str, name, sizeof(str)); + _strlcat(str, parser, sizeof(str)); ret = load_plugin(str); if (!ret) return 0; @@ -99,8 +100,8 @@ int load_parser_plugin(const char *name) strncpy(str, exec_path, sizeof(str)); str[sizeof(str) - 1] = '\0'; - strncat(str, "/", sizeof(str) - strlen(str) - 1); - strncat(str, name, sizeof(str) - strlen(str) - 1); - strncat(str, parser, sizeof(str) - strlen(str) - 1); + _strlcat(str, "/", sizeof(str)); + _strlcat(str, name, sizeof(str)); + _strlcat(str, parser, sizeof(str)); return load_plugin(str); } diff --git a/rrdtool.c b/rrdtool.c index d1e85f4..98844c5 100644 --- a/rrdtool.c +++ b/rrdtool.c @@ -50,8 +50,7 @@ int rrdtool_draw_image(struct rrd_image *image) tmpfile[0] = 0; tmp[0] = 0; strncpy(tmpfile, image->image_filename, sizeof(tmpfile) - 1); - strncat(tmpfile, ".tmp", - sizeof(tmpfile) - strlen(image->image_filename) - 1); + _strlcat(tmpfile, ".tmp", sizeof(tmpfile)); if (image->updatestr) updatestr = image->updatestr; diff --git a/utils.h b/utils.h index 304e32a..14133cd 100644 --- a/utils.h +++ b/utils.h @@ -25,6 +25,14 @@ int _mutex_unlock(struct mutex *lock); #define mutex_unlock(lock) _mutex_unlock(lock) #define mutex_lock_acquired(lock) _mutex_lock_acquired(lock, __FILE__, __LINE__) +static inline char *_strlcat(char *dst, const char *src, size_t len) +{ + strncat(dst, src, len - strnlen(dst, len) - 1); + dst[len - 1] = '\0'; + + return dst; +} + static inline int mutex_init(struct mutex *mutex) { mutex->line = 0;