]> git.itanic.dy.fi Git - rrdd/commitdiff
Replace strncat with _strlcat
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 11 Oct 2020 11:08:09 +0000 (14:08 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 11 Oct 2020 11:18:44 +0000 (14:18 +0300)
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 <timo.t.kokkonen@iki.fi>
plugin_manager.c
rrdtool.c
utils.h

index e91698090613eaf696dabaecbbd764bda15b56e4..e13e01f7d11b8175c7b9e72eb1ce53f809a14e0f 100644 (file)
@@ -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);
 }
index d1e85f4c1d398750ed25d81ad7ee15866f68ad85..98844c596af11ed0d9dc5061d11507a996a2950e 100644 (file)
--- 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 304e32a69013d8205aad1d46b06ed8685ea9424f..14133cdbee1607920fff43f187619ac32e20b5f7 100644 (file)
--- 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;