From: Michael Jeanson Date: Tue, 7 Mar 2023 19:16:44 +0000 (-0500) Subject: port: fix -Wdeprecated-declarations warning about sprintf on macOS clang 14 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=51a7b11ec8712e8888af09cc3d3df3bb848f70e9 port: fix -Wdeprecated-declarations warning about sprintf on macOS clang 14 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 Signed-off-by: Jérémie Galarneau --- diff --git a/src/common/string-utils/string-utils.cpp b/src/common/string-utils/string-utils.cpp index 63de52674..4f727e2e8 100644 --- a/src/common/string-utils/string-utils.cpp +++ b/src/common/string-utils/string-utils.cpp @@ -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(oldlen + ret + 1); + addlen = ret + 1; + new_str = zmalloc(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) { diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 831926aab..0f6e0362f 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -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); /*