Skip to content
Merged
Prev Previous commit
Next Next commit
Add warning and test coverage
  • Loading branch information
kripken committed Feb 20, 2024
commit 332fb6176d5abc391451692eea0404fd013238ec
3 changes: 3 additions & 0 deletions src/support/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ std::ostream& printEscapedJSON(std::ostream& os, const std::string_view str) {
if ((u0 & 0xF0) == 0xE0) {
u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
} else {
if ((u0 & 0xF8) != 0xF0) {
std::cerr << "warning: Bad UTF-8 leading byte " << int(u0) << '\n';
}
Comment on lines +208 to +210
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to emit similar warnings in the 1-, 2-, and 3-byte cases as well

i++;
u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (str[i] & 63);
}
Expand Down
8 changes: 4 additions & 4 deletions test/lit/passes/string-lowering.wast
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(string.const "foo")
)
(drop
(string.const "needs\tescaping\00.")
(string.const "needs\tescaping\00.'#%\"- .\r\n\\.")
)
)
)
Expand All @@ -24,7 +24,7 @@
;;
;; RUN: wasm-opt %s --string-lowering -all -S -o - | filecheck %s
;;
;; CHECK: custom section "string.consts", size 38, contents: "[\"bar\",\"foo\",\"needs\\tescaping\\u0000.\"]"
;; CHECK: custom section "string.consts", size 53, contents: "[\"bar\",\"foo\",\"needs\\tescaping\\u0000.'#%\\\"- .\\r\\n\\\\.\"]"

;; The custom section should parse OK using JSON.parse from node.
;; (Note we run --remove-unused-module-elements to remove externref-using
Expand All @@ -33,6 +33,6 @@
;; RUN: wasm-opt %s --string-lowering --remove-unused-module-elements -all -o %t.wasm
;; RUN: node %S/string-lowering.js %t.wasm | filecheck %s --check-prefix=CHECK-JS
;;
;; CHECK-JS: string: ["bar","foo","needs\tescaping\u0000."]
;; CHECK-JS: JSON: [ 'bar', 'foo', 'needs\tescaping\u0000.' ]
;; CHECK-JS: string: ["bar","foo","needs\tescaping\u0000.'#%\"- .\r\n\\."]
;; CHECK-JS: JSON: [ 'bar', 'foo', `needs\tescaping\u0000.'#%"- .\r\n\\.` ]