Fix integer promotion causing a warning in MSVC #111
Merged
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 continues the discussion from #105.
When compiled on x86, MSVC generates the following warning (which becomes an error when the Treat Warnings As Errors option is used):
on the following line:
pcre2/src/pcre2_jit_compile.c
Line 5889 in b52d055
This warning seems wrong at first, as
a_pri
andb_pri
are of typePCRE2_UCHAR
, whilemax_pri
is of typesljit_u32
, so both operands are supposed to be unsigned.Turns out, implicit conversion rules in C are... complicated. The way I understand them, the
a_pri + b_pri
expression is promoted to typeint
, which causes the signed/unsigned mismatch on>=
.Here are the relevant quotes from the Integer promotions section:
When applied to the
a_pri + b_pri
expression:In the 8-bit and 16-bit versions of the library, the rank of
PCRE2_UCHAR
is less than the rank ofint
(see the definition of rank in the linked article), and equal in the 32-bit version, therefore the integer promotion rules apply.In the 8-bit and 16-bit versions,
int
can represent the entire range of values ofPCRE2_UCHAR
, therefore the expression type gets promoted to typeint
and the warning is raised.In the 32-bit version,
int
cannot represent the entire range of values ofPCRE2_UCHAR
, thereforeunsigned int
is used and no warning is raised.I'm not sure why the warning is not raised on x64 though. Since Windows uses the LLP64 data model, I believe it should have raised the same warning as on x86, but I didn't dig deeper.
This PR casts
a_pri
tosljit_u32
, which forces the expression to an unsigned type. Note that this is the type ofmax_pri
, to which the addition is assigned later on.