Commit 6f31bc9c authored by jpp's avatar jpp Committed by Commit bot

[wasm] ASM-2-WASM. Enforces switch default clause appearing last.

BUG= https://bugs.chromium.org/p/chromium/issues/detail?id=628803
BUG= https://bugs.chromium.org/p/v8/issues/detail?id=4203
TEST= cctest/asmjs/test-asm-typer.cc
LOG= N

Review-Url: https://codereview.chromium.org/2172603002
Cr-Commit-Position: refs/heads/master@{#37954}
parent fb2feee6
......@@ -1347,17 +1347,23 @@ AsmType* AsmTyper::ValidateSwitchStatement(SwitchStatement* stmt) {
FAIL(stmt, "Switch tag must be signed.");
}
bool has_default = false;
int default_pos = kNoSourcePosition;
int last_case_pos = kNoSourcePosition;
ZoneSet<int32_t> cases_seen(zone_);
for (auto* a_case : *stmt->cases()) {
if (a_case->is_default()) {
CHECK(!has_default);
CHECK(default_pos == kNoSourcePosition);
RECURSE(ValidateDefault(a_case));
has_default = true;
default_pos = a_case->position();
continue;
}
if (last_case_pos == kNoSourcePosition) {
last_case_pos = a_case->position();
} else {
last_case_pos = std::max(last_case_pos, a_case->position());
}
int32_t case_lbl;
RECURSE(ValidateCase(a_case, &case_lbl));
auto case_lbl_pos = cases_seen.find(case_lbl);
......@@ -1375,6 +1381,11 @@ AsmType* AsmTyper::ValidateSwitchStatement(SwitchStatement* stmt) {
}
}
if (last_case_pos != kNoSourcePosition && default_pos != kNoSourcePosition &&
default_pos < last_case_pos) {
FAIL(stmt, "Switch default must appear last.");
}
return AsmType::Void();
}
......
......@@ -859,6 +859,8 @@ TEST(ErrorsInStatement) {
{"do {} while (fround(1));", "Do {} While condition must be type int"},
{"for (;fround(1););", "For condition must be type int"},
{"switch(flocal){ case 0: return 0; }", "Switch tag must be signed"},
{"switch(slocal){ default: case 0: return 0; }",
"Switch default must appear last"},
{"switch(slocal){ case 1: case 1: return 0; }", "Duplicated case label"},
{"switch(slocal){ case 1: case 0: break; case 1: return 0; }",
"Duplicated case label"},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment