Re: [PATCH v1] perf string: Avoid undefined NULL+1

From: Arnaldo Carvalho de Melo
Date: Thu Dec 12 2024 - 13:57:57 EST


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@xxxxxxxxxx>
> > +++ 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@xxxxxxxxxx>

Thanks, applied to perf-tools-next,

- Arnaldo