llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang 21 accepts invalid # 0 preprocessor lines leading to undefined behavior at compile time

Open mariete1223 opened this issue 1 year ago • 2 comments

Summary

Clang 21 accepts invalid standalone # 0 preprocessor lines in C source files without emitting an error or a warning. When compiled, the program exhibits undefined behavior (e.g., prints unlimited spaces or crashes), despite the code being syntactically invalid.

Program

uint32_t square(uint32_t x)
# 0
# 0

To reproduce Quickly

https://gcc.godbolt.org/z/c1daT34v6

It gets:

[Truncated] Compiler returned: 143

mariete1223 avatar Apr 29 '25 17:04 mariete1223

@llvm/issue-subscribers-clang-frontend

Author: None (mariete1223)

## Summary

Clang 21 accepts invalid standalone # 0 preprocessor lines in C source files without emitting an error or a warning. When compiled, the program exhibits undefined behavior (e.g., prints unlimited spaces or crashes), despite the code being syntactically invalid.

Program

uint32_t square(uint32_t x)
# 0
# 0

To reproduce Quickly

https://gcc.godbolt.org/z/c1daT34v6

It gets:

[Truncated] Compiler returned: 143

llvmbot avatar Apr 29 '25 17:04 llvmbot

Just one is required:

uint32_t square(uint32_t x)
# 0

If we use -pedantic we see these are extension and treated as #line markers. Zero is not going to be valid but we are clearly doing something wrong here.

Looks like this started after clang-16: https://godbolt.org/z/aYK8dr8c8

shafik avatar Apr 29 '25 20:04 shafik