Skip to content

Commit 75545ff

Browse files
authored
ref qualify parser methods to avoid use of dangling objects (simdjson#703)
To avoid using data belonging to a temporary, the parse functions are ref qualified to get a compile error if used on an rvalue. See simdjson#696 Compilation tests are also added, to make sure bad usage fails to compile. Reviewed by jkeiser.
1 parent 3c6ef83 commit 75545ff

File tree

12 files changed

+1361
-15
lines changed

12 files changed

+1361
-15
lines changed

benchmark/parsingcompetition.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,14 @@ bool bench(const char *filename, bool verbose, bool just_data, int repeat_multip
108108
repeat, volume, !just_data);
109109
}
110110

111-
if (!just_data)
112-
BEST_TIME("simdjson (dynamic mem) ", simdjson::dom::parser().parse(p).error(), simdjson::SUCCESS,
111+
if (!just_data) {
112+
auto parse_dynamic=[](auto& str){
113+
simdjson::dom::parser parser;
114+
return parser.parse(str).error();
115+
};
116+
BEST_TIME("simdjson (dynamic mem) ", parse_dynamic(p), simdjson::SUCCESS,
113117
, repeat, volume, !just_data);
118+
}
114119
// (static alloc)
115120
simdjson::dom::parser parser;
116121
BEST_TIME("simdjson ", parser.parse(p).error(), simdjson::SUCCESS, , repeat, volume,

include/simdjson/document.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,8 @@ class parser {
680680
* - CAPACITY if the parser does not have enough capacity and len > max_capacity.
681681
* - other json errors if parsing fails.
682682
*/
683-
inline simdjson_result<element> load(const std::string &path) noexcept;
684-
683+
inline simdjson_result<element> load(const std::string &path) & noexcept;
684+
inline simdjson_result<element> load(const std::string &path) && = delete ;
685685
/**
686686
* Parse a JSON document and return a temporary reference to it.
687687
*
@@ -717,13 +717,17 @@ class parser {
717717
* - CAPACITY if the parser does not have enough capacity and len > max_capacity.
718718
* - other json errors if parsing fails.
719719
*/
720-
inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) noexcept;
720+
inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
721+
inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
721722
/** @overload parse(const uint8_t *buf, size_t len, bool realloc_if_needed) */
722-
really_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) noexcept;
723+
really_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
724+
really_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
723725
/** @overload parse(const uint8_t *buf, size_t len, bool realloc_if_needed) */
724-
really_inline simdjson_result<element> parse(const std::string &s) noexcept;
726+
really_inline simdjson_result<element> parse(const std::string &s) & noexcept;
727+
really_inline simdjson_result<element> parse(const std::string &s) && =delete;
725728
/** @overload parse(const uint8_t *buf, size_t len, bool realloc_if_needed) */
726-
really_inline simdjson_result<element> parse(const padded_string &s) noexcept;
729+
really_inline simdjson_result<element> parse(const padded_string &s) & noexcept;
730+
really_inline simdjson_result<element> parse(const padded_string &s) && =delete;
727731

728732
/** @private We do not want to allow implicit conversion from C string to std::string. */
729733
really_inline simdjson_result<element> parse(const char *buf) noexcept = delete;
@@ -1303,4 +1307,4 @@ struct simdjson_result<dom::object> : public internal::simdjson_result_base<dom:
13031307

13041308
} // namespace simdjson
13051309

1306-
#endif // SIMDJSON_DOCUMENT_H
1310+
#endif // SIMDJSON_DOCUMENT_H

include/simdjson/inline/document.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ inline simdjson_result<size_t> parser::read_file(const std::string &path) noexce
366366
return bytes_read;
367367
}
368368

369-
inline simdjson_result<element> parser::load(const std::string &path) noexcept {
369+
inline simdjson_result<element> parser::load(const std::string &path) & noexcept {
370370
size_t len;
371371
error_code code;
372372
read_file(path).tie(len, code);
@@ -382,7 +382,7 @@ inline document_stream parser::load_many(const std::string &path, size_t batch_s
382382
return document_stream(*this, (const uint8_t*)loaded_bytes.get(), len, batch_size, code);
383383
}
384384

385-
inline simdjson_result<element> parser::parse(const uint8_t *buf, size_t len, bool realloc_if_needed) noexcept {
385+
inline simdjson_result<element> parser::parse(const uint8_t *buf, size_t len, bool realloc_if_needed) & noexcept {
386386
error_code code = ensure_capacity(len);
387387
if (code) { return code; }
388388

@@ -405,13 +405,13 @@ inline simdjson_result<element> parser::parse(const uint8_t *buf, size_t len, bo
405405
error = UNINITIALIZED;
406406
return doc.root();
407407
}
408-
really_inline simdjson_result<element> parser::parse(const char *buf, size_t len, bool realloc_if_needed) noexcept {
408+
really_inline simdjson_result<element> parser::parse(const char *buf, size_t len, bool realloc_if_needed) & noexcept {
409409
return parse((const uint8_t *)buf, len, realloc_if_needed);
410410
}
411-
really_inline simdjson_result<element> parser::parse(const std::string &s) noexcept {
411+
really_inline simdjson_result<element> parser::parse(const std::string &s) & noexcept {
412412
return parse(s.data(), s.length(), s.capacity() - s.length() < SIMDJSON_PADDING);
413413
}
414-
really_inline simdjson_result<element> parser::parse(const padded_string &s) noexcept {
414+
really_inline simdjson_result<element> parser::parse(const padded_string &s) & noexcept {
415415
return parse(s.data(), s.length(), false);
416416
}
417417

0 commit comments

Comments
 (0)