Skip to contentSkip to conversation

libtracefs: Move initialization below a null test.

PATCH In review Ian Rogers 2 participants 5 messages Dec 15, 2022

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/2 libtracefs: Move initialization below a null test.

Computing the address from a NULL pointer results in undefined behavior which things like undefined behavior sanitizer promote into real failures.

---
 src/tracefs-dynevents.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
src/tracefs-dynevents.c +7 -2
index 48bb26a..02e8023 100644
@@ -709,13 +709,18 @@ dynevent_info(struct tracefs_dynevent *dynevent, char **system,
 	      char **event, char **prefix, char **addr, char **format)
 {
 	char **lv[] = { system, event, prefix, addr, format };
-	char **rv[] = { &dynevent->system, &dynevent->event, &dynevent->prefix,
-			&dynevent->address, &dynevent->format };
+	char **rv[] = { NULL, NULL, NULL, NULL, NULL };
 	int i;
 
 	if (!dynevent)
 		return TRACEFS_DYNEVENT_UNKNOWN;
 
+	rv[0] = &dynevent->system;
+	rv[1] = &dynevent->event;
+	rv[2] = &dynevent->prefix;
+	rv[3] = &dynevent->address;
+	rv[4] = &dynevent->format;
+
 	for (i = 0; i < ARRAY_SIZE(lv); i++) {
 		if (lv[i]) {
 			if (*rv[i]) {
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
author
#1 lore raw

PATCH 2/2 libtracefs: Fix compiler -Wmemsize-comparison warning

Fixes the following clang warning:

utest/tracefs-utest.c:828:75: error: size argument in 'strncmp' call is a comparison [-Werror,-Wmemsize-comparison]

            (!save_debug || strncmp(save_debug, save_tracing, strlen(save_debug) != 0)))
                                                              ~~~~~~~~~~~~~~~~~~~^~~~
utest/tracefs-utest.c:828:22: note: did you mean to compare the result of 'strncmp' instead?
            (!save_debug || strncmp(save_debug, save_tracing, strlen(save_debug) != 0)))
                            ^                                                        ~
                                                                                )
utest/tracefs-utest.c:828:56: note: explicitly cast the argument to size_t to silence this warning
            (!save_debug || strncmp(save_debug, save_tracing, strlen(save_debug) != 0)))
                                                              ^
                                                              (size_t)(              )
---
 utest/tracefs-utest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utest/tracefs-utest.c +1 -1
index 629cf2b..e0e3c07 100644
@@ -825,7 +825,7 @@ static void test_mounting(void)
 		mount("debugfs", save_debug, "debugfs", 0, NULL);
 
 	if (save_tracing &&
-	    (!save_debug || strncmp(save_debug, save_tracing, strlen(save_debug) != 0)))
+	    (!save_debug || strncmp(save_debug, save_tracing, strlen(save_debug)) != 0))
 		mount("tracefs", save_tracing, "tracefs", 0, NULL);
 
 	free(save_debug);
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
#2 lore raw

Re: [PATCH v1 2/2] libtracefs: Fix compiler -Wmemsize-comparison warning

Quoted 13 lines · On Thu, 15 Dec 2022 10:31:51 -0800 Ian Rogers <[email protected]> wrote:

Fixes the following clang warning:

utest/tracefs-utest.c:828:75: error: size argument in 'strncmp' call is a comparison [-Werror,-Wmemsize-comparison]

            (!save_debug || strncmp(save_debug, save_tracing, strlen(save_debug) != 0)))
                                                              ~~~~~~~~~~~~~~~~~~~^~~~
utest/tracefs-utest.c:828:22: note: did you mean to compare the result of 'strncmp' instead?
            (!save_debug || strncmp(save_debug, save_tracing, strlen(save_debug) != 0)))
                            ^                                                        ~
                                                                                )
utest/tracefs-utest.c:828:56: note: explicitly cast the argument to size_t to silence this warning
            (!save_debug || strncmp(save_debug, save_tracing, strlen(save_debug) != 0)))
                                                              ^
                                                              (size_t)(              )

This is why I hate lisp!

Thanks, I'll apply this.

-- Steve

#3 lore raw
In reply to Ian Rogers
Quoted 3 lines · On Thu, 15 Dec 2022 10:31:50 -0800 Ian Rogers <[email protected]> wrote:

Computing the address from a NULL pointer results in undefined behavior which things like undefined behavior sanitizer promote into real failures.

I thought commit aff006d4af0c7 ("libtracefs: Do not initialize with NULL offsets") was suppose to fix this.

Oh, I think I may have forgotten to remove the check against dynevent!

Does this work instead?

-- Steve

src/tracefs-dynevents.c +0 -3
index 48bb26a96c58..7a3c45ce25a3 100644
@@ -713,9 +713,6 @@ dynevent_info(struct tracefs_dynevent *dynevent, char **system,
 			&dynevent->address, &dynevent->format };
 	int i;
 
-	if (!dynevent)
-		return TRACEFS_DYNEVENT_UNKNOWN;
-
 	for (i = 0; i < ARRAY_SIZE(lv); i++) {
 		if (lv[i]) {
 			if (*rv[i]) {
author
#4 lore raw
Quoted 10 lines · Steven Rostedt
Quoted 3 lines · On Thu, 15 Dec 2022 10:31:50 -0800 Ian Rogers <[email protected]> wrote:

Computing the address from a NULL pointer results in undefined behavior which things like undefined behavior sanitizer promote into real failures.

I thought commit aff006d4af0c7 ("libtracefs: Do not initialize with NULL offsets") was suppose to fix this.

It may have done, sorry for not keeping track. I went to my libtracefs git repo to fix the patch 2 issue and saw that this change didn't vanish with a rebase.

Quoted 11 lines

Oh, I think I may have forgotten to remove the check against dynevent!

Does this work instead?

-- Steve

src/tracefs-dynevents.c +0 -0
index 48bb26a96c58..7a3c45ce25a3 100644

src/tracefs-dynevents.c dynevent_info(struct tracefs_dynevent *dynevent, char **system, · line 718

Quoted 6 lines
src/tracefs-dynevents.c +0 -3
@@ -713,9 +713,6 @@ dynevent_info(struct tracefs_dynevent *dynevent, char **system,
                        &dynevent->address, &dynevent->format };
        int i;
 
-       if (!dynevent)
-               return TRACEFS_DYNEVENT_UNKNOWN;
-

I think the issue is above in &dynevent->... that dynevent may be NULL. It seems from removing this that dynevent can't be NULL and so the undefined behavior can never happen - in which case there's no issue to fix and the test is unnecessary and should be removed. That's a long winded way of saying I think that's a correct patch and this patch is unnecessary :-)

Thanks,
Ian

Quoted 3 lines
src/tracefs-dynevents.c +0 -0
        for (i = 0; i < ARRAY_SIZE(lv); i++) {
                if (lv[i]) {
                        if (*rv[i]) {