Skip to content

Commit 5b2f61e

Browse files
authored
Report invalid literals when parsing spectest JSON files (#1288)
These were being silently ignored previously.
1 parent d7f7fd7 commit 5b2f61e

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

src/tools/spectest-interp.cc

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -501,60 +501,74 @@ wabt::Result JSONParser::ParseConst(TypedValue* out_value) {
501501

502502
if (type_str == "i32") {
503503
uint32_t value;
504-
CHECK_RESULT(
505-
ParseInt32(value_start, value_end, &value, ParseIntType::UnsignedOnly));
504+
if (Failed((ParseInt32(value_start, value_end, &value,
505+
ParseIntType::UnsignedOnly)))) {
506+
PrintError("invalid i32 literal");
507+
return wabt::Result::Error;
508+
}
506509
out_value->type = Type::I32;
507510
out_value->value.i32 = value;
508-
return wabt::Result::Ok;
509511
} else if (type_str == "f32") {
510512
uint32_t value_bits;
511-
CHECK_RESULT(ParseInt32(value_start, value_end, &value_bits,
512-
ParseIntType::UnsignedOnly));
513+
if (Failed(ParseInt32(value_start, value_end, &value_bits,
514+
ParseIntType::UnsignedOnly))) {
515+
PrintError("invalid f32 literal");
516+
return wabt::Result::Error;
517+
}
513518
out_value->type = Type::F32;
514519
out_value->value.f32_bits = value_bits;
515-
return wabt::Result::Ok;
516520
} else if (type_str == "i64") {
517521
uint64_t value;
518-
CHECK_RESULT(
519-
ParseInt64(value_start, value_end, &value, ParseIntType::UnsignedOnly));
522+
if (Failed(ParseInt64(value_start, value_end, &value,
523+
ParseIntType::UnsignedOnly))) {
524+
PrintError("invalid i64 literal");
525+
return wabt::Result::Error;
526+
}
520527
out_value->type = Type::I64;
521528
out_value->value.i64 = value;
522-
return wabt::Result::Ok;
523529
} else if (type_str == "f64") {
524530
uint64_t value_bits;
525-
CHECK_RESULT(ParseInt64(value_start, value_end, &value_bits,
526-
ParseIntType::UnsignedOnly));
531+
if (Failed((ParseInt64(value_start, value_end, &value_bits,
532+
ParseIntType::UnsignedOnly)))) {
533+
PrintError("invalid f64 literal");
534+
return wabt::Result::Error;
535+
}
527536
out_value->type = Type::F64;
528537
out_value->value.f64_bits = value_bits;
529-
return wabt::Result::Ok;
530538
} else if (type_str == "v128") {
531539
v128 value_bits;
532-
CHECK_RESULT(ParseUint128(value_start, value_end, &value_bits));
540+
if (Failed(ParseUint128(value_start, value_end, &value_bits))) {
541+
PrintError("invalid v128 literal");
542+
return wabt::Result::Error;
543+
}
533544
out_value->type = Type::V128;
534545
out_value->value.vec128 = value_bits;
535-
return wabt::Result::Ok;
536546
} else if (type_str == "nullref") {
537547
out_value->type = Type::Nullref;
538548
out_value->value.ref = {RefType::Null, 0};
539-
return wabt::Result::Ok;
540549
} else if (type_str == "hostref") {
541550
uint32_t value;
542-
CHECK_RESULT(
543-
ParseInt32(value_start, value_end, &value, ParseIntType::UnsignedOnly));
551+
if (Failed(ParseInt32(value_start, value_end, &value,
552+
ParseIntType::UnsignedOnly))) {
553+
PrintError("invalid hostref literal");
554+
return wabt::Result::Error;
555+
}
544556
out_value->type = Type::Hostref;
545557
out_value->value.ref = {RefType::Host, value};
546-
return wabt::Result::Ok;
547558
} else if (type_str == "funcref") {
548559
uint32_t value;
549-
CHECK_RESULT(
550-
ParseInt32(value_start, value_end, &value, ParseIntType::UnsignedOnly));
560+
if (Failed(ParseInt32(value_start, value_end, &value, ParseIntType::UnsignedOnly))) {
561+
PrintError("invalid funcref literal");
562+
return wabt::Result::Error;
563+
}
551564
out_value->type = Type::Funcref;
552565
out_value->value.ref = {RefType::Func, value};
553-
return wabt::Result::Ok;
554566
} else {
555567
PrintError("unknown concrete type: \"%s\"", type_str.c_str());
556568
return wabt::Result::Error;
557569
}
570+
571+
return wabt::Result::Ok;
558572
}
559573

560574
wabt::Result JSONParser::ParseConstVector(TypedValues* out_values) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
;;; RUN: %(spectest-interp)s %(in_file)s
2+
;;; ERROR: 1
3+
{
4+
"source_filename": "test.wast",
5+
"commands": [
6+
{"type": "module", "line": 1, "filename": "test.0.wasm"},
7+
{
8+
"type": "assert_return",
9+
"line": 3,
10+
"action": {"type": "invoke", "field": "f", "args": []},
11+
"expected": [{"type": "i32", "value": "xx"}]
12+
}
13+
]
14+
}
15+
(;; STDERR ;;;
16+
out/test/spectest-interp-invalid-literal.txt:11:50: invalid i32 literal
17+
;;; STDERR ;;)

0 commit comments

Comments
 (0)