Skip to content

Commit e63c4a7

Browse files
authored
More misc ASAN fixes (#1882)
* fix buffer overflow in simple_ast.h printing. * check wasm binary format reading of function export indexes for errors. * check if s-expr format imports have a non-empty module and base. Fixes #1876 Fixes #1877 Fixes #1879
1 parent 0e5e550 commit e63c4a7

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

src/emscripten-optimizer/istring.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ struct IString {
149149
bool startsWith(const char *prefix) const {
150150
return stripPrefix(prefix) != nullptr;
151151
}
152+
153+
size_t size() const {
154+
return str ? strlen(str) : 0;
155+
}
152156
};
153157

154158
} // namespace cashew

src/emscripten-optimizer/simple_ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ struct JSPrinter {
561561

562562
void printAst() {
563563
print(ast);
564+
ensure(1);
564565
buffer[used] = 0;
565566
}
566567

src/wasm/wasm-binary.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,9 @@ void WasmBinaryBuilder::processFunctions() {
15051505
auto index = exportIndexes[curr];
15061506
switch (curr->kind) {
15071507
case ExternalKind::Function: {
1508+
if (index >= wasm.functions.size()) {
1509+
throwError("bad function export index");
1510+
}
15081511
curr->value = getFunctionIndexName(index);
15091512
break;
15101513
}

src/wasm/wasm-s-parser.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ void SExpressionWasmBuilder::parseFunction(Element& s, bool preParseImport) {
580580
}
581581
if (importModule.is()) {
582582
// this is an import, actually
583+
if (!importBase.size()) throw ParseException("module but no base for import");
583584
if (!preParseImport) throw ParseException("!preParseImport in func");
584585
auto im = make_unique<Function>();
585586
im->name = name;
@@ -1571,6 +1572,7 @@ void SExpressionWasmBuilder::parseImport(Element& s) {
15711572
auto module = s[i++]->str();
15721573
if (!s[i]->isStr()) throw ParseException("no name for import");
15731574
auto base = s[i++]->str();
1575+
if (!module.size() || !base.size()) throw ParseException("imports must have module and base");
15741576
// parse internals
15751577
Element& inner = newStyle ? *s[3] : s;
15761578
Index j = newStyle ? newStyleInner : i;
@@ -1694,6 +1696,7 @@ void SExpressionWasmBuilder::parseGlobal(Element& s, bool preParseImport) {
16941696
}
16951697
if (importModule.is()) {
16961698
// this is an import, actually
1699+
if (!importBase.size()) throw ParseException("module but no base for import");
16971700
if (!preParseImport) throw ParseException("!preParseImport in global");
16981701
auto im = make_unique<Global>();
16991702
im->name = global->name;

0 commit comments

Comments
 (0)