Skip to content

Commit

Permalink
zstring: fix encode rountrip for '\' as printable ASCII (Z3Prover#5120)
Browse files Browse the repository at this point in the history
This fixes encode roundtripping for all printable ASCII characters.
In particular, this now leaves a plain '\' untouched by the
encoding logic, instead of converting it to escaped hex-digits.
It also adds unit testing covering this specific zstring encoding
property, in order to avoid future regressions.
  • Loading branch information
lucab authored Mar 23, 2021
1 parent 119c5a9 commit b918f12
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ add_executable(test-z3
vector.cpp
lp/lp.cpp
lp/nla_solver_test.cpp
zstring.cpp
${z3_test_extra_object_files}
)
z3_add_install_tactic_rule(${z3_test_deps})
Expand Down
1 change: 1 addition & 0 deletions src/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ int main(int argc, char ** argv) {
TST(prime_generator);
TST(permutation);
TST(nlsat);
TST(zstring);
if (test_all) return 0;
TST(ext_numeral);
TST(interval);
Expand Down
25 changes: 25 additions & 0 deletions src/test/zstring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "util/debug.h"
#include "util/trace.h"
#include "util/zstring.h"

// Encode and check for roundtrip all printable ASCII characters.
static void tst_ascii_roundtrip() {
unsigned ascii_min = 0x20; // ' '
unsigned ascii_max = 0x7E; // '~'

for (unsigned i = ascii_min; i <= ascii_max; i++) {
zstring input(i);
std::string expected(1, i);
bool roundtrip_ok = input.encode() == expected;

if (!roundtrip_ok) {
std::cout << "Failed to roundtrip printable ASCII char: " << expected
<< "\n" << std::flush;
ENSURE(roundtrip_ok);
}
}
}

void tst_zstring() {
tst_ascii_roundtrip();
}
2 changes: 1 addition & 1 deletion src/util/zstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ std::string zstring::encode() const {
#define _flush() if (offset > 0) { buffer[offset] = 0; strm << buffer; offset = 0; }
for (unsigned i = 0; i < m_buffer.size(); ++i) {
unsigned ch = m_buffer[i];
if (ch < 32 || ch >= 128 || ch == '\\') {
if (ch < 32 || ch >= 128) {
_flush();
strm << "\\u{" << std::hex << ch << std::dec << "}";
}
Expand Down

0 comments on commit b918f12

Please sign in to comment.