Skip to content

Commit

Permalink
fix(fe): fix out-of-bounds read on AArch64 when checking keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Dec 29, 2023
1 parent 540f7dd commit 67ed86c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/quick-lint-js/fe/keyword-lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <quick-lint-js/fe/keyword-lexer.h>
#include <quick-lint-js/port/char8.h>
#include <quick-lint-js/port/have.h>
#include <quick-lint-js/port/math.h>
#include <quick-lint-js/port/simd.h>

namespace quick_lint_js {
Expand Down Expand Up @@ -37,7 +38,7 @@ bool Keyword_Lexer::key_strings_equal(const Char8* a, const Char8* b,
#else
// TODO(strager): Optimize ARM NEON.
// TODO(strager): Optimize WebAssembly SIMD128.
return std::memcmp(a, b, size) == 0;
return std::memcmp(a, b, minimum(maximum_key_length, size)) == 0;
#endif
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/quick-lint-js/port/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ template <class T, class U>
constexpr auto maximum(T x, U y) {
return x < y ? y : x;
}

template <class T, class U>
constexpr auto minimum(T x, U y) {
return x < y ? x : y;
}
}

// quick-lint-js finds bugs in JavaScript programs.
Expand Down
4 changes: 4 additions & 0 deletions test/test-lex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,10 @@ TEST_F(Test_Lex, lex_identifiers) {
this->check_single_token(u8"ident$with$dollars"_sv,
u8"ident$with$dollars"_sv);
this->check_single_token(u8"digits0123456789"_sv, u8"digits0123456789"_sv);

// This identifier used to read the keyword table out of bounds.
this->check_single_token(u8"kedhinkunnunnnunuwnunununnun"_sv,
u8"kedhinkunnunnnunuwnunununnun"_sv);
}

TEST_F(Test_Lex, ascii_identifier_with_escape_sequence) {
Expand Down

0 comments on commit 67ed86c

Please sign in to comment.