Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing issue 2120 #2121

Merged
merged 1 commit into from Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/simdjson/padded_string_view-inl.h
Expand Up @@ -11,6 +11,7 @@ namespace simdjson {
inline padded_string_view::padded_string_view(const char* s, size_t len, size_t capacity) noexcept
: std::string_view(s, len), _capacity(capacity)
{
if(_capacity < len) { _capacity = len; }
}

inline padded_string_view::padded_string_view(const uint8_t* s, size_t len, size_t capacity) noexcept
Expand All @@ -26,6 +27,7 @@ inline padded_string_view::padded_string_view(const std::string &s) noexcept
inline padded_string_view::padded_string_view(std::string_view s, size_t capacity) noexcept
: std::string_view(s), _capacity(capacity)
{
if(_capacity < s.length()) { _capacity = s.length(); }
}

inline size_t padded_string_view::capacity() const noexcept { return _capacity; }
Expand Down
6 changes: 4 additions & 2 deletions include/simdjson/padded_string_view.h
Expand Up @@ -28,7 +28,8 @@ class padded_string_view : public std::string_view {
*
* @param s The string.
* @param len The length of the string (not including padding).
* @param capacity The allocated length of the string, including padding.
* @param capacity The allocated length of the string, including padding. If the capacity is less
* than the length, the capacity will be set to the length.
*/
explicit inline padded_string_view(const char* s, size_t len, size_t capacity) noexcept;
/** overload explicit inline padded_string_view(const char* s, size_t len) noexcept */
Expand All @@ -47,7 +48,8 @@ class padded_string_view : public std::string_view {
* Promise the given string_view has at least SIMDJSON_PADDING extra bytes allocated to it.
*
* @param s The string.
* @param capacity The allocated length of the string, including padding.
* @param capacity The allocated length of the string, including padding. If the capacity is less
* than the length, the capacity will be set to the length.
*/
explicit inline padded_string_view(std::string_view s, size_t capacity) noexcept;

Expand Down
18 changes: 10 additions & 8 deletions tests/dom/CMakeLists.txt
Expand Up @@ -78,14 +78,16 @@ if (BASH AND (NOT WIN32) AND SIMDJSON_BASH AND (TARGET json2json)) # The scripts
$<TARGET_FILE:checkimplementation>
)
endif()
add_test(
NAME simdjson_force_implementation_error
COMMAND
${CMAKE_COMMAND} -E env
SIMDJSON_FORCE_IMPLEMENTATION=doesnotexist
$<TARGET_FILE:json2json> ${EXAMPLE_JSON}
)
set_tests_properties(simdjson_force_implementation_error PROPERTIES WILL_FAIL TRUE)
if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL amd64)
add_test(
NAME simdjson_force_implementation_error
COMMAND
${CMAKE_COMMAND} -E env
SIMDJSON_FORCE_IMPLEMENTATION=doesnotexist
$<TARGET_FILE:json2json> ${EXAMPLE_JSON}
)
set_tests_properties(simdjson_force_implementation_error PROPERTIES WILL_FAIL TRUE)
endif()
endif()

#
Expand Down
10 changes: 10 additions & 0 deletions tests/ondemand/ondemand_error_tests.cpp
Expand Up @@ -6,6 +6,15 @@ using namespace simdjson;
namespace error_tests {
using namespace std;

bool issue2120() {
TEST_START();
ondemand::parser parser;
size_t bad_capacity = 0;
ondemand::document doc;
ASSERT_ERROR( parser.iterate("{\"a\": 0 }", bad_capacity).get(doc), INSUFFICIENT_PADDING);
TEST_SUCCEED();
}

bool badbadjson() {
TEST_START();
ondemand::parser parser;
Expand Down Expand Up @@ -366,6 +375,7 @@ namespace error_tests {

bool run() {
return
issue2120() &&
badbadjson() &&
badbadjson2() &&
issue1834() &&
Expand Down