Skip to content

Commit 74a471c

Browse files
authored
[DebugInfo] Add bounds check to source map VLQ decoder shift (#8331)
- `readBase64VLQ()` increments `shift` by 5 for each continuation digit with no upper bound. - After 7 continuation digits, `shift` reaches 35 and `digit << shift` on a `uint32_t` is undefined behavior (shifting by >= type width). - Added a bounds check after incrementing `shift`, throwing `MapParseException` for malformed VLQ values with too many continuation digits.
1 parent b92b68d commit 74a471c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/wasm/source-map.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ int32_t SourceMapReader::readBase64VLQ() {
196196
ch > '9' ? ch - 'g' : (ch >= '0' ? ch - '0' + 20 : (ch == '+' ? 30 : 31));
197197
value |= digit << shift;
198198
shift += 5;
199+
if (shift >= 32) {
200+
throw MapParseException("VLQ value too large");
201+
}
199202
}
200203
return value & 1 ? -int32_t(value >> 1) : int32_t(value >> 1);
201204
}

test/gtest/source-map.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ TEST_F(SourceMapTest, BadSourceMaps) {
129129
// Mapping strings are parsed incrementally, so errors don't show up until a
130130
// sufficiently far-advanced location is requested to reach the problem.
131131
EXPECT_THROW(reader->readDebugLocationAt(1), MapParseException);
132+
133+
// VLQ with too many continuation digits. 7 continuation characters ('g')
134+
// push the shift to 35, exceeding the uint32_t width. This is a malformed
135+
// VLQ that should be rejected rather than causing undefined behavior.
136+
sourceMap = R"(
137+
{
138+
"version": 3,
139+
"sources": ["foo.c"],
140+
"mappings": "gggggggA"
141+
}
142+
)";
143+
ExpectParseError(sourceMap, "VLQ value too large");
132144
}
133145

134146
TEST_F(SourceMapTest, SourcesAndNames) {

0 commit comments

Comments
 (0)