Skip to content

Commit 5f38548

Browse files
authored
[Sema] Allow zero-size allocations for -Walloc-size (#155793)
Allocations of size zero are usually done intentionally and then reallocated before use. Fixes #155633
1 parent 6d902b6 commit 5f38548

8 files changed

+14
-24
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7841,7 +7841,9 @@ static void CheckSufficientAllocSize(Sema &S, QualType DestType,
78417841
return;
78427842
std::optional<llvm::APInt> AllocSize =
78437843
CE->evaluateBytesReturnedByAllocSizeCall(S.Context);
7844-
if (!AllocSize)
7844+
// Allocations of size zero are permitted as a special case. They are usually
7845+
// done intentionally.
7846+
if (!AllocSize || AllocSize->isZero())
78457847
return;
78467848
auto Size = CharUnits::fromQuantity(AllocSize->getZExtValue());
78477849

clang/test/Analysis/Malloc+MismatchedDeallocator+NewDelete.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_analyze_cc1 -Wno-alloc-size -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete -std=c++11 -verify %s
2-
// RUN: %clang_analyze_cc1 -Wno-alloc-size -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -verify %s
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete -std=c++11 -verify %s
2+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator,cplusplus.NewDelete,cplusplus.NewDeleteLeaks -DLEAKS -std=c++11 -verify %s
33

44
#include "Inputs/system-header-simulator-for-malloc.h"
55

clang/test/Analysis/Malloc+MismatchedDeallocator_intersections.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_analyze_cc1 -Wno-alloc-size -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator -std=c++11 -verify %s
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,unix.MismatchedDeallocator -std=c++11 -verify %s
22
// expected-no-diagnostics
33

44
typedef __typeof(sizeof(int)) size_t;

clang/test/Analysis/MismatchedDeallocator-checker-test.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_analyze_cc1 -Wno-alloc-size -analyzer-checker=core,unix.MismatchedDeallocator -fblocks -verify %s
2-
// RUN: %clang_analyze_cc1 -Wno-alloc-size -analyzer-checker=core,unix.MismatchedDeallocator -fblocks -DTEST_INLINABLE_ALLOCATORS -verify %s
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.MismatchedDeallocator -fblocks -verify %s
2+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.MismatchedDeallocator -fblocks -DTEST_INLINABLE_ALLOCATORS -verify %s
33

44
#include "Inputs/system-header-simulator-objc.h"
55
#include "Inputs/system-header-simulator-cxx.h"

clang/test/Analysis/NewDelete-checker-test.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks %s \
2-
// RUN: -Wno-alloc-size \
32
// RUN: -verify=expected,newdelete \
43
// RUN: -analyzer-checker=core \
54
// RUN: -analyzer-checker=cplusplus.NewDelete
65
//
76
// RUN: %clang_analyze_cc1 -DLEAKS -std=c++11 -fblocks %s \
8-
// RUN: -Wno-alloc-size \
97
// RUN: -verify=expected,newdelete,leak \
108
// RUN: -analyzer-checker=core \
119
// RUN: -analyzer-checker=cplusplus.NewDelete \
1210
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
1311
//
1412
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -verify %s \
15-
// RUN: -Wno-alloc-size \
1613
// RUN: -verify=expected,leak \
1714
// RUN: -analyzer-checker=core \
1815
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
1916
//
2017
// RUN: %clang_analyze_cc1 -std=c++17 -fblocks %s \
21-
// RUN: -Wno-alloc-size \
2218
// RUN: -verify=expected,newdelete \
2319
// RUN: -analyzer-checker=core \
2420
// RUN: -analyzer-checker=cplusplus.NewDelete
2521
//
2622
// RUN: %clang_analyze_cc1 -DLEAKS -std=c++17 -fblocks %s \
27-
// RUN: -Wno-alloc-size \
2823
// RUN: -verify=expected,newdelete,leak \
2924
// RUN: -analyzer-checker=core \
3025
// RUN: -analyzer-checker=cplusplus.NewDelete \
3126
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
3227
//
3328
// RUN: %clang_analyze_cc1 -std=c++17 -fblocks -verify %s \
34-
// RUN: -Wno-alloc-size \
3529
// RUN: -verify=expected,leak,inspection \
3630
// RUN: -analyzer-checker=core \
3731
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks \

clang/test/Analysis/NewDelete-intersections.mm

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
// RUN: %clang_analyze_cc1 -std=c++11 -fblocks %s \
2-
// RUN: -Wno-alloc-size \
32
// RUN: -verify=newdelete \
43
// RUN: -analyzer-checker=core \
54
// RUN: -analyzer-checker=cplusplus.NewDelete
65

76
// leak-no-diagnostics
87

98
// RUN: %clang_analyze_cc1 -std=c++11 -DLEAKS -fblocks %s \
10-
// RUN: -Wno-alloc-size \
119
// RUN: -verify=leak \
1210
// RUN: -analyzer-checker=core \
1311
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
1412

1513
// RUN: %clang_analyze_cc1 -std=c++11 -DLEAKS -fblocks %s \
16-
// RUN: -Wno-alloc-size \
1714
// RUN: -verify=mismatch \
1815
// RUN: -analyzer-checker=core \
1916
// RUN: -analyzer-checker=unix.MismatchedDeallocator

clang/test/Analysis/unix-fns.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -Wno-alloc-size -analyzer-checker=core,unix.API,osx.API,optin.portability %s -analyzer-output=plist -analyzer-config faux-bodies=true -fblocks -verify -o %t.plist
1+
// RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core,unix.API,osx.API,optin.portability %s -analyzer-output=plist -analyzer-config faux-bodies=true -fblocks -verify -o %t.plist
22
// RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/unix-fns.c.plist -
3-
// RUN: %clang_analyze_cc1 -triple x86_64-unknown-linux -Wno-alloc-size -analyzer-checker=core,unix.API,osx.API,optin.portability %s -analyzer-output=plist -analyzer-config faux-bodies=true -fblocks -verify -o %t.plist
3+
// RUN: %clang_analyze_cc1 -triple x86_64-unknown-linux -analyzer-checker=core,unix.API,osx.API,optin.portability %s -analyzer-output=plist -analyzer-config faux-bodies=true -fblocks -verify -o %t.plist
44
// RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/unix-fns.c.plist -
55
// RUN: mkdir -p %t.dir
66
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.API,osx.API,optin.portability -analyzer-output=html -analyzer-config faux-bodies=true -fblocks -o %t.dir %s

clang/test/Sema/warn-alloc-size.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// RUN: %clang_cc1 -triple x86_64-linux -fsyntax-only -verify -Walloc-size %s
22
struct Foo { int x[10]; };
33

4-
struct ZeroSize {
5-
int flexible_array[];
6-
};
7-
84
typedef __typeof__(sizeof(int)) size_t;
95
void *my_malloc(size_t) __attribute__((alloc_size(1)));
106
void *my_calloc(size_t, size_t) __attribute__((alloc_size(2, 1)));
@@ -42,8 +38,9 @@ void alloc_foo(void) {
4238
// expected-warning@-1 {{allocation of insufficient size '1' for type 'int' with size '4'}}
4339
(void)(int *)my_malloc(1); // expected-warning {{allocation of insufficient size '1' for type 'int' with size '4'}}
4440

45-
struct ZeroSize *ptr18 = my_malloc(0); // okay because sizeof(struct ZeroSize) = 0
41+
void *funcptr_1 = (void (*)(int))my_malloc(1);
4642

47-
void *funcptr_1 = (void (*)(int))my_malloc(0); // expected-warning {{allocation of insufficient size '0' for type 'void (int)' with size '1'}}
48-
void *funcptr_2 = (void (*)(int))my_malloc(1);
43+
// Zero size allocations are assumed to be intentional.
44+
int *zero_alloc1 = my_malloc(0);
45+
int *zero_alloc2 = (int *)my_malloc(0);
4946
}

0 commit comments

Comments
 (0)