[clang][Sema] `__swift_attr__` with no argument in type position crashes in HandleSwiftAttr (regression since clang 20)
__swift_attr__ without an argument in type-attribute position reads argument 0 of an empty argument list. Assertions builds assert, and release builds segfault instead of reporting the error. Invalid code; regression in clang 20.
Reproducer
https://godbolt.org/z/zYnT89fbh (assertions trunk, release trunk, 19.1.0, 20.1.0)
int * __attribute__((__swift_attr__)) a;
clang --target=x86_64-unknown-linux-gnu -c repro.c (same with clang++)
clang: clang/include/clang/Sema/ParsedAttr.h:375: clang::ArgsUnion clang::ParsedAttr::getArg(unsigned int) const: Assertion `Arg < NumArgs && "Arg access out of range!"' failed.
#10 clang::ParsedAttr::getArgAsExpr(unsigned int) const
#11 clang::Sema::checkStringLiteralArgumentAttr(clang::ParsedAttr const&, unsigned int, llvm::StringRef&, clang::SourceLocation*)
#12 processTypeAttrs(...) SemaType.cpp
#13 GetFullTypeForDeclarator(...) SemaType.cpp
Root cause
- In declaration position the same mistake is diagnosed:
__attribute__((__swift_attr__)) int a;giveserror: '__swift_attr__' attribute takes one argument. There,ProcessDeclAttributerunscheckCommonAttributeFeatures, which includes the argument-count check, beforeSemaSwift::handleAttrAttr. - On a declarator chunk the attribute goes to
HandleSwiftAttr(SemaType.cpp, inlined intoprocessTypeAttrsabove) instead. That function callsS.checkStringLiteralArgumentAttr(PAttr, 0, Str)without checking the argument count, andgetArgAsExpr(0)reads past the end of the empty argument list. - Type-attribute handlers have to do that check themselves;
HandleBTFTypeTagAttribute, for example, checksAttr.getNumArgs() != 1.
Suggested fix (untested)
if (!PAttr.checkExactlyNumArgs(S, 1)) { PAttr.setInvalid(); return; } before the checkStringLiteralArgumentAttr call in HandleSwiftAttr.
Details
- Classification: crash on invalid code.
__swift_attr__requires one string argument, and the declaration-position form and clang 16.0 to 19.1 reject this input with the error above. - Release builds: segfault in the same place (
checkStringLiteralArgumentAttr->Expr::IgnoreParenCasts), C and C++. - History: regression in clang 20. 16.0, 18.1 and 19.1 (release) and the 19.1 assertions build emit the error; 20.1 segfaults (release) or asserts (assertions build). Likely cause: #108631 ("[clang/AST] Make it possible to use SwiftAttr in type context", d3daa3c4435a), which added
HandleSwiftAttrwith this unchecked call. Not bisected. - Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a segfaults.
- 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/zYnT89fbh (assertions trunk, release trunk, 19.1.0, 20.1.0)
int * __attribute__((__swift_attr__)) a;
clang --target=x86_64-unknown-linux-gnu -c repro.c (same with clang++)
clang: clang/include/clang/Sema/ParsedAttr.h:375: clang::ArgsUnion clang::ParsedAttr::getArg(unsigned int) const: Assertion `Arg < NumArgs && "Arg access out of range!"' failed.
#<!-- -->10 clang::ParsedAttr::getArgAsExpr(unsigned int) const
#<!-- -->11 clang::Sema::checkStringLiteralArgumentAttr(clang::ParsedAttr const&, unsigned int, llvm::StringRef&, clang::SourceLocation*)
#<!-- -->12 processTypeAttrs(...) SemaType.cpp
#<!-- -->13 GetFullTypeForDeclarator(...) SemaType.cpp
Root cause
- In declaration position the same mistake is diagnosed:
__attribute__((__swift_attr__)) int a;giveserror: '__swift_attr__' attribute takes one argument. There,ProcessDeclAttributerunscheckCommonAttributeFeatures, which includes the argument-count check, beforeSemaSwift::handleAttrAttr. - On a declarator chunk the attribute goes to
HandleSwiftAttr(SemaType.cpp, inlined intoprocessTypeAttrsabove) instead. That function callsS.checkStringLiteralArgumentAttr(PAttr, 0, Str)without checking the argument count, andgetArgAsExpr(0)reads past the end of the empty argument list. - Type-attribute handlers have to do that check themselves;
HandleBTFTypeTagAttribute, for example, checksAttr.getNumArgs() != 1.
Suggested fix (untested)
if (!PAttr.checkExactlyNumArgs(S, 1)) { PAttr.setInvalid(); return; } before the checkStringLiteralArgumentAttr call in HandleSwiftAttr.
Details
- Classification: crash on invalid code.
__swift_attr__requires one string argument, and the declaration-position form and clang 16.0 to 19.1 reject this input with the error above. - Release builds: segfault in the same place (
checkStringLiteralArgumentAttr->Expr::IgnoreParenCasts), C and C++. - History: regression in clang 20. 16.0, 18.1 and 19.1 (release) and the 19.1 assertions build emit the error; 20.1 segfaults (release) or asserts (assertions build). Likely cause: #108631 ("[clang/AST] Make it possible to use SwiftAttr in type context", d3daa3c4435a), which added
HandleSwiftAttrwith this unchecked call. Not bisected. - Tested: clang 24.0.0git ef45ca75ce72 (Compiler Explorer assertions trunk, 2026-09-29); release trunk f4085066128a segfaults.
- Related: none found.
- Found by a fuzzer (generated C program), reduced by hand.
@rjmccall @Bigcheese