From 965d834d229b9fe3804e165dbecb16dbeb954c57 Mon Sep 17 00:00:00 2001 From: ala89 Date: Fri, 15 Dec 2023 14:46:24 +0100 Subject: [PATCH] Fix tokenizer int overflow --- src/include/types.h | 1 + src/tokenize.cpp | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/include/types.h b/src/include/types.h index a058a72..6a12e8c 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -197,6 +197,7 @@ enum class ErrorType { // Lexing UnknownToken, + IntegerTooLarge, // Parsing EmptyInput, diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 756703c..e3588fa 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -221,9 +221,15 @@ vector tokenize(vector input, int initial_line) { } if (regex_search(str, m, INT_REGEX, regex_constants::match_continuous)) { + int val; + try { val = stoi(m.str()); } + catch (const out_of_range& e) { + throw SyntaxError(ErrorType::IntegerTooLarge, pos); + } + Token token = { .type = TokenType::Litteral, - .data = stoi(m.str()), + .data = val, .pos = pos }; tokens.emplace_back(token);