Skip to content

Commit 9ba1a3f

Browse files
authored
Reject invalid integer constants in unevaluated preprocessor operands (#134884)
Clang was previously accepting invalid code like: ``` #if 1 ? 1 : 999999999999999999999 #endif ``` because the integer constant (which is too large to fit into any standard or extended integer type) was in an unevaluated branch of the conditional operator. Similar invalid code involving || or && was also accepted and is now rejected. Fixes #134658
1 parent 7e70d70 commit 9ba1a3f

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ Bug Fixes in This Version
354354
- Defining an integer literal suffix (e.g., ``LL``) before including
355355
``<stdint.h>`` in a freestanding build no longer causes invalid token pasting
356356
when using the ``INTn_C`` macros. (#GH85995)
357+
- Clang no longer accepts invalid integer constants which are too large to fit
358+
into any (standard or extended) integer type when the constant is unevaluated.
359+
Merely forming the token is sufficient to render the program invalid. Code
360+
like this was previously accepted and is now rejected (#GH134658):
361+
.. code-block:: c
362+
363+
#if 1 ? 1 : 999999999999999999999
364+
#endif
357365
358366
Bug Fixes to Compiler Builtins
359367
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Lex/PPExpressions.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,7 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
345345
// Parse the integer literal into Result.
346346
if (Literal.GetIntegerValue(Result.Val)) {
347347
// Overflow parsing integer literal.
348-
if (ValueLive)
349-
PP.Diag(PeekTok, diag::err_integer_literal_too_large)
350-
<< /* Unsigned */ 1;
348+
PP.Diag(PeekTok, diag::err_integer_literal_too_large) << /* Unsigned */ 1;
351349
Result.Val.setIsUnsigned(true);
352350
} else {
353351
// Set the signedness of the result to match whether there was a U suffix

clang/test/Preprocessor/constants.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %clang_cc1 -E -verify %s
2+
3+
// C++ [lex.icon]p4 and C 6.4.4.1p2 + 6.4.4.2p7 both require C and C++ to
4+
// validate the integer constant value when converting a preprocessing token
5+
// into a token for semantic analysis, even within the preprocessor itself.
6+
7+
// Plain integer constant.
8+
#if 999999999999999999999 // expected-error {{integer literal is too large to be represented in any integer type}}
9+
#endif
10+
11+
// These cases were previously incorrectly accepted. See GH134658.
12+
13+
// Integer constant in an unevaluated branch of a conditional.
14+
#if 1 ? 1 : 999999999999999999999 // expected-error {{integer literal is too large to be represented in any integer type}}
15+
#endif
16+
17+
// Integer constant in an unevaluated operand of a logical operator.
18+
#if 0 && 999999999999999999999 // expected-error {{integer literal is too large to be represented in any integer type}}
19+
#endif
20+
21+
#if 1 || 999999999999999999999 // expected-error {{integer literal is too large to be represented in any integer type}}
22+
#endif
23+
24+
// Make sure we also catch it in an elif condition.
25+
#if 0
26+
#elif 1 || 999999999999999999999 // expected-error {{integer literal is too large to be represented in any integer type}}
27+
#endif
28+
29+
// However, if the block is skipped entirely, then it doesn't matter how
30+
// invalid the constant value is.
31+
#if 0
32+
int x = 999999999999999999999;
33+
34+
#if 999999999999999999999
35+
#endif
36+
37+
#if 0 && 999999999999999999999
38+
#endif
39+
40+
#endif

0 commit comments

Comments
 (0)