Disallow parser.iterate("1"_padded), as it won't work

This commit is contained in:
John Keiser 2021-01-01 19:06:08 -08:00
parent 17f4f82827
commit 0039c5b981
5 changed files with 46 additions and 0 deletions

View File

@ -28,6 +28,13 @@ simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::it
return document::start(this);
}
simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(const simdjson_result<padded_string> &result) & noexcept {
// We don't presently have a way to temporarily get a const T& from a simdjson_result<T> without throwing an exception
SIMDJSON_TRY( result.error() );
const padded_string &buf = result.first;
return iterate(buf);
}
simdjson_warn_unused simdjson_really_inline simdjson_result<json_iterator> parser::iterate_raw(const padded_string &buf) & noexcept {
// Allocate if needed
if (_capacity < buf.size()) {

View File

@ -63,6 +63,9 @@ public:
* - UNCLOSED_STRING if there is an unclosed string in the document.
*/
simdjson_warn_unused simdjson_result<document> iterate(const padded_string &json) & noexcept;
simdjson_warn_unused simdjson_result<document> iterate(const simdjson_result<padded_string> &json) & noexcept;
simdjson_warn_unused simdjson_result<document> iterate(padded_string &&json) & noexcept = delete;
simdjson_warn_unused simdjson_result<document> iterate(const std::string_view &json) & noexcept = delete;
simdjson_warn_unused simdjson_result<document> iterate(const std::string &json) & noexcept = delete;
/**
* @private

View File

@ -1,6 +1,7 @@
# All remaining tests link with simdjson proper
link_libraries(simdjson)
include_directories(..)
add_subdirectory(compilation_failure_tests)
add_cpp_test(ondemand_active_tests LABELS ondemand acceptance per_implementation)
add_cpp_test(ondemand_compilation_tests LABELS ondemand acceptance per_implementation)

View File

@ -0,0 +1,17 @@
#
# This directory contains files aimed to verify that constructs that
# are supposed to fail at compile time, indeed do so.
# To prevent bit rot, the same source file is compiled twice with
# the macro COMPILATION_TEST_USE_FAILING_CODE set to 0 or 1.
#
# adds a compilation test. Two targets are created, one expected to
# succeed compilation and one that is expected to fail.
function(add_dual_compile_test TEST_NAME)
add_cpp_test(${TEST_NAME}_should_compile SOURCES ${TEST_NAME}.cpp COMPILE_ONLY LABELS no_mingw)
add_cpp_test(${TEST_NAME}_should_not_compile SOURCES ${TEST_NAME}.cpp COMPILE_ONLY WILL_FAIL LABELS acceptance no_mingw)
target_compile_definitions(${TEST_NAME}_should_not_compile PRIVATE COMPILATION_TEST_USE_FAILING_CODE=1)
endfunction(add_dual_compile_test)
add_dual_compile_test(iterate_temporary_buffer)

View File

@ -0,0 +1,18 @@
#include <iostream>
#include "simdjson.h"
using namespace simdjson::builtin;
int main() {
ondemand::parser parser;
#if COMPILATION_TEST_USE_FAILING_CODE
auto doc = parser.iterate("1"_padded);
#else
auto json = "1"_padded;
auto doc = parser.iterate(json);
#endif
int64_t value;
auto error = doc.get(value);
if (error) { exit(1); }
std::cout << value << std::endl;
}