Merge pull request #3207 from nakengelhardt/json_escape_quotes

fix handling of escaped chars in json backend and frontend (mostly)
This commit is contained in:
Miodrag Milanović 2022-03-04 13:57:32 +01:00 committed by GitHub
commit c3124023e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 5 deletions

View File

@ -916,7 +916,7 @@ clean:
rm -rf tests/simple/*.out tests/simple/*.log
rm -rf tests/memories/*.out tests/memories/*.log tests/memories/*.dmp
rm -rf tests/sat/*.log tests/techmap/*.log tests/various/*.log
rm -rf tests/bram/temp tests/fsm/temp tests/realmath/temp tests/share/temp tests/smv/temp
rm -rf tests/bram/temp tests/fsm/temp tests/realmath/temp tests/share/temp tests/smv/temp tests/various/temp
rm -rf vloghtb/Makefile vloghtb/refdat vloghtb/rtl vloghtb/scripts vloghtb/spec vloghtb/check_yosys vloghtb/vloghammer_tb.tar.bz2 vloghtb/temp vloghtb/log_test_*
rm -f tests/svinterfaces/*.log_stdout tests/svinterfaces/*.log_stderr tests/svinterfaces/dut_result.txt tests/svinterfaces/reference_result.txt tests/svinterfaces/a.out tests/svinterfaces/*_syn.v tests/svinterfaces/*.diff
rm -f tests/tools/cmp_tbdata

View File

@ -52,8 +52,23 @@ struct JsonWriter
string newstr = "\"";
for (char c : str) {
if (c == '\\')
newstr += "\\\\";
else if (c == '"')
newstr += "\\\"";
else if (c == '\b')
newstr += "\\b";
else if (c == '\f')
newstr += "\\f";
else if (c == '\n')
newstr += "\\n";
else if (c == '\r')
newstr += "\\r";
else if (c == '\t')
newstr += "\\t";
else if (c < 0x20)
newstr += stringf("\\u%04X", c);
else
newstr += c;
newstr += c;
}
return newstr + "\"";
}

View File

@ -60,10 +60,38 @@ struct JsonNode
break;
if (ch == '\\') {
int ch = f.get();
ch = f.get();
if (ch == EOF)
log_error("Unexpected EOF in JSON string.\n");
switch (ch) {
case EOF: log_error("Unexpected EOF in JSON string.\n"); break;
case '"':
case '/':
case '\\': break;
case 'b': ch = '\b'; break;
case 'f': ch = '\f'; break;
case 'n': ch = '\n'; break;
case 'r': ch = '\r'; break;
case 't': ch = '\t'; break;
case 'u':
int val = 0;
for (int i = 0; i < 4; i++) {
ch = f.get();
val <<= 4;
if (ch >= '0' && '9' >= ch) {
val += ch - '0';
} else if (ch >= 'A' && 'F' >= ch) {
val += 10 + ch - 'A';
} else if (ch >= 'a' && 'f' >= ch) {
val += 10 + ch - 'a';
} else
log_error("Unexpected non-digit character in \\uXXXX sequence: %c.\n", ch);
}
if (val < 128)
ch = val;
else
log_error("Unsupported \\uXXXX sequence in JSON string: %04X.\n", val);
break;
}
}
data_string += ch;

View File

@ -5,3 +5,4 @@
/run-test.mk
/plugin.so
/plugin.so.dSYM
/temp

View File

@ -0,0 +1,14 @@
! mkdir -p temp
read_verilog <<EOT
(* src = "\042 \057 \134 \010 \014 \012 \015 \011 \025 \033" *)
module foo;
endmodule
EOT
write_json temp/test_escapes.json
design -reset
read_json temp/test_escapes.json
write_json temp/test_escapes.json
design -reset
read_json temp/test_escapes.json
write_rtlil temp/test_escapes.json.il
! grep -F 'attribute \src "\" / \\ \010 \014 \n \015 \t \025 \033"' temp/test_escapes.json.il