Skip to contentSkip to conversation

tracing/synthetic: avoid comparing string against NULL

PATCH In review Arnd Bergmann 2 participants 2 messages Oct 13 – Nov 17 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing/synthetic: avoid comparing string against NULL

From: Arnd Bergmann <[email protected]>

gcc-10 warns about passing a NULL argument into __synth_event_add_val(), which uses it in an unchecked strcmp():

In function '__synth_event_add_val',
inlined from 'synth_event_add_next_val' at kernel/trace/trace_events_synth.c:2162:9:
kernel/trace/trace_events_synth.c:2076:8: error: argument 2 null where non-null expected [-Werror=nonnull]
2076 | if (strcmp(field->name, field_name) == 0)

      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In file included from include/linux/string.h:65,
from kernel/trace/trace_events_synth.c:8:
kernel/trace/trace_events_synth.c: In function 'synth_event_add_next_val':
arch/s390/include/asm/string.h:39:5: note: in a call to function 'strcmp' declared here

   39 | int strcmp(const char *s1, const char *s2);
      |     ^~~~~~

The code is actually correct here, as the trace_state->add_name does check for it, but this is hard for the compiler to prove.

Avoid the problem by repeating the check where the compiler sees it.

---
 kernel/trace/trace_events_synth.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_events_synth.c +2 -1
index dc15658a887c..c1287ca95aff 100644
@@ -2073,7 +2073,8 @@ static int __synth_event_add_val(const char *field_name, u64 val,
 	if (trace_state->add_name) {
 		for (i = 0; i < event->n_fields; i++) {
 			field = event->fields[i];
-			if (strcmp(field->name, field_name) == 0)
+			if (!field_name ||
+			    strcmp(field->name, field_name) == 0)
 				break;
 		}
 		if (!field) {
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 14 lines · On Tue, 13 Oct 2026 22:53:52 +0200 Arnd Bergmann <[email protected]> wrote:

From: Arnd Bergmann <[email protected]>

gcc-10 warns about passing a NULL argument into __synth_event_add_val(), which uses it in an unchecked strcmp():

In function '__synth_event_add_val',
inlined from 'synth_event_add_next_val' at kernel/trace/trace_events_synth.c:2162:9:
kernel/trace/trace_events_synth.c:2076:8: error: argument 2 null where non-null expected [-Werror=nonnull]
2076 | if (strcmp(field->name, field_name) == 0)

      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In file included from include/linux/string.h:65,
from kernel/trace/trace_events_synth.c:8:
kernel/trace/trace_events_synth.c: In function 'synth_event_add_next_val':
arch/s390/include/asm/string.h:39:5: note: in a call to function 'strcmp' declared here

Quoted 8 lines
   39 | int strcmp(const char *s1, const char *s2);
      |     ^~~~~~

The code is actually correct here, as the trace_state->add_name does check for it, but this is hard for the compiler to prove.

Avoid the problem by repeating the check where the compiler sees it.

Yeah, but the compiler is being stupid here. It's not that difficult for it to see it.

	if (field_name) {                      <<-- checks field_name
		if (trace_state->add_next) {
			ret = -EINVAL;
			goto out;
		}
		trace_state->add_name = true;  <<-- set add_name
	} else {
		if (trace_state->add_name) {  <<-- If field_name is NULL and add name is set, error out here
			ret = -EINVAL;
			goto out;
		}
		trace_state->add_next = true;
	}

	if (trace_state->disabled)
		goto out;

	event = trace_state->event;
	if (trace_state->add_name) {  <<-- Only get here if add_name is set (thus field_name is not NULL)
		for (i = 0; i < event->n_fields; i++) {
			field = event->fields[i];
			if (strcmp(field->name, field_name) == 0)   <<-- test field_name
				break;
		}
Quoted 11 lines
---
 kernel/trace/trace_events_synth.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_events_synth.c +0 -0
index dc15658a887c..c1287ca95aff 100644

kernel/trace/trace_events_synth.c static int __synth_event_add_val(const char *field_name, u64 val, · line 2077

Quoted 6 lines
kernel/trace/trace_events_synth.c +2 -1
@@ -2073,7 +2073,8 @@ static int __synth_event_add_val(const char *field_name, u64 val,
 	if (trace_state->add_name) {
 		for (i = 0; i < event->n_fields; i++) {
 			field = event->fields[i];
-			if (strcmp(field->name, field_name) == 0)
+			if (!field_name ||
+			    strcmp(field->name, field_name) == 0)

I don't like making the code look worse to please something stupid the compiler is doing.

-- Steve

Quoted 3 lines
kernel/trace/trace_events_synth.c +0 -0
 				break;
 		}
 		if (!field) {