Skip to content

Commit

Permalink
[clang] Satisfy clang v12
Browse files Browse the repository at this point in the history
Older versions of clang (for example, v12) throw an error when compiling
CStringChecker.cpp that the initializers for `SourceArgExpr`,
`DestinationArgExpr`, and `SizeArgExpr` are missing braces around
initialization of subobject.  Newer clang versions don't throw this
error.  This patch adds the initialization braces to satisfy clang.

Reviewed By: steakhal

Differential Revision: https://reviews.llvm.org/D154871
  • Loading branch information
ashay committed Jul 11, 2023
1 parent 9602aa4 commit 46333f7
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,9 +1372,9 @@ void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE,
CharKind CK) const {
// void *memcpy(void *restrict dst, const void *restrict src, size_t n);
// The return value is the address of the destination buffer.
DestinationArgExpr Dest = {CE->getArg(0), 0};
SourceArgExpr Src = {CE->getArg(1), 1};
SizeArgExpr Size = {CE->getArg(2), 2};
DestinationArgExpr Dest = {{CE->getArg(0), 0}};
SourceArgExpr Src = {{CE->getArg(1), 1}};
SizeArgExpr Size = {{CE->getArg(2), 2}};

ProgramStateRef State = C.getState();

Expand All @@ -1387,9 +1387,9 @@ void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE,
CharKind CK) const {
// void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
// The return value is a pointer to the byte following the last written byte.
DestinationArgExpr Dest = {CE->getArg(0), 0};
SourceArgExpr Src = {CE->getArg(1), 1};
SizeArgExpr Size = {CE->getArg(2), 2};
DestinationArgExpr Dest = {{CE->getArg(0), 0}};
SourceArgExpr Src = {{CE->getArg(1), 1}};
SizeArgExpr Size = {{CE->getArg(2), 2}};

constexpr bool IsRestricted = true;
constexpr bool IsMempcpy = true;
Expand All @@ -1401,9 +1401,9 @@ void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE,
CharKind CK) const {
// void *memmove(void *dst, const void *src, size_t n);
// The return value is the address of the destination buffer.
DestinationArgExpr Dest = {CE->getArg(0), 0};
SourceArgExpr Src = {CE->getArg(1), 1};
SizeArgExpr Size = {CE->getArg(2), 2};
DestinationArgExpr Dest = {{CE->getArg(0), 0}};
SourceArgExpr Src = {{CE->getArg(1), 1}};
SizeArgExpr Size = {{CE->getArg(2), 2}};

constexpr bool IsRestricted = false;
constexpr bool IsMempcpy = false;
Expand All @@ -1413,9 +1413,9 @@ void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE,

void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
// void bcopy(const void *src, void *dst, size_t n);
SourceArgExpr Src{CE->getArg(0), 0};
DestinationArgExpr Dest = {CE->getArg(1), 1};
SizeArgExpr Size = {CE->getArg(2), 2};
SourceArgExpr Src{{CE->getArg(0), 0}};
DestinationArgExpr Dest = {{CE->getArg(1), 1}};
SizeArgExpr Size = {{CE->getArg(2), 2}};

constexpr bool IsRestricted = false;
constexpr bool IsMempcpy = false;
Expand All @@ -1430,7 +1430,7 @@ void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE,

AnyArgExpr Left = {CE->getArg(0), 0};
AnyArgExpr Right = {CE->getArg(1), 1};
SizeArgExpr Size = {CE->getArg(2), 2};
SizeArgExpr Size = {{CE->getArg(2), 2}};

ProgramStateRef State = C.getState();
SValBuilder &Builder = C.getSValBuilder();
Expand Down Expand Up @@ -1698,14 +1698,14 @@ void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
const LocationContext *LCtx = C.getLocationContext();

// Check that the destination is non-null.
DestinationArgExpr Dst = {CE->getArg(0), 0};
DestinationArgExpr Dst = {{CE->getArg(0), 0}};
SVal DstVal = state->getSVal(Dst.Expression, LCtx);
state = checkNonNull(C, state, Dst, DstVal);
if (!state)
return;

// Check that the source is non-null.
SourceArgExpr srcExpr = {CE->getArg(1), 1};
SourceArgExpr srcExpr = {{CE->getArg(1), 1}};
SVal srcVal = state->getSVal(srcExpr.Expression, LCtx);
state = checkNonNull(C, state, srcExpr, srcVal);
if (!state)
Expand Down Expand Up @@ -1736,10 +1736,11 @@ void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,

// FIXME: Why do we choose the srcExpr if the access has no size?
// Note that the 3rd argument of the call would be the size parameter.
SizeArgExpr SrcExprAsSizeDummy = {srcExpr.Expression, srcExpr.ArgumentIndex};
SizeArgExpr SrcExprAsSizeDummy = {
{srcExpr.Expression, srcExpr.ArgumentIndex}};
state = CheckOverlap(
C, state,
(IsBounded ? SizeArgExpr{CE->getArg(2), 2} : SrcExprAsSizeDummy), Dst,
(IsBounded ? SizeArgExpr{{CE->getArg(2), 2}} : SrcExprAsSizeDummy), Dst,
srcExpr);

if (!state)
Expand All @@ -1748,7 +1749,7 @@ void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
// If the function is strncpy, strncat, etc... it is bounded.
if (IsBounded) {
// Get the max number of characters to copy.
SizeArgExpr lenExpr = {CE->getArg(2), 2};
SizeArgExpr lenExpr = {{CE->getArg(2), 2}};
SVal lenVal = state->getSVal(lenExpr.Expression, LCtx);

// Protect against misdeclared strncpy().
Expand Down Expand Up @@ -2222,7 +2223,7 @@ void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const {
// char *strsep(char **stringp, const char *delim);
// Verify whether the search string parameter matches the return type.
SourceArgExpr SearchStrPtr = {CE->getArg(0), 0};
SourceArgExpr SearchStrPtr = {{CE->getArg(0), 0}};

QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType();
if (CharPtrTy.isNull() ||
Expand Down Expand Up @@ -2323,9 +2324,9 @@ void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
// void *memset(void *s, int c, size_t n);
CurrentFunctionDescription = "memory set function";

DestinationArgExpr Buffer = {CE->getArg(0), 0};
DestinationArgExpr Buffer = {{CE->getArg(0), 0}};
AnyArgExpr CharE = {CE->getArg(1), 1};
SizeArgExpr Size = {CE->getArg(2), 2};
SizeArgExpr Size = {{CE->getArg(2), 2}};

ProgramStateRef State = C.getState();

Expand Down Expand Up @@ -2372,8 +2373,8 @@ void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const {
CurrentFunctionDescription = "memory clearance function";

DestinationArgExpr Buffer = {CE->getArg(0), 0};
SizeArgExpr Size = {CE->getArg(1), 1};
DestinationArgExpr Buffer = {{CE->getArg(0), 0}};
SizeArgExpr Size = {{CE->getArg(1), 1}};
SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy);

ProgramStateRef State = C.getState();
Expand Down Expand Up @@ -2427,7 +2428,7 @@ void CStringChecker::evalSnprintf(CheckerContext &C, const CallExpr *CE) const {
void CStringChecker::evalSprintfCommon(CheckerContext &C, const CallExpr *CE,
bool IsBounded, bool IsBuiltin) const {
ProgramStateRef State = C.getState();
DestinationArgExpr Dest = {CE->getArg(0), 0};
DestinationArgExpr Dest = {{CE->getArg(0), 0}};

const auto NumParams = CE->getCalleeDecl()->getAsFunction()->getNumParams();
assert(CE->getNumArgs() >= NumParams);
Expand All @@ -2442,14 +2443,15 @@ void CStringChecker::evalSprintfCommon(CheckerContext &C, const CallExpr *CE,
!type->isAnyPointerType() ||
!type->getPointeeType()->isAnyCharacterType())
continue;
SourceArgExpr Source = {ArgExpr, unsigned(ArgIdx)};
SourceArgExpr Source = {{ArgExpr, unsigned(ArgIdx)}};

// Ensure the buffers do not overlap.
SizeArgExpr SrcExprAsSizeDummy = {Source.Expression, Source.ArgumentIndex};
SizeArgExpr SrcExprAsSizeDummy = {
{Source.Expression, Source.ArgumentIndex}};
State = CheckOverlap(
C, State,
(IsBounded ? SizeArgExpr{CE->getArg(1), 1} : SrcExprAsSizeDummy), Dest,
Source);
(IsBounded ? SizeArgExpr{{CE->getArg(1), 1}} : SrcExprAsSizeDummy),
Dest, Source);
if (!State)
return;
}
Expand Down

0 comments on commit 46333f7

Please sign in to comment.