From 0e733f80a39413148f4f279b91f4f35c7a638527 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 11 Sep 2023 09:48:14 -0400 Subject: [PATCH] ctf: fix -Wformat-overflow warning in ctf-meta-resolve.cpp Compiling with gcc 13.2.1 (Arch's gcc version as of today) and -O2, I get: libtool: compile: ccache g++ -std=c++11 -DHAVE_CONFIG_H -I/home/smarchi/src/babeltrace/include -I../../../../../src -I/home/smarchi/src/babeltrace/src -include common/config.h -I. -I/home/smarchi/src/babeltrace/src/plugins/ctf/common/metadata -fvisibility=hidden -fvisibility-inlines-hidden -pthread -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-4 -pthread -Wall -Wextra -Wmissing-prototypes -Wmissing-declarations -Wnull-dereference -Wundef -Wredundant-decls -Wshadow -Wjump-misses-init -Wsuggest-attribute=format -Wnested-externs -Wwrite-strings -Wformat=2 -Wstrict-aliasing -Wmissing-noreturn -Winit-self -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wsuggest-override -Wno-unused-parameter -Wno-sign-compare -Wno-cast-function-type -Wno-missing-field-initializers -Wno-maybe-uninitialized -Werror -g3 -O2 -fmax-errors=1 -fdiagnostics-color=always -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_28 -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_28 -D_GLIBCXX_DEBUG=1 -MT ctf-meta-resolve.lo -MD -MP -MF .deps/ctf-meta-resolve.Tpo -c /home/smarchi/src/babeltrace/src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp -fPIC -DPIC -o .libs/ctf-meta-resolve.o In file included from /home/smarchi/src/babeltrace/src/logging/comp-logging.h:16, from /home/smarchi/src/babeltrace/src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp:12: /home/smarchi/src/babeltrace/src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp: In function 'int64_t get_field_paths_lca_index(ctf_field_path*, ctf_field_path*, resolve_context*)': /home/smarchi/src/babeltrace/src/logging/comp-logging.h:18:41: error: '%s' directive argument is null [-Werror=format-overflow=] 18 | #define _BT_COMP_LOG_COMP_PREFIX "[%s] " /home/smarchi/src/babeltrace/src/logging/log.h:807:67: note: in definition of macro 'BT_LOG_WRITE' 807 | lvl, tag, __VA_ARGS__); \ | ^~~~~~~~~~~ /home/smarchi/src/babeltrace/src/logging/comp-logging.h:23:42: note: in expansion of macro '_BT_COMP_LOG_COMP_PREFIX' 23 | BT_LOG_WRITE((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \ | ^~~~~~~~~~~~~~~~~~~~~~~~ /home/smarchi/src/babeltrace/src/logging/comp-logging.h:83:9: note: in expansion of macro 'BT_COMP_LOG' 83 | BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) | ^~~~~~~~~~~ /home/smarchi/src/babeltrace/src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp:734:9: note: in expansion of macro 'BT_COMP_LOGD' 734 | BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: " | ^~~~~~~~~~~~ It is undefined what happens when %s receives NULL. Use the literal string "(null)" instead of NULL. Change-Id: I4f53165ff9fc08fb373f7c1cdf87a63f2b792532 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/10856 Reviewed-by: Philippe Proulx --- src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp b/src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp index 7042ec3a..c77ec46b 100644 --- a/src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp +++ b/src/plugins/ctf/common/metadata/ctf-meta-resolve.cpp @@ -728,8 +728,8 @@ static int64_t get_field_paths_lca_index(struct ctf_field_path *field_path1, if (BT_LOG_ON_TRACE) { GString *field_path1_pretty = ctf_field_path_string(field_path1); GString *field_path2_pretty = ctf_field_path_string(field_path2); - const char *field_path1_pretty_str = field_path1_pretty ? field_path1_pretty->str : NULL; - const char *field_path2_pretty_str = field_path2_pretty ? field_path2_pretty->str : NULL; + const char *field_path1_pretty_str = field_path1_pretty ? field_path1_pretty->str : "(null)"; + const char *field_path2_pretty_str = field_path2_pretty ? field_path2_pretty->str : "(null)"; BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: " "field-path-1=\"%s\", field-path-2=\"%s\"", -- 2.34.1