Skip to contentSkip to conversation

perf string: Avoid undefined NULL+1

PATCH Applied Ian Rogers 3 participants 3 messages Nov 20, 2024 – Dec 12, 2024 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf string: Avoid undefined NULL+1

While the value NULL+1 is never used it triggers a ubsan warning. Restructure and comment the loop to avoid this.

---
 tools/perf/util/string.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
tools/perf/util/string.c +12 -3
index 308fc7ec88cc..c0e927bbadf6 100644
@@ -254,11 +254,20 @@ char *strpbrk_esc(char *str, const char *stopset)
 
 	do {
 		ptr = strpbrk(str, stopset);
-		if (ptr == str ||
-		    (ptr == str + 1 && *(ptr - 1) != '\\'))
+		if (!ptr) {
+			/* stopset not in str. */
 			break;
+		}
+		if (ptr == str) {
+			/* stopset character is first in str. */
+			break;
+		}
+		if (ptr == str + 1 && str[0] != '\\') {
+			/* stopset chacter is second and wasn't preceded by a '\'. */
+			break;
+		}
 		str = ptr + 1;
-	} while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
+	} while (ptr[-1] == '\\' && ptr[-2] != '\\');
 
 	return ptr;
 }
Signature · 1 line
2.47.0.371.ga323438b13-goog
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 26 lines · Ian Rogers

While the value NULL+1 is never used it triggers a ubsan warning. Restructure and comment the loop to avoid this.

---
  tools/perf/util/string.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
tools/perf/util/string.c +7 -2
index 308fc7ec88cc..c0e927bbadf6 100644
@@ -254,11 +254,20 @@ char *strpbrk_esc(char *str, const char *stopset)
 
  	do {
  		ptr = strpbrk(str, stopset);
-		if (ptr == str ||
-		    (ptr == str + 1 && *(ptr - 1) != '\\'))
+		if (!ptr) {
+			/* stopset not in str. */
  			break;
+		}
+		if (ptr == str) {
+			/* stopset character is first in str. */
+			break;
+		}

tools/perf/util/string.c char *strpbrk_esc(char *str, const char *stopset) · line 273

Quoted 10 lines
tools/perf/util/string.c +5 -1
+		if (ptr == str + 1 && str[0] != '\\') {
+			/* stopset chacter is second and wasn't preceded by a '\'. */
+			break;
+		}
  		str = ptr + 1;
-	} while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
+	} while (ptr[-1] == '\\' && ptr[-2] != '\\');
 
  	return ptr;
  }
maintainer Applied
#2 lore raw
Quoted 22 lines · James Clark
Quoted 21 lines · Ian Rogers

While the value NULL+1 is never used it triggers a ubsan warning. Restructure and comment the loop to avoid this.

+++ b/tools/perf/util/string.c

tools/perf/util/string.c +10 -2
@@ -254,11 +254,20 @@ char *strpbrk_esc(char *str, const char *stopset)
  	do {
  		ptr = strpbrk(str, stopset);
-		if (ptr == str ||
-		    (ptr == str + 1 && *(ptr - 1) != '\\'))
+		if (!ptr) {
+			/* stopset not in str. */
  			break;
+		}
+		if (ptr == str) {
+			/* stopset character is first in str. */
+			break;
+		}
+		if (ptr == str + 1 && str[0] != '\\') {
+			/* stopset chacter is second and wasn't preceded by a '\'. */
+			break;
Quoted 8 lines

tools/perf/util/string.c char *strpbrk_esc(char *str, const char *stopset) · line 273

Quoted 6 lines
tools/perf/util/string.c +2 -1
+		}
  		str = ptr + 1;
-	} while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
+	} while (ptr[-1] == '\\' && ptr[-2] != '\\');
  	return ptr;
  }

Thanks, applied to perf-tools-next,

- Arnaldo