From 51a7b11ec8712e8888af09cc3d3df3bb848f70e9 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Tue, 7 Mar 2023 14:16:44 -0500 Subject: [PATCH] port: fix -Wdeprecated-declarations warning about sprintf on macOS clang 14 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/common/string-utils/string-utils.cpp | 6 ++++-- src/common/utils.cpp | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) 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); /* -- 2.34.1