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

cleanup: Use hex constants for i8/i16/i32/i64 limits. #79

Merged
merged 1 commit into from
Apr 14, 2024

Conversation

iphydf
Copy link
Contributor

@iphydf iphydf commented Apr 5, 2024

The rest of the code uses mostly hex constants, which are much less error-prone than a complicated number like 9223372036834775807 (in which I made a mistake, but it's hard to spot).

I've only put UINT64_C around the 64 bit constant, because those macros aren't used anywhere else in the code. This avoids some warnings in overly strict compilers (cstd 6.4.4.1 allows large integer constants and puts them into whatever type can represent them, but some compilers warn about it anyway and ask you to put suffixes like ULL).

The rest of the code uses mostly hex constants, which are much less
error-prone than a complicated number like 9223372036834775807 (in which
I made a mistake, but it's hard to spot).

I've only put `UINT64_C` around the 64 bit constant, because those
macros aren't used anywhere else in the code. This avoids some warnings
in overly strict compilers (cstd 6.4.4.1 allows large integer constants
and puts them into whatever type can represent them, but some compilers
warn about it anyway and ask you to put suffixes like `ULL`).
@iphydf
Copy link
Contributor Author

iphydf commented Apr 5, 2024

Should we use INTxx_MAX/MIN, instead?

return cmp_write_s16(ctx, (int16_t)d);
if (d >= (-2147483647 - 1))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a confusing construct. Why did we have an expression like this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't at all remember; but git blame says it once was -2147483648 which (at least some) 32-bit compilers didn't like.

@camgunz
Copy link
Owner

camgunz commented Apr 14, 2024

Should we use INTxx_MAX/MIN, instead?

Nah; these are MessagePack-specific constants, not platform constants; they shouldn't change based on what your machine thinks the biggest int (etc.) is.

@camgunz
Copy link
Owner

camgunz commented Apr 14, 2024

I tried really hard to get any compiler on Godbolt to complain about this, but nothing did so I think we're fine here. I'll merge and see if anyone experiences any issues.

@camgunz camgunz merged commit e5dbe35 into camgunz:master Apr 14, 2024
1 check passed
@iphydf
Copy link
Contributor Author

iphydf commented Apr 14, 2024

Should we use INTxx_MAX/MIN, instead?

Nah; these are MessagePack-specific constants, not platform constants; they shouldn't change based on what your machine thinks the biggest int (etc.) is.

I mean INT32_MAX etc. They have exact definitions and aren't platform-specific.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants