Skip to content

Commit 1d8779f

Browse files
committed
[serde]Fix loading array field, check if field is empty before doing anything
Signed-off-by: Alexis Jeandet <[email protected]>
1 parent 7e9bacb commit 1d8779f

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

include/cpp_utils/serde/deserialization.hpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,25 @@ constexpr inline std::size_t load_field(const auto& parent_composite, auto& pars
111111
using array_field_t = std::decay_t<decltype(array_field)>;
112112
using field_t = typename array_field_t::value_type;
113113
const auto count = parent_composite.field_size(array_field);
114-
array_field.resize(count);
115-
if constexpr (std::is_compound_v<field_t>)
114+
if (count > 0)
116115
{
117-
for (std::size_t i = 0; i < count; ++i)
116+
array_field.resize(count);
117+
if constexpr (std::is_compound_v<field_t>)
118118
{
119-
offset = deserialize(
120-
array_field[i], std::forward<decltype(parsing_context)>(parsing_context), offset);
119+
for (std::size_t i = 0; i < count; ++i)
120+
{
121+
offset = deserialize(array_field[i],
122+
std::forward<decltype(parsing_context)>(parsing_context), offset);
123+
}
124+
return offset;
125+
}
126+
else
127+
{
128+
return details::_load_values_from_memory(details::_pointer_to_memory(parsing_context),
129+
offset, array_field.data(), count, parent_composite);
121130
}
122-
return offset;
123-
}
124-
else
125-
{
126-
return details::_load_values_from_memory(details::_pointer_to_memory(parsing_context),
127-
offset, array_field.data(), count, parent_composite);
128131
}
132+
return offset;
129133
}
130134

131135
template <typename field_t>
@@ -140,21 +144,25 @@ constexpr inline std::size_t load_field(const auto& parent_composite, auto& pars
140144
constexpr auto field_size = reflexion::field_size<field_t>();
141145
const auto count = (parsing_context.size() - offset) / field_size;
142146

143-
array_field.resize(count);
144-
if constexpr (reflexion::can_split_v<field_t>)
147+
if (count > 0)
145148
{
146-
for (std::size_t i = 0; i < count; ++i)
149+
array_field.resize(count);
150+
if constexpr (reflexion::can_split_v<field_t>)
147151
{
148-
offset = deserialize(
149-
array_field[i], std::forward<decltype(parsing_context)>(parsing_context), offset);
152+
for (std::size_t i = 0; i < count; ++i)
153+
{
154+
offset = deserialize(array_field[i],
155+
std::forward<decltype(parsing_context)>(parsing_context), offset);
156+
}
157+
return offset;
158+
}
159+
else
160+
{
161+
return details::_load_values_from_memory(details::_pointer_to_memory(parsing_context),
162+
offset, array_field.data(), count, parent_composite);
150163
}
151-
return offset;
152-
}
153-
else
154-
{
155-
return details::_load_values_from_memory(details::_pointer_to_memory(parsing_context),
156-
offset, array_field.data(), count, parent_composite);
157164
}
165+
return offset;
158166
}
159167

160168
template <typename field_t>

0 commit comments

Comments
 (0)