Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Int value range is incorrect #862

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,18 @@ go_sentence

step_clause
: %empty { $$ = new StepClause(); }
| INTEGER KW_STEPS { $$ = new StepClause($1); }
| KW_UPTO INTEGER KW_STEPS { $$ = new StepClause($2, true); }
| INTEGER KW_STEPS {
if ((uint64_t)$1 == 9223372036854775808ULL) {
LOG(ERROR) << "Integer overflow";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to handle exceptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the exception?

}
$$ = new StepClause($1);
}
| KW_UPTO INTEGER KW_STEPS {
if ((uint64_t)$2 == 9223372036854775808ULL) {
LOG(ERROR) << "Integer overflow";
}
$$ = new StepClause($2, true);
}
;

from_clause
Expand Down Expand Up @@ -486,12 +496,18 @@ vid

unary_integer
: PLUS INTEGER {
if ((uint64_t)$2 == 9223372036854775808ULL) {
LOG(ERROR) << "Integer overflow";
}
$$ = $2;
}
| MINUS INTEGER {
$$ = -$2;
}
| INTEGER {
if ((uint64_t)$1 == 9223372036854775808ULL) {
LOG(ERROR) << "Integer overflow";
}
$$ = $1;
}
;
Expand Down Expand Up @@ -1269,7 +1285,14 @@ host_item
/* TODO(dutor) Support hostname and IPv6 */
;

port : INTEGER { $$ = $1; }
port
: INTEGER {
if ((uint64_t)$1 == 9223372036854775808ULL) {
LOG(ERROR) << "Integer overflow";
}
$$ = $1;
}
;

config_module_enum
: KW_GRAPH { $$ = ConfigModule::GRAPH; }
Expand Down Expand Up @@ -1339,11 +1362,17 @@ space_opt_list
}
;

space_opt_item
space_opt_item
: KW_PARTITION_NUM ASSIGN INTEGER {
if ((uint64_t)$3 == 9223372036854775808ULL) {
LOG(ERROR) << "Integer overflow";
}
$$ = new SpaceOptItem(SpaceOptItem::PARTITION_NUM, $3);
}
| KW_REPLICA_FACTOR ASSIGN INTEGER {
if ((uint64_t)$3 == 9223372036854775808ULL) {
LOG(ERROR) << "Integer overflow";
}
$$ = new SpaceOptItem(SpaceOptItem::REPLICA_FACTOR, $3);
}
// TODO(YT) Create Spaces for different engines
Expand Down
16 changes: 13 additions & 3 deletions src/parser/scanner.lex
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,11 @@ IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
yyterminate();
}
}
int64_t val = 0;
uint64_t val = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hex and octal do not need to be processed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? Don't they overflow?

sscanf(yytext, "%lx", &val);
if (val > 0x8000000000000000) {
yyterminate();
}
yylval->intval = val;
return TokenType::INTEGER;
}
Expand All @@ -302,15 +305,22 @@ IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
yyterminate();
}
}
int64_t val = 0;
uint64_t val = 0;
sscanf(yytext, "%lo", &val);
if (val > 01000000000000000000000) {
yyterminate();
}
yylval->intval = val;
return TokenType::INTEGER;
}
{DEC}+ {
try {
folly::StringPiece text(yytext, yyleng);
yylval->intval = folly::to<int64_t>(text);
uint64_t val = folly::to<uint64_t>(text);
if (val > 9223372036854775808ULL) {
yyterminate();
}
yylval->intval = val;
} catch (...) {
yyterminate();
}
Expand Down
11 changes: 6 additions & 5 deletions src/parser/test/ScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,13 @@ TEST(Scanner, Basic) {
CHECK_SEMANTIC_VALUE(".123", TokenType::DOUBLE, 0.123),
CHECK_SEMANTIC_VALUE("123.456", TokenType::DOUBLE, 123.456),

CHECK_SEMANTIC_VALUE("0xFFFFFFFFFFFFFFFF", TokenType::INTEGER, 0xFFFFFFFFFFFFFFFFL),
CHECK_SEMANTIC_VALUE("0x00FFFFFFFFFFFFFFFF", TokenType::INTEGER, 0x00FFFFFFFFFFFFFFFFL),
CHECK_SEMANTIC_VALUE("0x8000000000000000", TokenType::INTEGER, 0x8000000000000000),
CHECK_SEMANTIC_VALUE("0x008000000000000000", TokenType::INTEGER, 0x008000000000000000L),

CHECK_SEMANTIC_VALUE("9223372036854775807", TokenType::INTEGER, 9223372036854775807L),
CHECK_SEMANTIC_VALUE("001777777777777777777777", TokenType::INTEGER,
001777777777777777777777),
CHECK_LEXICAL_ERROR("9223372036854775808"),
CHECK_SEMANTIC_VALUE("001000000000000000000000", TokenType::INTEGER,
001000000000000000000000),
CHECK_LEXICAL_ERROR("9223372036854775809"),
CHECK_LEXICAL_ERROR("0xFFFFFFFFFFFFFFFFF"),
CHECK_LEXICAL_ERROR("002777777777777777777777"),
// TODO(dutor) It's too tedious to paste an overflowed double number here,
Expand Down