[clang][Sema] `++`/`--` on `_Atomic` function pointer crashes in diagnoseArithmeticOnFunctionPointer (release builds too)
++/-- on an _Atomic function pointer asserts in diagnoseArithmeticOnFunctionPointer, because the _Atomic wrapper is not stripped before the pointee type is read. Release builds segfault as soon as the diagnostic is printed (-Wpointer-arith/-pedantic in C, default flags in C++).
Reproducer
https://godbolt.org/z/sbb76zoMx (C: assertions trunk -c, release trunk -c -Wpointer-arith, GCC 16.2 -c -pedantic; C++: release trunk -c)
void f(void) { _Atomic(int (*)(void)) b; b++; }
clang --target=x86_64-unknown-linux-gnu -c repro.c (release builds: add -Wpointer-arith or -pedantic)
Assertions build:
clang: clang/lib/Sema/SemaExpr.cpp:11564: void diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*): Assertion `Pointer->getType()->isAnyPointerType()' failed.
#10 diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
#11 checkArithmeticOpPointerOperand(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
#12 CheckIncrementDecrementOperand(...) SemaExpr.cpp
#13 clang::Sema::CreateBuiltinUnaryOp(clang::SourceLocation, clang::UnaryOperatorKind, clang::Expr*, bool)
Release build with -Wpointer-arith:
#4 ConvertTypeToDiagnosticString(clang::ASTContext&, clang::QualType, ...) ASTDiagnostic.cpp
#5 clang::FormatASTNodeDiagnosticArgument(...)
#6 clang::Diagnostic::FormatDiagnostic(char const*, char const*, llvm::SmallVectorImpl<char>&) const
#13 diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
Program terminated with signal SIGSEGV (11)
Root cause
checkArithmeticOpPointerOperandlooks through theAtomicType(ResAtomicType->getValueType()) to find the function pointee, but then passes the original_Atomicoperand todiagnoseArithmeticOnFunctionPointer.- That function asserts
Pointer->getType()->isAnyPointerType()and passesPointer->getType()->getPointeeType()to the diagnostic. For anAtomicTypethat is a nullQualType. - In release builds the assertion is compiled out. With default C flags the GNU-extension warning is off, so the null type is never formatted and the build exits 0. With
-Wpointer-arithor-pedanticthe diagnostic printer segfaults. In C++ the same diagnostic is an error, so release clang++ segfaults with default flags.
Related spellings behave differently: --b is the same; b + 1 compiles (its operand has already gone through lvalue conversion, which drops _Atomic); b += 1 is rejected with invalid operands to binary expression ('_Atomic(int (*)(void))' and 'int'). GCC accepts all three. _Atomic(void *) v; v++; does not crash, because diagnoseArithmeticOnVoidPointer does not use the type.
Suggested fix (untested)
Strip _Atomic in diagnoseArithmeticOnFunctionPointer too, e.g. use Pointer->getType().getAtomicUnqualifiedType() for both the assertion and the pointee type.
Details
- Classification: in C, crash on valid GNU C (arithmetic on function pointers is a GNU extension; GCC 16.2 accepts
b++, and warns only with-pedantic). In C++, crash on invalid code (int (*p)(); p++;is an error there), and the assertion fires before that error is emitted. - History: long-standing. The same assertion fires in assertions builds from 3.7 to trunk; 3.6 compiles the file with no diagnostic. Release builds tried from 3.7.1 to 23.1 (3.7.1, 9.0, 16.0, 22.1, 23.1) segfault with
-Wpointer-arith; 3.6 does not. Likely cause: cf7d164ec1c8 ("Sema: Semantically check _Atomic-qualified pointers", 2015-02-12), which added theAtomicTypeunwrapping tocheckArithmeticOpPointerOperandbut not todiagnoseArithmeticOnFunctionPointer. Not bisected. - Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a as described above.
- Related: none found.
- Found by a fuzzer (generated C program), reduced by hand.
@llvm/issue-subscribers-clang-frontend
Author: skaeads12
Reproducer
https://godbolt.org/z/sbb76zoMx (C: assertions trunk -c, release trunk -c -Wpointer-arith, GCC 16.2 -c -pedantic; C++: release trunk -c)
void f(void) { _Atomic(int (*)(void)) b; b++; }
clang --target=x86_64-unknown-linux-gnu -c repro.c (release builds: add -Wpointer-arith or -pedantic)
Assertions build:
clang: clang/lib/Sema/SemaExpr.cpp:11564: void diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*): Assertion `Pointer->getType()->isAnyPointerType()' failed.
#<!-- -->10 diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
#<!-- -->11 checkArithmeticOpPointerOperand(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
#<!-- -->12 CheckIncrementDecrementOperand(...) SemaExpr.cpp
#<!-- -->13 clang::Sema::CreateBuiltinUnaryOp(clang::SourceLocation, clang::UnaryOperatorKind, clang::Expr*, bool)
Release build with -Wpointer-arith:
#<!-- -->4 ConvertTypeToDiagnosticString(clang::ASTContext&, clang::QualType, ...) ASTDiagnostic.cpp
#<!-- -->5 clang::FormatASTNodeDiagnosticArgument(...)
#<!-- -->6 clang::Diagnostic::FormatDiagnostic(char const*, char const*, llvm::SmallVectorImpl<char>&) const
#<!-- -->13 diagnoseArithmeticOnFunctionPointer(clang::Sema&, clang::SourceLocation, clang::Expr*) SemaExpr.cpp
Program terminated with signal SIGSEGV (11)
Root cause
checkArithmeticOpPointerOperandlooks through theAtomicType(ResAtomicType->getValueType()) to find the function pointee, but then passes the original_Atomicoperand todiagnoseArithmeticOnFunctionPointer.- That function asserts
Pointer->getType()->isAnyPointerType()and passesPointer->getType()->getPointeeType()to the diagnostic. For anAtomicTypethat is a nullQualType. - In release builds the assertion is compiled out. With default C flags the GNU-extension warning is off, so the null type is never formatted and the build exits 0. With
-Wpointer-arithor-pedanticthe diagnostic printer segfaults. In C++ the same diagnostic is an error, so release clang++ segfaults with default flags.
Related spellings behave differently: --b is the same; b + 1 compiles (its operand has already gone through lvalue conversion, which drops _Atomic); b += 1 is rejected with invalid operands to binary expression ('_Atomic(int (*)(void))' and 'int'). GCC accepts all three. _Atomic(void *) v; v++; does not crash, because diagnoseArithmeticOnVoidPointer does not use the type.
Suggested fix (untested)
Strip _Atomic in diagnoseArithmeticOnFunctionPointer too, e.g. use Pointer->getType().getAtomicUnqualifiedType() for both the assertion and the pointee type.
Details
- Classification: in C, crash on valid GNU C (arithmetic on function pointers is a GNU extension; GCC 16.2 accepts
b++, and warns only with-pedantic). In C++, crash on invalid code (int (*p)(); p++;is an error there), and the assertion fires before that error is emitted. - History: long-standing. The same assertion fires in assertions builds from 3.7 to trunk; 3.6 compiles the file with no diagnostic. Release builds tried from 3.7.1 to 23.1 (3.7.1, 9.0, 16.0, 22.1, 23.1) segfault with
-Wpointer-arith; 3.6 does not. Likely cause: cf7d164ec1c8 ("Sema: Semantically check _Atomic-qualified pointers", 2015-02-12), which added theAtomicTypeunwrapping tocheckArithmeticOpPointerOperandbut not todiagnoseArithmeticOnFunctionPointer. Not bisected. - Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a as described above.
- Related: none found.
- Found by a fuzzer (generated C program), reduced by hand.
working on this