Skip to content

Commit

Permalink
[struct_pack][fix] fix mod zero error when struct only contain compat…
Browse files Browse the repository at this point in the history
…ible
  • Loading branch information
poor-circle committed Sep 11, 2024
1 parent 6bc63de commit 5f9c927
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/ylt/struct_pack/alignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ constexpr auto calculate_padding_size() {
std::array<std::size_t, struct_pack::members_count<T> + 1> padding_size{};
std::size_t offset = 0;
for_each<T, calculate_padding_size_impl>(offset, padding_size);
if (offset % align::alignment_v<T>) {
if (align::alignment_v < T >> 0 && offset % align::alignment_v<T>) {
padding_size[struct_pack::members_count<T>] =
align::alignment_v<T> - offset % align::alignment_v<T>;
}
Expand Down
15 changes: 14 additions & 1 deletion src/struct_pack/tests/test_compatible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "doctest.h"
#include "test_struct.hpp"
#include "ylt/struct_pack/compatible.hpp"
#include "ylt/struct_pack/endian_wrapper.hpp"

using namespace struct_pack;
Expand Down Expand Up @@ -1307,4 +1308,16 @@ TEST_CASE("test nested trival_serialzable_obj_with_compatible") {
CHECK(result.has_value());
CHECK(test_equal(result.value(), a_v1));
}
}
}

struct only_compatible {
int i;
struct_pack::compatible<int> hi;
};
TEST_CASE("test only_compatible") {
only_compatible o{{0}};
auto buffer = struct_pack::serialize(o);
auto result = struct_pack::deserialize<only_compatible>(buffer);
CHECK(result.has_value());
CHECK(result->hi == o.hi);
}

0 comments on commit 5f9c927

Please sign in to comment.