Avoid assuming that "std::string_view" is nullterminated. Generally it is really not. (#538)

Co-authored-by: ActuallyaDeviloper <ActuallyaDeviloper@users.noreply.github.com>
Co-authored-by: Dr. Patrick Urbanke <patrick@getml.com>
This commit is contained in:
ActuallyaDeviloper
2025-12-06 18:53:45 +01:00
committed by GitHub
co-authored by ActuallyaDeviloper Dr. Patrick Urbanke
parent 36bc907d78
commit 8b2833b136
11 changed files with 52 additions and 43 deletions
+2 -2
View File
@@ -88,7 +88,7 @@ struct Reader {
yyjson_obj_iter_init(_obj.val_, &iter);
yyjson_val* key;
while ((key = yyjson_obj_iter_next(&iter))) {
const auto name = std::string_view(yyjson_get_str(key));
const auto name = std::string_view(yyjson_get_str(key), yyjson_get_len(key));
_object_reader.read(name, InputVarType(yyjson_obj_iter_get_val(key)));
}
return std::nullopt;
@@ -101,7 +101,7 @@ struct Reader {
if (r == NULL) {
return error("Could not cast to string.");
}
return std::string(r);
return std::string(r, yyjson_get_len(_var.val_));
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!yyjson_is_bool(_var.val_)) {
+2 -2
View File
@@ -96,7 +96,7 @@ class RFL_API Writer {
OutputObjectType* _parent) const {
const auto val = from_basic_type(_var);
const bool ok = yyjson_mut_obj_add(
_parent->val_, yyjson_mut_strcpy(doc(), _name.data()), val.val_);
_parent->val_, yyjson_mut_strncpy(doc(), _name.data(), _name.size()), val.val_);
if (!ok) {
throw std::runtime_error("Could not add field '" + std::string(_name) +
"' to object.");
@@ -117,7 +117,7 @@ class RFL_API Writer {
template <class T>
OutputVarType from_basic_type(const T& _var) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
return OutputVarType(yyjson_mut_strcpy(doc(), _var.c_str()));
return OutputVarType(yyjson_mut_strncpy(doc(), _var.c_str(), _var.size()));
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
return OutputVarType(yyjson_mut_bool(doc(), _var));