port: fix -Wdeprecated-declarations warning about sprintf on macOS clang 14
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 7 Mar 2023 19:16:44 +0000 (14:16 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 16 Mar 2023 15:02:58 +0000 (11:02 -0400)
Remove uses of sprintf to fix this warning:

    warning: 'sprintf' is deprecated: This function is provided for
    compatibility reasons only.  Due to security concerns inherent in the
    design of sprintf(3), it is highly recommended that you use snprintf(3)
    instead. [-Wdeprecated-declarations]

Change-Id: Ifff3746c1cc4e51a8cf4c08ccd845c887d76c6be
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/string-utils/string-utils.cpp
src/common/utils.cpp

index 63de5267442721ac861ffb666c6a6b42d332ccb4..4f727e2e8ed0fe9f407249e538a4dfe1b80d7f85 100644 (file)
@@ -395,6 +395,7 @@ int strutils_appendf(char **s, const char *fmt, ...)
 {
        char *new_str;
        size_t oldlen = (*s) ? strlen(*s) : 0;
+       size_t addlen = 0;
        int ret;
        va_list args;
 
@@ -408,7 +409,8 @@ int strutils_appendf(char **s, const char *fmt, ...)
        }
 
        /* Allocate space for old string + new string + \0. */
-       new_str = zmalloc<char>(oldlen + ret + 1);
+       addlen = ret + 1;
+       new_str = zmalloc<char>(oldlen + addlen);
        if (!new_str) {
                ret = -ENOMEM;
                goto end;
@@ -421,7 +423,7 @@ int strutils_appendf(char **s, const char *fmt, ...)
 
        /* Format new string in-place. */
        va_start(args, fmt);
-       ret = vsprintf(&new_str[oldlen], fmt, args);
+       ret = vsnprintf(&new_str[oldlen], addlen, fmt, args);
        va_end(args);
 
        if (ret == -1) {
index 831926aab16c48656082113a984386d530be3203..0f6e0362f2d32f6c557ffede0341ed0d829b2ff7 100644 (file)
@@ -1032,7 +1032,7 @@ int utils_show_help(int section, const char *page_name, const char *help_msg)
        }
 
        /* Section integer -> section string */
-       ret = sprintf(section_string, "%d", section);
+       ret = snprintf(section_string, sizeof(section_string), "%d", section);
        LTTNG_ASSERT(ret > 0 && ret < 8);
 
        /*
This page took 0.026575 seconds and 4 git commands to generate.