From bf2cda3585b27b96d0cc3bdd696bed0d3eb5b821 Mon Sep 17 00:00:00 2001 From: dv Date: Wed, 20 Dec 2023 14:00:17 +0000 Subject: [PATCH] btrace: add support for hex and octal values. Changes number tokenizing and parsing to support hex & octal values. Does not address other lexer issues (e.g. $0x1) to close gaps with bpftrace. OK claudio@ --- regress/usr.sbin/btrace/arithm.bt | 5 +++-- regress/usr.sbin/btrace/arithm.ok | 4 ++-- usr.sbin/btrace/bt_parse.y | 22 +++++++++++++--------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/regress/usr.sbin/btrace/arithm.bt b/regress/usr.sbin/btrace/arithm.bt index e2d35a83a1b..58638e7c93a 100644 --- a/regress/usr.sbin/btrace/arithm.bt +++ b/regress/usr.sbin/btrace/arithm.bt @@ -3,7 +3,7 @@ BEGIN @a = 10; @b = 5; - printf("a + b = %d\n", @a + @b); + printf("a + b + 0xf = %d\n", @a + @b + 0xf); } END @@ -11,5 +11,6 @@ END printf("a - b = %d\n", @a - @b); $c = @a + 2 * @b; - printf("c = %d, total = %d\n", $c, ($c - @b) / 5); + $d = @a + 0xf5; + printf("c = %d, d = 0x%x, total = %d\n", $c, $d, ($c - @b) / 5); } diff --git a/regress/usr.sbin/btrace/arithm.ok b/regress/usr.sbin/btrace/arithm.ok index 9575561d2be..0c05e397b1a 100644 --- a/regress/usr.sbin/btrace/arithm.ok +++ b/regress/usr.sbin/btrace/arithm.ok @@ -1,3 +1,3 @@ -a + b = 15 +a + b + 0xf = 30 a - b = 5 -c = 20, total = 3 +c = 20, d = 0xff, total = 3 diff --git a/usr.sbin/btrace/bt_parse.y b/usr.sbin/btrace/bt_parse.y index 0207fe904ea..1eb528013cd 100644 --- a/usr.sbin/btrace/bt_parse.y +++ b/usr.sbin/btrace/bt_parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: bt_parse.y,v 1.55 2023/12/20 01:38:46 dv Exp $ */ +/* $OpenBSD: bt_parse.y,v 1.56 2023/12/20 14:00:17 dv Exp $ */ /* * Copyright (c) 2019-2023 Martin Pieuchot @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -925,17 +926,20 @@ again: yyerror("line too long"); return ERROR; } - } while ((c = lgetc()) != EOF && isdigit(c)); + } while ((c = lgetc()) != EOF && + (isxdigit(c) || c == 'x' || c == 'X')); lungetc(); if (c == EOF || allowed_to_end_number(c)) { - const char *errstr = NULL; - *p = '\0'; - yylval.v.number = strtonum(buf, LONG_MIN, LONG_MAX, - &errstr); - if (errstr) { - yyerror("invalid number '%s' (%s)", buf, - errstr); + errno = 0; + yylval.v.number = strtol(buf, NULL, 0); + if (errno == ERANGE) { + /* + * Characters are already validated, so only + * check ERANGE. + */ + yyerror("%sflow", (yylval.v.number == LONG_MIN) + ? "under" : "over"); return ERROR; } return NUMBER; -- 2.20.1