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>
#include "plugin.h"
#include "debug.h"
#include "version.h"
#include "plugin.h"
#include "debug.h"
#include "version.h"
strncpy(str, name, sizeof(str));
str[sizeof(str) - 1] = '\0';
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';
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;
ret = load_plugin(str);
if (!ret)
return 0;
strncpy(str, exec_path, sizeof(str));
str[sizeof(str) - 1] = '\0';
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);
}
return load_plugin(str);
}
tmpfile[0] = 0;
tmp[0] = 0;
strncpy(tmpfile, image->image_filename, sizeof(tmpfile) - 1);
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;
if (image->updatestr)
updatestr = image->updatestr;
#define mutex_unlock(lock) _mutex_unlock(lock)
#define mutex_lock_acquired(lock) _mutex_lock_acquired(lock, __FILE__, __LINE__)
#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;
static inline int mutex_init(struct mutex *mutex)
{
mutex->line = 0;