Skip to content
Merged
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
10 changes: 9 additions & 1 deletion src/parser/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ inline std::optional<uint64_t> Lexer::takeHexnum(OverflowBehavior behavior) {
}

inline Lexer::Sign Lexer::takeSign() {
if (empty()) {
return NoSign;
}
auto c = peek();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we make peek return optional so that future callers know to check if they hit the end of the buffer?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, I did that in #8737 as a follow-up because the diff for that change swamps the diff for fixing these bugs.

if (c == '+') {
take(1);
Expand Down Expand Up @@ -812,7 +815,8 @@ inline std::optional<Lexer::LexedFloat> Lexer::takeFloat() {
// we need to strip any underscores since `std::strtod` does not understand
// them.
std::stringstream ss;
for (const char *curr = &buffer[startPos], *end = &buffer[pos]; curr != end;
for (const char *curr = buffer.data() + startPos, *end = buffer.data() + pos;
curr != end;
++curr) {
if (*curr != '_') {
ss << *curr;
Expand Down Expand Up @@ -853,6 +857,10 @@ inline std::optional<Lexer::StringOrView> Lexer::takeStr() {
// Escape sequences
ensureBuildingEscaped();
take(1);
if (empty()) {
pos = startPos;
return std::nullopt;
}
auto c = peek();
take(1);
switch (c) {
Expand Down
9 changes: 9 additions & 0 deletions test/gtest/wat-lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
using namespace wasm::WATParser;
using namespace std::string_view_literals;

TEST(LexerTest, EmptyInput) {
EXPECT_TRUE(Lexer(""sv).empty());
EXPECT_EQ(Lexer(""sv).takeI32(), std::nullopt);
EXPECT_EQ(Lexer(""sv).takeF32(), std::nullopt);
EXPECT_EQ(Lexer(""sv).takeString(), std::nullopt);
EXPECT_EQ(Lexer(""sv).takeID(), std::nullopt);
}

TEST(LexerTest, LexWhitespace) {
Lexer lexer(" 1\t2\n3\r4 \n\n\t 5 "sv);

Expand Down Expand Up @@ -915,6 +923,7 @@ TEST(LexerTest, LexString) {
"_$_\xC2\xA3_\xE2\x82\xAC_\xF0\x90\x8D\x88_"s);

EXPECT_FALSE(Lexer("\"unterminated"sv).takeString());
EXPECT_FALSE(Lexer("\"foo\\"sv).takeString());
EXPECT_FALSE(Lexer("\"unescaped nul\0\""sv).takeString());
EXPECT_FALSE(Lexer("\"unescaped U+19\x19\""sv).takeString());
EXPECT_FALSE(Lexer("\"unescaped U+7f\x7f\""sv).takeString());
Expand Down
Loading