Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix std::array<T,0> #848

Merged
merged 3 commits into from
Jan 20, 2025
Merged

Fix std::array<T,0> #848

merged 3 commits into from
Jan 20, 2025

Conversation

PeizeLin
Copy link
Contributor

@PeizeLin PeizeLin commented Dec 23, 2024

For std::array<T,0>, the sizeof(array)!=0.
Then bugs appear at
https://github.com/USCiLab/cereal/blob/master/include/cereal/types/array.hpp#L45
and
https://github.com/USCiLab/cereal/blob/master/include/cereal/types/array.hpp#L55 ,
since array.data() is nullptr for std::array<T,0>.

@AzothAmmo
Copy link
Contributor

Do you mind adding a test case to https://github.com/USCiLab/cereal/blob/master/unittests/array.hpp?

@PeizeLin
Copy link
Contributor Author

Do you mind adding a test case to https://github.com/USCiLab/cereal/blob/master/unittests/array.hpp?

OK, I've added the test cases.

@sjrdc
Copy link
Contributor

sjrdc commented Jan 2, 2025

I would expect that the same kind of error occurs for valarray of size 0, vector of size 0, etc. Shouldn't you fix this in the handling of binary_data created of nullptr and size 0 by the archive class, rather than the specific type(s)? That way we have a generic solution for the situation.

@PeizeLin
Copy link
Contributor Author

PeizeLin commented Jan 2, 2025

I would expect that the same kind of error occurs for valarray of size 0, vector of size 0, etc. Shouldn't you fix this in the handling of binary_data created of nullptr and size 0 by the archive class, rather than the specific type(s)? That way we have a generic solution for the situation.

I've checked that vector and valarray will not occur error.
It can be seen from
https://github.com/USCiLab/cereal/blob/master/include/cereal/types/vector.hpp#L45
https://github.com/USCiLab/cereal/blob/master/include/cereal/types/valarray.hpp#L47
The binary_data size is vector.size(), which is indeed 0.

But, perhaps we can change
ar( binary_data( array.data(), sizeof(array) ) );
to
ar( binary_data( array.data(), N*sizeof(T) ) );
@AzothAmmo Maybe this is a more generic solution?

@sjrdc
Copy link
Contributor

sjrdc commented Jan 2, 2025

Great suggestion, or use array.size()

@PeizeLin
Copy link
Contributor Author

PeizeLin commented Jan 5, 2025

Great suggestion, or use array.size()

updated

@AzothAmmo AzothAmmo added the bug label Jan 20, 2025
@AzothAmmo AzothAmmo added this to the v1.3.3 milestone Jan 20, 2025
@AzothAmmo AzothAmmo merged commit a56bad8 into USCiLab:master Jan 20, 2025
24 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants