Skip to content

Commit

Permalink
Prohibit ASCII control characters from showing up in strings.
Browse files Browse the repository at this point in the history
These must be escaped properly, so we see them naked in a string, we throw.

We shouldn't need explicit checks for these characters outside the strings,
as we chomp specific whitespace and then check for specific characters,
so the presence of such control characters should error out anyway.
  • Loading branch information
LTLA committed Jan 7, 2024
1 parent d3a4861 commit 1d20108
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.14)

project(millijson
VERSION 1.0.0
VERSION 1.0.1
DESCRIPTION "Lightweight JSON library for C++"
LANGUAGES CXX)

Expand Down
5 changes: 5 additions & 0 deletions include/millijson/millijson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ std::string extract_string(Input& input) {
}
}
break;
case (char) 0: case (char) 1: case (char) 2: case (char) 3: case (char) 4: case (char) 5: case (char) 6: case (char) 7: case (char) 8: case (char) 9:
case (char)10: case (char)11: case (char)12: case (char)13: case (char)14: case (char)15: case (char)16: case (char)17: case (char)18: case (char)19:
case (char)20: case (char)21: case (char)22: case (char)23: case (char)24: case (char)25: case (char)26: case (char)27: case (char)28: case (char)29:
case (char)30: case (char)31:
throw std::runtime_error("string contains ASCII control character at position " + std::to_string(input.position() + 1));
default:
output += next;
break;
Expand Down
2 changes: 2 additions & 0 deletions tests/src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ TEST(JsonParsingTest, StringLoading) {
parse_raw_json_error(" \"asdasdaasd\\a", "unrecognized escape");
parse_raw_json_error(" \"asdas\\uasdasd", "invalid unicode");
parse_raw_json_error(" \"asdas\\u00", "unterminated string");
parse_raw_json_error(" \"0sdasd\nasdasd\"", "string contains ASCII control character at position 9");
parse_raw_json_error(" \"sdasd\tasdasd\"", "string contains ASCII control character at position 8");
}

TEST(JsonParsingTest, IntegerLoading) {
Expand Down

0 comments on commit 1d20108

Please sign in to comment.