From 74c3f8e27f5b750b71136ce1737972c28f288540 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Fri, 26 Mar 2021 15:34:21 -0400 Subject: [PATCH] fix: string constants (-Wwrite-strings) Do not use an anonymous string literal with a non-const char pointer as this will discard the implicit const, use static non-const string instead when appropriate. Otherwise, we could end up trying to write to a string constant. Change-Id: Ie2bb0e5ab7978930f9edcdb379bd181caaacc15c Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- doc/examples/easy-ust/sample_component_provider.h | 2 +- doc/examples/gen-tp/sample_tracepoint.tp | 2 +- liblttng-ust/lttng-context-procname.c | 4 ++-- liblttng-ust/ust-events-internal.h | 4 ++-- snprintf/vfprintf.c | 7 +++++-- tests/compile/test-app-ctx/hello.c | 3 ++- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/doc/examples/easy-ust/sample_component_provider.h b/doc/examples/easy-ust/sample_component_provider.h index edfac8f5..d457664d 100644 --- a/doc/examples/easy-ust/sample_component_provider.h +++ b/doc/examples/easy-ust/sample_component_provider.h @@ -74,7 +74,7 @@ TRACEPOINT_EVENT( * TP_ARGS() is valid to mean no arguments * TP_ARGS( void ) is valid too */ - TP_ARGS(char *, text), + TP_ARGS(const char *, text), /* * TP_FIELDS describes how to write the fields of the trace event. * You can use the args here diff --git a/doc/examples/gen-tp/sample_tracepoint.tp b/doc/examples/gen-tp/sample_tracepoint.tp index fa7d18a5..22f7713f 100644 --- a/doc/examples/gen-tp/sample_tracepoint.tp +++ b/doc/examples/gen-tp/sample_tracepoint.tp @@ -1,7 +1,7 @@ TRACEPOINT_EVENT( sample_tracepoint, message, // C++ Style comment - TP_ARGS(char *, text), + TP_ARGS(const char *, text), TP_FIELDS( ctf_string(message, text) ) diff --git a/liblttng-ust/lttng-context-procname.c b/liblttng-ust/lttng-context-procname.c index a65585de..53c42d9e 100644 --- a/liblttng-ust/lttng-context-procname.c +++ b/liblttng-ust/lttng-context-procname.c @@ -37,7 +37,7 @@ static DEFINE_URCU_TLS(procname_array, cached_procname); static DEFINE_URCU_TLS(int, procname_nesting); static inline -char *wrapper_getprocname(void) +const char *wrapper_getprocname(void) { int nesting = CMM_LOAD_SHARED(URCU_TLS(procname_nesting)); @@ -76,7 +76,7 @@ void procname_record(struct lttng_ust_ctx_field *field, struct lttng_ust_lib_ring_buffer_ctx *ctx, struct lttng_ust_channel_buffer *chan) { - char *procname; + const char *procname; procname = wrapper_getprocname(); chan->ops->event_write(ctx, procname, LTTNG_UST_ABI_PROCNAME_LEN, 1); diff --git a/liblttng-ust/ust-events-internal.h b/liblttng-ust/ust-events-internal.h index daf72b9d..786b650d 100644 --- a/liblttng-ust/ust-events-internal.h +++ b/liblttng-ust/ust-events-internal.h @@ -250,14 +250,14 @@ struct lttng_event_notifier_group { }; struct lttng_transport { - char *name; + const char *name; struct cds_list_head node; struct lttng_ust_channel_buffer_ops ops; const struct lttng_ust_lib_ring_buffer_config *client_config; }; struct lttng_counter_transport { - char *name; + const char *name; struct cds_list_head node; struct lttng_counter_ops ops; const struct lib_counter_config *client_config; diff --git a/snprintf/vfprintf.c b/snprintf/vfprintf.c index 5cb356c2..c2b07eb0 100644 --- a/snprintf/vfprintf.c +++ b/snprintf/vfprintf.c @@ -35,6 +35,9 @@ #include "fvwrite.h" #include "various.h" +static char null_str[] = "(null)"; +static char bad_base_str[] = "bug in ust_safe_vfprintf: bad base"; + union arg { int intarg; unsigned int uintarg; @@ -715,7 +718,7 @@ fp_common: goto nosign; case 's': if ((cp = GETARG(char *)) == NULL) - cp = "(null)"; + cp = null_str; if (prec >= 0) { /* * can't use strlen; can only look for the @@ -802,7 +805,7 @@ number: if ((dprec = prec) >= 0) break; default: - cp = "bug in ust_safe_vfprintf: bad base"; + cp = bad_base_str; size = strlen(cp); goto skipsize; } diff --git a/tests/compile/test-app-ctx/hello.c b/tests/compile/test-app-ctx/hello.c index c08fa8dd..da347714 100644 --- a/tests/compile/test-app-ctx/hello.c +++ b/tests/compile/test-app-ctx/hello.c @@ -243,9 +243,10 @@ void test_get_value(struct lttng_ust_ctx_field *field, } } +static char myprovider_name[] = "$app.myprovider"; struct lttng_ust_context_provider myprovider = { .struct_size = sizeof(struct lttng_ust_context_provider), - .name = "$app.myprovider", + .name = myprovider_name, .get_size = test_get_size, .record = test_record, .get_value = test_get_value, -- 2.34.1