port: fix -Wdeprecated-declarations warning about sprintf on macOS clang 14
authorMichael Jeanson <mjeanson@efficios.com>
Mon, 27 Feb 2023 20:16:03 +0000 (15:16 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 2 Mar 2023 01:30:55 +0000 (20:30 -0500)
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: I69dff6379623aefcfd60d6ad1ad96b48ae637155
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/9592
Tested-by: jenkins <jenkins@lttng.org>
src/common/common.c
src/common/uuid.c
src/plugins/ctf/common/metadata/visitor-generate-ir.cpp
src/plugins/ctf/fs-sink/translate-trace-ir-to-ctf-ir.cpp
src/plugins/utils/trimmer/trimmer.c

index c82bee330c2e2ff963c10a044e5bcdcef63fd813..aca73e5fb757b47a84717612a3a4fc8b41b06095 100644 (file)
@@ -2041,7 +2041,7 @@ int bt_common_g_string_append_printf(GString *str, const char *fmt, ...)
                /* Resize. */
                g_string_set_size(str, len + print_len);
                va_start(ap, fmt);
-               print_len = vsprintf(str->str + len, fmt, ap);
+               print_len = vsnprintf(str->str + len, print_len + 1, fmt, ap);
                va_end(ap);
        } else {
                str->len = len + print_len;
index a0af2bc04dc75b57238cfda7d88ed000f92bbe32..b2457602c7b57c841fbc92b614691b7644c1b21b 100644 (file)
@@ -57,7 +57,7 @@ void bt_uuid_to_str(const bt_uuid_t uuid_in, char *str_out)
        BT_ASSERT_DBG(uuid_in);
        BT_ASSERT_DBG(str_out);
 
-       sprintf(str_out, BT_UUID_FMT, BT_UUID_FMT_VALUES(uuid_in));
+       snprintf(str_out, BT_UUID_STR_LEN + 1, BT_UUID_FMT, BT_UUID_FMT_VALUES(uuid_in));
 }
 
 BT_HIDDEN
index 50d8d441fe7ed312a16b0ab99762950bc8a332b6..a55cbc375fb20d4b9b6dbeb8eff84c8123b31102 100644 (file)
@@ -23,6 +23,7 @@
 #include <glib.h>
 #include <inttypes.h>
 #include <errno.h>
+#include <string>
 #include "common/common.h"
 #include "common/uuid.h"
 #include "compat/endian.h"
@@ -265,23 +266,9 @@ end:
 static GQuark get_prefixed_named_quark(struct ctf_visitor_generate_ir *ctx, char prefix,
                                        const char *name)
 {
-    GQuark qname = 0;
-
     BT_ASSERT(name);
-
-    /* Prefix character + original string + '\0' */
-    char *prname = g_new(char, strlen(name) + 2);
-    if (!prname) {
-        _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to allocate a string.");
-        goto end;
-    }
-
-    sprintf(prname, "%c%s", prefix, name);
-    qname = g_quark_from_string(prname);
-    g_free(prname);
-
-end:
-    return qname;
+    std::string prname = std::string {prefix} + name;
+    return g_quark_from_string(prname.c_str());
 }
 
 /**
index d7ff260f271d8b49e5d006b72fcf53ea6d2e26d6..ec7e566d6030123c8e197e02fcf5369edfe385b7 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <string.h>
+#include <string>
 #include <glib.h>
 
 #include "fs-sink.hpp"
@@ -1562,17 +1563,15 @@ end:
 static void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class *sc)
 {
     unsigned int suffix = 0;
-    char buf[16];
 
-    g_string_assign(sc->default_clock_class_name, "");
-    sprintf(buf, "default");
+    std::string name = "default";
 
-    while (default_clock_class_name_exists(sc->trace, buf)) {
-        sprintf(buf, "default%u", suffix);
+    while (default_clock_class_name_exists(sc->trace, name.c_str())) {
+        name = "default" + std::to_string(suffix);
         suffix++;
     }
 
-    g_string_assign(sc->default_clock_class_name, buf);
+    g_string_assign(sc->default_clock_class_name, name.c_str());
 }
 
 static int translate_stream_class(struct fs_sink_comp *fs_sink, struct fs_sink_ctf_trace *trace,
index a65b2ecc881fcd5928e969a353a35bae9a3f58bb..68230d5ef440f2e2f1159b53b739781412fd7544 100644 (file)
@@ -440,7 +440,7 @@ int set_bound_from_param(struct trimmer_comp *trimmer_comp,
                 * Just convert it to a temporary string to handle
                 * everything the same way.
                 */
-               sprintf(tmp_arg, "%" PRId64, value);
+               snprintf(tmp_arg, sizeof(tmp_arg), "%" PRId64, value);
                arg = tmp_arg;
        } else {
                BT_ASSERT(bt_value_is_string(param));
This page took 0.029 seconds and 4 git commands to generate.