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
#769)

* [struct_pack][fix] fix mod zero error when struct only contain compatible

* fix test
  • Loading branch information
poor-circle authored Sep 11, 2024
1 parent 4c14c1f commit be8da74
Show file tree
Hide file tree
Showing 2 changed files with 14 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
14 changes: 13 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,15 @@ TEST_CASE("test nested trival_serialzable_obj_with_compatible") {
CHECK(result.has_value());
CHECK(test_equal(result.value(), a_v1));
}
}
}

struct only_compatible {
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 be8da74

Please sign in to comment.