[PATCH v2 1/5] perf disasm: Avoid undefined behavior in incrementing NULL

Ian Rogers posted 5 patches 1 month, 1 week ago
[PATCH v2 1/5] perf disasm: Avoid undefined behavior in incrementing NULL
Posted by Ian Rogers 1 month, 1 week ago
Incrementing NULL is undefined behavior and triggers ubsan during the
perf annotate test. Split a compound statement over two lines to avoid
this.

Fixes: 98f69a573c66 ("perf annotate: Split out util/disasm.c")
Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/disasm.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index b1e4919d016f..e257bd918c89 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -390,13 +390,16 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s
 	 * skip over possible up to 2 operands to get to address, e.g.:
 	 * tbnz	 w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
 	 */
-	if (c++ != NULL) {
+	if (c != NULL) {
+		c++;
 		ops->target.addr = strtoull(c, NULL, 16);
 		if (!ops->target.addr) {
 			c = strchr(c, ',');
 			c = validate_comma(c, ops);
-			if (c++ != NULL)
+			if (c != NULL) {
+				c++;
 				ops->target.addr = strtoull(c, NULL, 16);
+			}
 		}
 	} else {
 		ops->target.addr = strtoull(ops->raw, NULL, 16);
-- 
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v2 1/5] perf disasm: Avoid undefined behavior in incrementing NULL
Posted by Collin Funk 1 month, 1 week ago
Ian Rogers <irogers@google.com> writes:

> Incrementing NULL is undefined behavior and triggers ubsan during the
> perf annotate test. Split a compound statement over two lines to avoid
> this.
>
> Fixes: 98f69a573c66 ("perf annotate: Split out util/disasm.c")
> Signed-off-by: Ian Rogers <irogers@google.com>
> Acked-by: Namhyung Kim <namhyung@kernel.org>
> ---
>  tools/perf/util/disasm.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index b1e4919d016f..e257bd918c89 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -390,13 +390,16 @@ static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_s
>  	 * skip over possible up to 2 operands to get to address, e.g.:
>  	 * tbnz	 w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
>  	 */
> -	if (c++ != NULL) {
> +	if (c != NULL) {
> +		c++;
>  		ops->target.addr = strtoull(c, NULL, 16);
>  		if (!ops->target.addr) {
>  			c = strchr(c, ',');
>  			c = validate_comma(c, ops);
> -			if (c++ != NULL)
> +			if (c != NULL) {
> +				c++;
>  				ops->target.addr = strtoull(c, NULL, 16);
> +			}
>  		}
>  	} else {
>  		ops->target.addr = strtoull(ops->raw, NULL, 16);

It is undefined behavior, but works correctly with GCC and Clang. In
Gnulib, we allow it and suggest using -fno-sanitize=pointer-overflow
instead [1].

But I can understand that is not every projects preference. Therefore,
this change looks good to me.

Reviewed-by: Collin Funk <collin.funk1@gmail.com>

Collin

[1] https://www.gnu.org/software/gnulib/manual/html_node/Unsupported-Platforms.html