Skip to content

Commit da546e7

Browse files
Fix detection of malloc(a * sizeof(b)) with implicit casts.
This was the cause of the `definedType.c` failure on Windows X86, so we can now re-enable that test. We also have a dedicated test for the bug that should work in our main Linux environment. Fixes #345.
1 parent 1b8b690 commit da546e7

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

clang/lib/3C/ConstraintResolver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ PVConstraint *ConstraintResolver::addAtom(PVConstraint *PVC, ConstAtom *PtrTyp,
119119
}
120120

121121
static bool getSizeOfArg(Expr *Arg, QualType &ArgTy) {
122+
Arg = Arg->IgnoreParenImpCasts();
122123
if (auto *SizeOf = dyn_cast<UnaryExprOrTypeTraitExpr>(Arg))
123124
if (SizeOf->getKind() == UETT_SizeOf) {
124125
ArgTy = SizeOf->getTypeOfArgument();

clang/test/3C/definedType.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
// This test currently fails on Windows X86, but the 3C team is waiting to try
2-
// to fix it until Microsoft addresses a problem that currently makes the tests
3-
// difficult to run on Windows X86:
4-
//
5-
// https://github.com/microsoft/checkedc-clang/pull/956#issuecomment-752317866
6-
//
7-
// UNSUPPORTED: system-windows
8-
91
// RUN: rm -rf %t*
102
// RUN: 3c -base-dir=%S -addcr -alltypes %s -- | FileCheck -match-full-lines -check-prefixes="CHECK_ALL","CHECK" %s
113
// RUN: 3c -base-dir=%S -addcr %s -- | FileCheck -match-full-lines -check-prefixes="CHECK_NOALL","CHECK" %s

clang/test/3C/malloc_implicit_cast.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Regression test for failure to detect the `sizeof` in `malloc(x * sizeof(y))`
2+
// if there is an implicit cast around the `sizeof` because `x` is of an integer
3+
// type bigger than `size_t`
4+
// (https://github.com/correctcomputation/checkedc-clang/issues/345).
5+
6+
// Of course, in order to trigger the bug (if it exists), we need an integer
7+
// type bigger than `size_t`. We make a best effort to find one (in our main
8+
// Linux x86_64 environment with the GCC system headers, `size_t` should be 64
9+
// bits and `unsigned __int128` should be available); if we don't find one, the
10+
// test may falsely pass.
11+
12+
// RUN: 3c -base-dir=%S -alltypes %s -- | FileCheck -match-full-lines %s
13+
14+
#include <stddef.h>
15+
_Itype_for_any(T) void *malloc(size_t size) : itype(_Array_ptr<T>) byte_count(size);
16+
17+
#include <stdint.h>
18+
#ifdef __SIZEOF_INT128__
19+
typedef unsigned __int128 uintrealmax_t;
20+
#else
21+
typedef uintmax_t uintrealmax_t;
22+
#endif
23+
24+
void foo() {
25+
uintrealmax_t n = 5;
26+
// If the bug triggers, we'll get an "Unsafe call to allocator function" root
27+
// cause here (not tested) and `p` will remain wild.
28+
int *p = malloc(n * sizeof(int));
29+
// CHECK: _Array_ptr<int> p : count(n) = malloc<int>(n * sizeof(int));
30+
p[0] = 42;
31+
}

0 commit comments

Comments
 (0)