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

ULL constants are not C89 #745

Open
real-or-random opened this issue Apr 20, 2020 · 1 comment
Open

ULL constants are not C89 #745

real-or-random opened this issue Apr 20, 2020 · 1 comment

Comments

@real-or-random
Copy link
Contributor

The README states as a goal: "Intended to be portable to any system with a C89 compiler and uint64_t support."

The C89 syntax does not specify unsigned long long (ULL) constants because there is no requirement that a unsigned long long type exists. We use ULL in a few places, e.g.:

#define SECP256K1_N_0 ((uint64_t)0xBFD25E8CD0364141ULL)

We should replace ULL with the UINT64_C macro from in stdint.h. Note that UL is not an issue: It's in the C89 syntax and unsigned long is guaranteed to have at least 32 value bits, so we can use it for uint32_t constants. We may still want to change it for consistency and readability (clearer expression of intent).

Alternatively, we can do nothing (because noone seemed to care about this so far).

real-or-random added a commit to real-or-random/secp256k1 that referenced this issue Apr 22, 2020
This commit is the result of creating a file constants.sed with contents
```
s/(0x([A-F]|[0-9])+)ULL/UINT64_C(\1)/gi
s/(0x([A-F]|[0-9])+)UL/UINT32_C(\1)/gi
s/(([0-9])+)ULL/UINT64_C(\1)/gi
s/(([0-9])+)UL/UINT32_C(\1)/gi
s/(([0-9])+)LL/INT64_C(\1)/gi
```
and running `sed -E -i -f constants.sed src/*.[ch]` (and fixing custom
indentation in four affected lines).

Use `git grep -iE '([A-F]|[0-9])UL'` to verify the result.

Moreover, this commit removes `-Wno-long-long` from CFLAGS, which is no longer
needed then. It also removes `-Wno-overlength-strings`, which is apparently
not needed currently either.

The motivation for this change is compliance with C89 (bitcoin-core#745) for ULL/LL but also
reviewability: Even though it's not relevant in the current code, I think it's
confusing to use the portable uint32_t/uint64_t types but then constants of
the unsigned long (long) types, which differ across targets.

Fixes bitcoin-core#745.
@prusnak
Copy link

prusnak commented Jan 19, 2021

long long is specified in C99, so possible fix would be just to replace C89 with C99 in the readme

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

No branches or pull requests

2 participants