forked from OSchip/llvm-project
[analyzer] Fix a false positive in sizeof malloc checker.
Don't warn when the sizeof argument is an array with the same element type as the pointee of the return type. llvm-svn: 163407
This commit is contained in:
parent
a8d0ca070f
commit
694be01519
|
@ -184,7 +184,24 @@ public:
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QualType SizeofType = SFinder.Sizeofs[0]->getTypeOfArgument();
|
QualType SizeofType = SFinder.Sizeofs[0]->getTypeOfArgument();
|
||||||
if (!typesCompatible(BR.getContext(), PointeeType, SizeofType)) {
|
|
||||||
|
if (typesCompatible(BR.getContext(), PointeeType, SizeofType))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// If the argument to sizeof is an array, the result could be a
|
||||||
|
// pointer to the array element.
|
||||||
|
if (const ArrayType *AT = dyn_cast<ArrayType>(SizeofType)) {
|
||||||
|
QualType ElemType = AT->getElementType();
|
||||||
|
if (typesCompatible(BR.getContext(), PointeeType,
|
||||||
|
AT->getElementType()))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// For now, let's only reason about arrays of built in types.
|
||||||
|
if (!ElemType->isBuiltinType())
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const TypeSourceInfo *TSI = 0;
|
const TypeSourceInfo *TSI = 0;
|
||||||
if (i->CastedExprParent.is<const VarDecl *>()) {
|
if (i->CastedExprParent.is<const VarDecl *>()) {
|
||||||
TSI =
|
TSI =
|
||||||
|
@ -222,7 +239,6 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,3 +34,17 @@ void ignore_const() {
|
||||||
const char ***y = (const char ***)malloc(1 * sizeof(char *)); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'const char **', which is incompatible with sizeof operand type 'char *'}}
|
const char ***y = (const char ***)malloc(1 * sizeof(char *)); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'const char **', which is incompatible with sizeof operand type 'char *'}}
|
||||||
free(x);
|
free(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int *mallocArraySize() {
|
||||||
|
static const int sTable[10];
|
||||||
|
static const int nestedTable[10][10];
|
||||||
|
int *table = malloc(sizeof sTable);
|
||||||
|
int *table1 = malloc(sizeof nestedTable);
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
int *mallocWrongArraySize() {
|
||||||
|
static const double sTable[10];
|
||||||
|
int *table = malloc(sizeof sTable); // expected-warning {{Result of 'malloc' is converted to a pointer of type 'int', which is incompatible with sizeof operand type 'const double [10]'}}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue