Skip to content

Commit

Permalink
Check exponent value can be expressed in uint64_t
Browse files Browse the repository at this point in the history
Undefined Sanitizer found the following error.

```
sudo ./src/bpftrace -e 'BEGIN {@ = 1e30;}'
/home/ubuntu/work/bpftrace/src/utils.cpp:809:10: runtime error: 1e+30 is outside the range of representable values of type 'unsigned long'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/ubuntu/work/bpftrace/src/utils.cpp:809:10 in
Attaching 1 probe...
^C

@: 0
```

To fix this, in the parse_exponent(), check the exponent value and if
it's bigger than uint64_t'x max value, throw exception. The Lexer then
make an error.

Now it becomes

```
% sudo ./src/bpftrace -e 'BEGIN {@ = 1e30;}'
stdin:1:12-16: ERROR: 1e30 is too big for uint64_t
BEGIN {@ = 1e30;}
           ~~~~
stdin:1:12-17: ERROR: syntax error, unexpected ;
BEGIN {@ = 1e30;}
           ~~~~~
```
  • Loading branch information
mmisono committed Nov 15, 2020
1 parent 3d2f737 commit 327076c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ bpftrace|perf { return Parser::make_STACK_MODE(yytext, loc); }
{call} { return Parser::make_CALL(yytext, loc); }
{call_and_builtin} { return Parser::make_CALL_BUILTIN(yytext, loc); }
{int} { return Parser::make_INT(strtoul(yytext, NULL, 0), loc); }
{exponent} { return Parser::make_INT(parse_exponent(yytext), loc); }
{exponent} {
uint64_t num;
try {
num = parse_exponent(yytext);
return Parser::make_INT(num, loc);
} catch (std::exception const &e) {
driver.error(loc, e.what());
}
}
{path} { return Parser::make_PATH(yytext, loc); }
{map} { return Parser::make_MAP(yytext, loc); }
{var} { return Parser::make_VAR(yytext, loc); }
Expand Down
4 changes: 4 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <fcntl.h>
#include <fstream>
#include <glob.h>
#include <limits>
#include <link.h>
#include <map>
#include <memory>
Expand Down Expand Up @@ -806,6 +807,9 @@ uint64_t parse_exponent(const char *str)

auto exp = strtoll(e_offset + 1, nullptr, 10);
auto num = base * std::pow(10, exp);
uint64_t max = std::numeric_limits<uint64_t>::max();
if (num > (double)max)
throw std::runtime_error(std::string(str) + " is too big for uint64_t");
return num;
}

Expand Down
1 change: 1 addition & 0 deletions tests/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ TEST(Parser, scientific_notation)
"Program\n kprobe:f\n call: print\n int: 5000000000\n");

test_parse_failure("k:f { print(5e-9); }");
test_parse_failure("k:f { print(1e100); }");
}

TEST(Parser, while_loop)
Expand Down

0 comments on commit 327076c

Please sign in to comment.