tools/perf/util/string.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
While the value NULL+1 is never used it triggers a ubsan
warning. Restructure and comment the loop to avoid this.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/string.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
index 308fc7ec88cc..c0e927bbadf6 100644
--- a/tools/perf/util/string.c
+++ b/tools/perf/util/string.c
@@ -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;
}
--
2.47.0.371.ga323438b13-goog
On 20/11/2024 6:52 am, Ian Rogers wrote:
> While the value NULL+1 is never used it triggers a ubsan
> warning. Restructure and comment the loop to avoid this.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> tools/perf/util/string.c | 15 ++++++++++++---
> 1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
> index 308fc7ec88cc..c0e927bbadf6 100644
> --- a/tools/perf/util/string.c
> +++ b/tools/perf/util/string.c
> @@ -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;
> }
Reviewed-by: James Clark <james.clark@linaro.org>
On Mon, Dec 09, 2024 at 11:49:15AM +0000, James Clark wrote:
> On 20/11/2024 6:52 am, Ian Rogers wrote:
> > While the value NULL+1 is never used it triggers a ubsan
> > warning. Restructure and comment the loop to avoid this.
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > +++ b/tools/perf/util/string.c
> > @@ -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;
> > }
> Reviewed-by: James Clark <james.clark@linaro.org>
Thanks, applied to perf-tools-next,
- Arnaldo
© 2016 - 2026 Red Hat, Inc.