Skip to content

No longer assert on incorrect attribute argument index #133766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ Improvements to Clang's diagnostics

- Improve the ``-Wundefined-func-template`` warning when a function template is not instantiated due to being unreachable in modules.

- Fixed an assertion when referencing an out-of-bounds parameter via a function
attribute whose argument list refers to parameters by index and the function
is variadic. e.g.,
.. code-block:: c

__attribute__ ((__format_arg__(2))) void test (int i, ...) { }

Fixes #GH61635

Improvements to Clang's time-trace
----------------------------------

Expand Down
11 changes: 6 additions & 5 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -4796,10 +4796,10 @@ class Sema final : public SemaBase {
///
/// \returns true if IdxExpr is a valid index.
template <typename AttrInfo>
bool checkFunctionOrMethodParameterIndex(const Decl *D, const AttrInfo &AI,
unsigned AttrArgNum,
const Expr *IdxExpr, ParamIdx &Idx,
bool CanIndexImplicitThis = false) {
bool checkFunctionOrMethodParameterIndex(
const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false,
bool CanIndexVariadicArguments = false) {
assert(isFunctionOrMethodOrBlockForAttrSubject(D));

// In C++ the implicit 'this' function parameter also counts.
Expand All @@ -4820,7 +4820,8 @@ class Sema final : public SemaBase {
}

unsigned IdxSource = IdxInt->getLimitedValue(UINT_MAX);
if (IdxSource < 1 || (!IV && IdxSource > NumParams)) {
if (IdxSource < 1 ||
((!IV || !CanIndexVariadicArguments) && IdxSource > NumParams)) {
Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
<< &AI << AttrArgNum << IdxExpr->getSourceRange();
return false;
Expand Down
17 changes: 12 additions & 5 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,10 @@ static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
Expr *Ex = AL.getArgAsExpr(I);
ParamIdx Idx;
if (!S.checkFunctionOrMethodParameterIndex(D, AL, I + 1, Ex, Idx))
if (!S.checkFunctionOrMethodParameterIndex(
D, AL, I + 1, Ex, Idx,
/*CanIndexImplicitThis=*/false,
/*CanIndexVariadicArguments=*/true))
return;

// Is the function argument a pointer type?
Expand Down Expand Up @@ -5756,13 +5759,17 @@ static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,
}

ParamIdx ArgumentIdx;
if (!S.checkFunctionOrMethodParameterIndex(D, AL, 2, AL.getArgAsExpr(1),
ArgumentIdx))
if (!S.checkFunctionOrMethodParameterIndex(
D, AL, 2, AL.getArgAsExpr(1), ArgumentIdx,
/*CanIndexImplicitThis=*/false,
/*CanIndexVariadicArguments=*/true))
return;

ParamIdx TypeTagIdx;
if (!S.checkFunctionOrMethodParameterIndex(D, AL, 3, AL.getArgAsExpr(2),
TypeTagIdx))
if (!S.checkFunctionOrMethodParameterIndex(
D, AL, 3, AL.getArgAsExpr(2), TypeTagIdx,
/*CanIndexImplicitThis=*/false,
/*CanIndexVariadicArguments=*/true))
return;

bool IsPointer = AL.getAttrName()->getName() == "pointer_with_type_tag";
Expand Down
5 changes: 5 additions & 0 deletions clang/test/Sema/attr-args.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ inline __attribute__((stdcall(a))) void *f8(void); // expected-error {{'stdcall
inline __attribute__((used(a))) void *f9(void); // expected-error {{'used' attribute takes no arguments}}
inline __attribute__((unused(a))) void *f10(void); // expected-error {{'unused' attribute takes no arguments}}
inline __attribute__((weak(a))) void *f11(void); // expected-error {{'weak' attribute takes no arguments}}

__attribute__ ((__format_arg__(2))) // expected-error {{'__format_arg__' attribute parameter 1 is out of bounds}}
void test (int, ...);

void __attribute__ ((alloc_size (2, 3))) *test2(int, ...); // expected-error {{'alloc_size' attribute parameter 1 is out of bounds}}