diff --git a/include/osmium/builder/osm_object_builder.hpp b/include/osmium/builder/osm_object_builder.hpp index 48f96c86..1be0d888 100644 --- a/include/osmium/builder/osm_object_builder.hpp +++ b/include/osmium/builder/osm_object_builder.hpp @@ -126,8 +126,13 @@ namespace osmium { if (value_length > osmium::max_osm_string_length) { throw std::length_error{"OSM tag value is too long"}; } - add_size(append_with_zero(key, static_cast(key_length))); - add_size(append_with_zero(value, static_cast(value_length))); + const auto size = static_cast(key_length + value_length + 2); + unsigned char* target = reserve_space(size); + target = std::copy_n(reinterpret_cast(key), key_length, target); + *target++ = '\0'; + target = std::copy_n(reinterpret_cast(value), value_length, target); + *target = '\0'; + add_size(size); } /** @@ -154,14 +159,7 @@ namespace osmium { * @param value Tag value. */ void add_tag(const std::string_view& key, const std::string_view& value) { - if (key.size() > osmium::max_osm_string_length) { - throw std::length_error{"OSM tag key is too long"}; - } - if (value.size() > osmium::max_osm_string_length) { - throw std::length_error{"OSM tag value is too long"}; - } - add_size(append_with_zero(key.data(), static_cast(key.size()))); - add_size(append_with_zero(value.data(), static_cast(value.size()))); + add_tag(key.data(), key.size(), value.data(), value.size()); } /** @@ -495,6 +493,9 @@ namespace osmium { const auto size_of_object = sizeof(T) + sizeof(string_size_type); assert(cobject().user_size() == 1 && (size() <= size_of_object + osmium::memory::padded_length(1)) && "set_user() must be called at most once and before any sub-builders"); + if (length == 0) { + return static_cast(*this); + } constexpr const auto available_space = min_size_for_user - sizeof(string_size_type) - 1; if (length > available_space) { const auto space_needed = osmium::memory::padded_length(length - available_space); diff --git a/include/osmium/io/detail/pbf_decoder.hpp b/include/osmium/io/detail/pbf_decoder.hpp index 3690813e..7f28c2b0 100644 --- a/include/osmium/io/detail/pbf_decoder.hpp +++ b/include/osmium/io/detail/pbf_decoder.hpp @@ -614,18 +614,26 @@ namespace osmium { } void build_tag_list_from_dense_nodes(osmium::builder::NodeBuilder& builder, values_access_int32& tags) { + auto idx = tags.next_int32(); + if (idx == 0) { + return; + } + osmium::builder::TagListBuilder tl_builder{builder}; - while (!tags.empty()) { - const auto idx = tags.next_int32(); - if (idx == 0) { - return; - } + while (true) { const auto& k = m_stringtable.at(idx); if (tags.empty()) { throw osmium::pbf_error{"PBF format error"}; // this is against the spec, keys/vals must come in pairs } const auto& v = m_stringtable.at(tags.next_int32()); tl_builder.add_tag(k.first, k.second, v.first, v.second); + if (tags.empty()) { + return; + } + idx = tags.next_int32(); + if (idx == 0) { + return; + } } }