Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2738c0

Browse files
Fix crash in 'malloc' referring to function without a argument (#159371)
As reported in #159080, patch #68059 didn't correctly check for the argument count of the target function from malloc to ensure it has an argument. This patch corrects that check. Fixes: #159080 --------- Co-authored-by: Sergei Barannikov <[email protected]>
1 parent 51a840e commit d2738c0

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ Bug Fixes in This Version
347347
``-Wshadow`` and show uncaptured-local warnings with ``-Wshadow-all``. (#GH68605)
348348
- Fixed a failed assertion with a negative limit parameter value inside of
349349
``__has_embed``. (#GH157842)
350+
- Fixed an assertion when an improper use of the ``malloc`` attribute targeting
351+
a function without arguments caused us to try to access a non-existent argument.
352+
(#GH159080)
350353

351354
Bug Fixes to Compiler Builtins
352355
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,11 @@ static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
18021802
if (AL.getNumArgs() == 1) {
18031803
DeallocPtrIdx = ParamIdx(1, DeallocFD);
18041804

1805-
if (!DeallocPtrIdx.isValid() ||
1805+
// FIXME: We could probably be better about diagnosing that there IS no
1806+
// argument, or that the function doesn't have a prototype, but this is how
1807+
// GCC diagnoses this, and is reasonably clear.
1808+
if (!DeallocPtrIdx.isValid() || !hasFunctionProto(DeallocFD) ||
1809+
getFunctionOrMethodNumParams(DeallocFD) < 1 ||
18061810
!getFunctionOrMethodParamType(DeallocFD, DeallocPtrIdx.getASTIndex())
18071811
.getCanonicalType()
18081812
->isPointerType()) {

clang/test/Sema/attr-args.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ __attribute__ ((__format_arg__(2))) // expected-error {{'__format_arg__' attribu
2929
void test (int, ...);
3030

3131
void __attribute__ ((alloc_size (2, 3))) *test2(int, ...); // expected-error {{'alloc_size' attribute parameter 1 is out of bounds}}
32+
33+
void gh159080_a(void);
34+
void *gh159080_b(void) __attribute__((malloc(gh159080_a))); // expected-error{{'malloc' argument 'gh159080_a' must take a pointer type as its first argument}}
35+
void gh159080_c();
36+
void *gh159080_d(void) __attribute__((malloc(gh159080_c))); // expected-error{{'malloc' argument 'gh159080_c' must take a pointer type as its first argument}}
37+

0 commit comments

Comments
 (0)