Skip to content

Commit 3c20e25

Browse files
authored
[NFC] Size and element numbers are often swapped when calling calloc (#79081)
gcc-14 will now throw a warning if size and elements are swapped.
1 parent f05dd29 commit 3c20e25

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

clang/test/Analysis/malloc.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,17 @@ void testUseAfterFree() {
116116
}
117117

118118
void testNoCopy() {
119-
char *p = (char *)calloc(sizeof(int), 1);
119+
char *p = (char *)calloc(1, sizeof(int));
120120
CustomData *w = [CustomData somethingNoCopy:p]; // no-warning
121121
}
122122

123123
void testFreeWhenDone() {
124-
char *p = (char *)calloc(sizeof(int), 1);
124+
char *p = (char *)calloc(1, sizeof(int));
125125
CustomData *w = [CustomData something:p freeWhenDone:1]; // no-warning
126126
}
127127

128128
void testFreeWhenDonePositive() {
129-
char *p = (char *)calloc(sizeof(int), 1);
129+
char *p = (char *)calloc(1, sizeof(int));
130130
CustomData *w = [CustomData something:p freeWhenDone:0]; // expected-warning{{leak}}
131131
}
132132

clang/test/Analysis/uninit-vals.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Point makePoint(float x, float y) {
158158
}
159159

160160
void PR14765_test(void) {
161-
Circle *testObj = calloc(sizeof(Circle), 1);
161+
Circle *testObj = calloc(1, sizeof(Circle));
162162

163163
clang_analyzer_eval(testObj->size == 0); // expected-warning{{TRUE}}
164164
// expected-note@-1{{TRUE}}
@@ -207,7 +207,7 @@ IntPoint makeIntPoint(int x, int y) {
207207
}
208208

209209
void PR14765_test_int(void) {
210-
IntCircle *testObj = calloc(sizeof(IntCircle), 1);
210+
IntCircle *testObj = calloc(1, sizeof(IntCircle));
211211

212212
clang_analyzer_eval(testObj->size == 0); // expected-warning{{TRUE}}
213213
// expected-note@-1{{TRUE}}
@@ -311,7 +311,7 @@ void testLargeStructsNotCopiedPerField(void) {
311311
}
312312

313313
void testSmallStructInLargerStruct(void) {
314-
IntCircle2D *testObj = calloc(sizeof(IntCircle2D), 1);
314+
IntCircle2D *testObj = calloc(1, sizeof(IntCircle2D));
315315

316316
clang_analyzer_eval(testObj->size == 0); // expected-warning{{TRUE}}
317317
// expected-note@-1{{TRUE}}

clang/test/CodeGen/alloc-size.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void test5(void) {
137137
// CHECK: store i32 36
138138
gi = OBJECT_SIZE_BUILTIN(&data->t[1], 3);
139139

140-
struct Data *const arr = my_calloc(sizeof(*data), 2);
140+
struct Data *const arr = my_calloc(2, sizeof(*data));
141141
// CHECK: store i32 96
142142
gi = OBJECT_SIZE_BUILTIN(arr, 0);
143143
// CHECK: store i32 96
@@ -171,7 +171,7 @@ void test6(void) {
171171
// CHECK: store i32 11
172172
gi = OBJECT_SIZE_BUILTIN(data->end, 3);
173173

174-
struct Data *const arr = my_calloc(sizeof(*arr) + 5, 3);
174+
struct Data *const arr = my_calloc(3, sizeof(*arr) + 5);
175175
// AFAICT, GCC treats malloc and calloc identically. So, we should do the
176176
// same.
177177
//

compiler-rt/lib/profile/InstrProfilingFile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ static void initFileWriter(ProfDataWriter *This, FILE *File) {
335335
COMPILER_RT_VISIBILITY ProfBufferIO *
336336
lprofCreateBufferIOInternal(void *File, uint32_t BufferSz) {
337337
FreeHook = &free;
338-
DynamicBufferIOBuffer = (uint8_t *)calloc(BufferSz, 1);
338+
DynamicBufferIOBuffer = (uint8_t *)calloc(1, BufferSz);
339339
VPBufferSize = BufferSz;
340340
ProfDataWriter *fileWriter =
341-
(ProfDataWriter *)calloc(sizeof(ProfDataWriter), 1);
341+
(ProfDataWriter *)calloc(1, sizeof(ProfDataWriter));
342342
initFileWriter(fileWriter, File);
343343
ProfBufferIO *IO = lprofCreateBufferIO(fileWriter);
344344
IO->OwnFileWriter = 1;

compiler-rt/test/tsan/java_finalizer2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void *Ballast(void *p) {
5151
}
5252

5353
int main() {
54-
Heap* heap = (Heap*)calloc(sizeof(Heap), 2) + 1;
54+
Heap *heap = (Heap *)calloc(2, sizeof(Heap)) + 1;
5555
__tsan_java_init((jptr)heap, sizeof(*heap));
5656
__tsan_java_alloc((jptr)heap, sizeof(*heap));
5757
// Ballast threads merely make the bug a bit easier to trigger.

0 commit comments

Comments
 (0)