mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-27 05:16:18 +08:00
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:
co-authored by
ActuallyaDeviloper
Dr. Patrick Urbanke
parent
36bc907d78
commit
8b2833b136
@@ -147,7 +147,8 @@ class RFL_API Writer {
|
||||
OutputVarType add_value_to_map(const std::string_view& _name, const T& _var,
|
||||
OutputMapType* _parent) const {
|
||||
avro_value_t new_value;
|
||||
int result = avro_value_add(&_parent->val_, _name.data(), &new_value,
|
||||
// Copy name to nullterminate.
|
||||
int result = avro_value_add(&_parent->val_, std::string(_name).c_str(), &new_value,
|
||||
nullptr, nullptr);
|
||||
if (result != 0) {
|
||||
throw std::runtime_error("Error adding value to map: error(" +
|
||||
@@ -163,7 +164,8 @@ class RFL_API Writer {
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const {
|
||||
avro_value_t new_value;
|
||||
int result = avro_value_get_by_name(&_parent->val_, _name.data(),
|
||||
// Copy name to nullterminate.
|
||||
int result = avro_value_get_by_name(&_parent->val_, std::string(_name).c_str(),
|
||||
&new_value, nullptr);
|
||||
if (result != 0) {
|
||||
throw std::runtime_error("Error adding value to object: error(" +
|
||||
|
||||
@@ -165,8 +165,9 @@ class RFL_API Reader {
|
||||
const auto entries = _map.val_.get("entries").as<capnp::DynamicList>();
|
||||
for (auto entry : entries) {
|
||||
auto s = entry.template as<capnp::DynamicStruct>();
|
||||
const char* key = s.get("key").as<capnp::Text>().cStr();
|
||||
_map_reader.read(std::string_view(key), InputVarType{s.get("value")});
|
||||
const auto key = s.get("key").as<capnp::Text>();
|
||||
_map_reader.read(std::string_view(key.cStr(), key.size()),
|
||||
InputVarType{s.get("value")});
|
||||
}
|
||||
return std::nullopt;
|
||||
} catch (std::exception& e) {
|
||||
|
||||
@@ -10,15 +10,11 @@
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
// #include "../Box.hpp"
|
||||
#include "../Bytestring.hpp"
|
||||
// #include "../Ref.hpp"
|
||||
// #include "../Result.hpp"
|
||||
#include "../Vectorstring.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/is_literal.hpp"
|
||||
// #include "../internal/ptr_cast.hpp"
|
||||
#include "../common.hpp"
|
||||
#include "../internal/is_literal.hpp"
|
||||
|
||||
namespace rfl::capnproto {
|
||||
|
||||
@@ -193,7 +189,7 @@ class RFL_API Writer {
|
||||
auto entries =
|
||||
OutputArrayType{_parent->val_.get("entries").as<capnp::DynamicList>()};
|
||||
auto new_entry = add_object_to_array(2, &entries);
|
||||
add_value_to_object("key", std::string(_name), &new_entry);
|
||||
add_value_to_object("key", _name, &new_entry);
|
||||
return add_value_to_object("value", _var, &new_entry);
|
||||
}
|
||||
|
||||
@@ -202,20 +198,22 @@ class RFL_API Writer {
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
_parent->val_.set(_name.data(), _var.c_str());
|
||||
_parent->val_.set(to_kj_string_ptr(_name), _var.c_str());
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
const auto array_ptr = kj::ArrayPtr<const kj::byte>(
|
||||
internal::ptr_cast<const unsigned char*>(_var.data()), _var.size());
|
||||
_parent->val_.set(_name.data(), capnp::Data::Reader(array_ptr));
|
||||
_parent->val_.set(to_kj_string_ptr(_name),
|
||||
capnp::Data::Reader(array_ptr));
|
||||
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
_parent->val_.set(_name.data(), _var);
|
||||
_parent->val_.set(to_kj_string_ptr(_name), _var);
|
||||
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
_parent->val_.set(_name.data(), static_cast<std::int64_t>(_var));
|
||||
_parent->val_.set(to_kj_string_ptr(_name),
|
||||
static_cast<std::int64_t>(_var));
|
||||
|
||||
} else if constexpr (internal::is_literal_v<T>) {
|
||||
return add_value_to_object(_name, _var.value(), _parent);
|
||||
@@ -262,6 +260,11 @@ class RFL_API Writer {
|
||||
|
||||
void end_object(OutputObjectType* /*_obj*/) const {}
|
||||
|
||||
private:
|
||||
kj::StringPtr to_kj_string_ptr(const std::string_view& _str) const {
|
||||
return kj::StringPtr(_str.data(), _str.size());
|
||||
}
|
||||
|
||||
private:
|
||||
capnp::DynamicStruct::Builder* root_;
|
||||
};
|
||||
|
||||
@@ -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_)) {
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -93,9 +93,9 @@ struct RFL_API Writer {
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
std::string to_string(const T& _val) const {
|
||||
decltype(auto) to_string(const T& _val) const {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return _val;
|
||||
return _val; // Return reference to avoid expensive string copy.
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return _val ? "true" : "false";
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
|
||||
@@ -77,7 +77,6 @@ class RFL_API Writer {
|
||||
|
||||
void end_object(OutputObjectType* _obj) const;
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string_view& _name,
|
||||
const T& _var) const {
|
||||
@@ -85,14 +84,14 @@ class RFL_API Writer {
|
||||
std::is_same<std::remove_cvref_t<T>, bool>() ||
|
||||
std::is_same<std::remove_cvref_t<T>,
|
||||
std::remove_cvref_t<decltype(YAML::Null)>>()) {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << _var;
|
||||
(*out_) << YAML::Key << std::string(_name) << YAML::Value << _var;
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
// std::to_string is necessary to ensure that floating point values are
|
||||
// always written as floats.
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value
|
||||
(*out_) << YAML::Key << std::string(_name) << YAML::Value
|
||||
<< std::to_string(_var);
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value
|
||||
(*out_) << YAML::Key << std::string(_name) << YAML::Value
|
||||
<< static_cast<int64_t>(_var);
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
|
||||
@@ -47,8 +47,9 @@ Writer::OutputArrayType Writer::add_array_to_map(const std::string_view& _name,
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const {
|
||||
return OutputArrayType{_parent->val_.init(_name.data(), SafeSizeToUInt(_size))
|
||||
.as<capnp::DynamicList>()};
|
||||
return OutputArrayType{
|
||||
_parent->val_.init(to_kj_string_ptr(_name), SafeSizeToUInt(_size))
|
||||
.as<capnp::DynamicList>()};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_union(
|
||||
@@ -111,7 +112,7 @@ Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t /*_size*/,
|
||||
OutputObjectType* _parent) const {
|
||||
return OutputObjectType{
|
||||
_parent->val_.get(_name.data()).as<capnp::DynamicStruct>()};
|
||||
_parent->val_.get(to_kj_string_ptr(_name)).as<capnp::DynamicStruct>()};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_union(
|
||||
@@ -142,7 +143,7 @@ Writer::OutputUnionType Writer::add_union_to_map(const std::string_view& _name,
|
||||
Writer::OutputUnionType Writer::add_union_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const {
|
||||
return OutputUnionType{
|
||||
_parent->val_.get(_name.data()).as<capnp::DynamicStruct>()};
|
||||
_parent->val_.get(to_kj_string_ptr(_name)).as<capnp::DynamicStruct>()};
|
||||
}
|
||||
|
||||
Writer::OutputUnionType Writer::add_union_to_union(
|
||||
@@ -171,7 +172,7 @@ Writer::OutputVarType Writer::add_null_to_map(const std::string_view& _name,
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const {
|
||||
_parent->val_.set(_name.data(), capnp::VOID);
|
||||
_parent->val_.set(to_kj_string_ptr(_name), capnp::VOID);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,8 @@ Writer::OutputVarType Writer::add_null_to_array(
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* /*_parent*/) const {
|
||||
fbb_->Null(_name.data());
|
||||
// Copy the string, because "std::string_view" might not be nullterminated.
|
||||
fbb_->Null(std::string(_name).c_str());
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
@@ -64,7 +65,8 @@ void Writer::end_object(OutputObjectType* _obj) const {
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array(const std::string_view& _name) const {
|
||||
const auto start = fbb_->StartVector(_name.data());
|
||||
// Copy the string, because "std::string_view" might not be nullterminated.
|
||||
const auto start = fbb_->StartVector(std::string(_name).c_str());
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
@@ -75,7 +77,8 @@ Writer::OutputArrayType Writer::new_array() const {
|
||||
|
||||
Writer::OutputObjectType Writer::new_object(
|
||||
const std::string_view& _name) const {
|
||||
const auto start = fbb_->StartMap(_name.data());
|
||||
// Copy the string, because "std::string_view" might not be nullterminated.
|
||||
const auto start = fbb_->StartMap(std::string(_name).c_str());
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
|
||||
@@ -39,27 +39,27 @@ Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(const size_t /*_size*/) const {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_));
|
||||
return OutputArrayType(root_name_, node_child);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(const size_t /*_size*/) const {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::value_as_root_impl(
|
||||
const std::string& _str) const {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str);
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ Writer::OutputVarType Writer::add_value_to_array_impl(
|
||||
const std::string& _str, OutputArrayType* _parent) const {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_parent->name_));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str);
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
@@ -86,15 +86,15 @@ Writer::OutputVarType Writer::add_value_to_object_impl(
|
||||
const std::string_view& _name, const std::string& _str,
|
||||
OutputObjectType* _parent, const bool _is_attribute) const {
|
||||
if (_is_attribute) {
|
||||
_parent->node_->append_attribute(_name) = _str.c_str();
|
||||
_parent->node_->append_attribute(_name) = _str;
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
_parent->node_->append_child(pugi::node_pcdata).set_value(_str.c_str());
|
||||
_parent->node_->append_child(pugi::node_pcdata).set_value(_str);
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str);
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ void Writer::end_object(OutputObjectType* /*_obj*/) const {
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array(const std::string_view& _name) const {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginSeq;
|
||||
(*out_) << YAML::Key << std::string(_name) << YAML::Value << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ Writer::OutputArrayType Writer::new_array() const {
|
||||
|
||||
Writer::OutputObjectType Writer::new_object(
|
||||
const std::string_view& _name) const {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginMap;
|
||||
(*out_) << YAML::Key << std::string(_name) << YAML::Value << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user