Skip to contentSkip to conversation

libtracefs utest: Silence string-plus-int warnings

PATCH In review Ian Rogers 2 participants 5 messages Apr 23, 2021 – Apr 26, 2021

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/3 libtracefs utest: Silence string-plus-int warnings

Fix warnings of the form:

utest/tracefs-utest.c:476:35: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]

        CU_TEST(strstr(kprobes, KPROBE_1 + 2) != NULL);
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
---
 utest/tracefs-utest.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
utest/tracefs-utest.c +3 -3
index edb3a3d..c5efd5f 100644
@@ -453,21 +453,21 @@ static void test_instance_file(void)
 	CU_TEST(ret == strlen(KPROBE_1));
 	kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
 	CU_TEST(kprobes != NULL);
-	CU_TEST(strstr(kprobes, KPROBE_1 + 2) != NULL);
+	CU_TEST(strstr(kprobes, &KPROBE_1[2]) != NULL);
 	free(kprobes);
 
 	ret = tracefs_instance_file_append(NULL, KPROB_EVTS, KPROBE_2);
 	CU_TEST(ret == strlen(KPROBE_2));
 	kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
 	CU_TEST(kprobes != NULL);
-	CU_TEST(strstr(kprobes, KPROBE_2 + 2) != NULL);
+	CU_TEST(strstr(kprobes, &KPROBE_2[2]) != NULL);
 	free(kprobes);
 
 	ret = tracefs_instance_file_append(NULL, KPROB_EVTS, KPROBE_1_RM);
 	CU_TEST(ret == strlen(KPROBE_1_RM));
 	kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
 	CU_TEST(kprobes != NULL);
-	CU_TEST(strstr(kprobes, KPROBE_1 + 2) == NULL);
+	CU_TEST(strstr(kprobes, &KPROBE_1[2]) == NULL);
 	free(kprobes);
 
 	ret = tracefs_instance_file_clear(NULL, KPROB_EVTS);
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
author
#1 lore raw

PATCH 2/3 libtracefs utest: Fix a sometimes uninitialized variable.

The warning looks like:

utest/tracefs-utest.c:706:7: error: variable 'fd' is used uninitialized whenever 'if' condition is true [-Werror,-Wsometimes-uninitialized]

                if (rstat != -1)
                    ^~~~~~~~~~~
---
 utest/tracefs-utest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utest/tracefs-utest.c +1 -1
index c5efd5f..a0d911b 100644
@@ -659,7 +659,7 @@ static bool check_option(struct tracefs_instance *instance,
 	bool supported;
 	struct stat st;
 	char buf[10];
-	int fd;
+	int fd = 0;
 	int r;
 	int rstat;
 
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
author
#2 lore raw

PATCH 3/3 libtracefs utest: Make a null string fatal if it is passed to strstr

In reply to Ian Rogers

strstr will segv for a null string. If a string is passed to strstr and is null there is no point segving, it is better to fail the earlier test with a fatal and continue the rest of the suite. This allows the suite to complete with an error rather than die.

---
 utest/tracefs-utest.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
utest/tracefs-utest.c +5 -5
index a0d911b..e846dc4 100644
@@ -452,21 +452,21 @@ static void test_instance_file(void)
 	ret = tracefs_instance_file_write(NULL, KPROB_EVTS, KPROBE_1);
 	CU_TEST(ret == strlen(KPROBE_1));
 	kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
-	CU_TEST(kprobes != NULL);
+	CU_TEST_FATAL(kprobes != NULL);
 	CU_TEST(strstr(kprobes, &KPROBE_1[2]) != NULL);
 	free(kprobes);
 
 	ret = tracefs_instance_file_append(NULL, KPROB_EVTS, KPROBE_2);
 	CU_TEST(ret == strlen(KPROBE_2));
 	kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
-	CU_TEST(kprobes != NULL);
+	CU_TEST_FATAL(kprobes != NULL);
 	CU_TEST(strstr(kprobes, &KPROBE_2[2]) != NULL);
 	free(kprobes);
 
 	ret = tracefs_instance_file_append(NULL, KPROB_EVTS, KPROBE_1_RM);
 	CU_TEST(ret == strlen(KPROBE_1_RM));
 	kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
-	CU_TEST(kprobes != NULL);
+	CU_TEST_FATAL(kprobes != NULL);
 	CU_TEST(strstr(kprobes, &KPROBE_1[2]) == NULL);
 	free(kprobes);
 
@@ -1026,7 +1026,7 @@ static void current_clock_check(struct tracefs_instance *instance, const char *c
 	char *str;
 
 	clocks = tracefs_instance_file_read(instance, TRACE_CLOCK, &size);
-	CU_TEST(clocks != NULL);
+	CU_TEST_FATAL(clocks != NULL);
 	CU_TEST(size > strlen(clock));
 	str = strstr(clocks, clock);
 	CU_TEST(str != NULL);
@@ -1041,7 +1041,7 @@ static void test_instance_get_clock(struct tracefs_instance *instance)
 	const char *clock;
 
 	clock = tracefs_get_clock(instance);
-	CU_TEST(clock != NULL);
+	CU_TEST_FATAL(clock != NULL);
 	current_clock_check(instance, clock);
 	free((char *)clock);
 }
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
Acked-by on v1 1/3
#3 lore raw
In reply to Ian Rogers
Quoted 7 lines · Ian Rogers

Fix warnings of the form:

utest/tracefs-utest.c:476:35: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]

        CU_TEST(strstr(kprobes, KPROBE_1 + 2) != NULL);
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~

Hi Ian,
Thanks for testing and sending this patch. What compiler/version/options you are using to get these warnings ? The whole patchset looks good to me.

Quoted 35 lines
---
 utest/tracefs-utest.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
utest/tracefs-utest.c +3 -4
index edb3a3d..c5efd5f 100644
@@ -453,21 +453,21 @@ static void test_instance_file(void)
        CU_TEST(ret == strlen(KPROBE_1));
        kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
        CU_TEST(kprobes != NULL);
-       CU_TEST(strstr(kprobes, KPROBE_1 + 2) != NULL);
+       CU_TEST(strstr(kprobes, &KPROBE_1[2]) != NULL);
        free(kprobes);
 
        ret = tracefs_instance_file_append(NULL, KPROB_EVTS, KPROBE_2);
        CU_TEST(ret == strlen(KPROBE_2));
        kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
        CU_TEST(kprobes != NULL);
-       CU_TEST(strstr(kprobes, KPROBE_2 + 2) != NULL);
+       CU_TEST(strstr(kprobes, &KPROBE_2[2]) != NULL);
        free(kprobes);
 
        ret = tracefs_instance_file_append(NULL, KPROB_EVTS, KPROBE_1_RM);
        CU_TEST(ret == strlen(KPROBE_1_RM));
        kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
        CU_TEST(kprobes != NULL);
-       CU_TEST(strstr(kprobes, KPROBE_1 + 2) == NULL);
+       CU_TEST(strstr(kprobes, &KPROBE_1[2]) == NULL);
        free(kprobes);
 
        ret = tracefs_instance_file_clear(NULL, KPROB_EVTS);
--
Quoted 2 lines

2.31.1.498.g6c1eba8ee3d-goog

--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center

author
#4 lore raw
Quoted 10 lines · Tzvetomir Stoyanov
Quoted 7 lines · Ian Rogers

Fix warnings of the form:

utest/tracefs-utest.c:476:35: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]

        CU_TEST(strstr(kprobes, KPROBE_1 + 2) != NULL);
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
Quoted 4 lines

Hi Ian,
Thanks for testing and sending this patch. What compiler/version/options you are using to get these warnings ? The whole patchset looks good to me.

(Re-send with plain-text mode enabled).

Thanks, the compiler is clang 12.

Ian

Quoted 47 lines
Quoted 35 lines
---
 utest/tracefs-utest.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
utest/tracefs-utest.c +3 -4
index edb3a3d..c5efd5f 100644
@@ -453,21 +453,21 @@ static void test_instance_file(void)
        CU_TEST(ret == strlen(KPROBE_1));
        kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
        CU_TEST(kprobes != NULL);
-       CU_TEST(strstr(kprobes, KPROBE_1 + 2) != NULL);
+       CU_TEST(strstr(kprobes, &KPROBE_1[2]) != NULL);
        free(kprobes);
 
        ret = tracefs_instance_file_append(NULL, KPROB_EVTS, KPROBE_2);
        CU_TEST(ret == strlen(KPROBE_2));
        kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
        CU_TEST(kprobes != NULL);
-       CU_TEST(strstr(kprobes, KPROBE_2 + 2) != NULL);
+       CU_TEST(strstr(kprobes, &KPROBE_2[2]) != NULL);
        free(kprobes);
 
        ret = tracefs_instance_file_append(NULL, KPROB_EVTS, KPROBE_1_RM);
        CU_TEST(ret == strlen(KPROBE_1_RM));
        kprobes = tracefs_instance_file_read(NULL, KPROB_EVTS, &size);
        CU_TEST(kprobes != NULL);
-       CU_TEST(strstr(kprobes, KPROBE_1 + 2) == NULL);
+       CU_TEST(strstr(kprobes, &KPROBE_1[2]) == NULL);
        free(kprobes);
 
        ret = tracefs_instance_file_clear(NULL, KPROB_EVTS);
--
Quoted 2 lines

2.31.1.498.g6c1eba8ee3d-goog

--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center