Ensure signed integer type for stse_ReturnCode_t enum#85
Open
parmi93 wants to merge 1 commit intoSTMicroelectronics:mainfrom
Open
Ensure signed integer type for stse_ReturnCode_t enum#85parmi93 wants to merge 1 commit intoSTMicroelectronics:mainfrom
stse_ReturnCode_t enum#85parmi93 wants to merge 1 commit intoSTMicroelectronics:mainfrom
Conversation
Add `__STSE_SIGNED = -1` as the last enum value to guarantee that `stse_ReturnCode_t` is treated as a signed integer type across all compilers. This allows printing enum values using the `%d` format specifier reliably on all platforms.
stse_ReturnCode_t enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a sentinel value
__STSE_SIGNED = -1to thestse_ReturnCode_tenum to guarantee that the enum is always treated as a signed integer type across all compilers and platforms.Why This Change?
In C, enumerations are implicitly convertible to integers, but the C standard does not mandate whether an enum type should be signed or unsigned. The actual underlying type depends on:
The Problem
Without this fix, users cannot write portable code to print
stse_ReturnCode_tvalues. Depending on the platform and compiler,%umay be required on some systems while%dis needed on others, resulting in non-portable and potentially buggy code.Without this fix, code like:
Could behave inconsistently:
%dgives incorrect output (user should use%u)%dgives correct outputThe Solution
By adding
__STSE_SIGNED = -1as the last enum value, we force the compiler to choose a signed integer type (since the enum now contains a negative value that must be representable).This guarantees:
%dformat specifier for printing enum valuesTechnical Details
Per C99/C11 standards, the underlying type of an enum is implementation-defined but must be capable of representing all enumerator values. By including a negative value, we ensure the enum's underlying type is signed integer, making
printf()with%dportable and safe.