Skip to content

Commit

Permalink
[Diagnostic] Warn if the size argument of memset is character literal
Browse files Browse the repository at this point in the history
zero

Closing #55402

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D125521
  • Loading branch information
ChuanqiXu9 committed May 16, 2022
1 parent 1878f24 commit 3bef90d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clang/lib/Sema/SemaChecking.cpp
Expand Up @@ -11200,7 +11200,10 @@ static void CheckMemaccessSize(Sema &S, unsigned BId, const CallExpr *Call) {
Call->getArg(BId == Builtin::BImemset ? 2 : 1)->IgnoreImpCasts();

auto isLiteralZero = [](const Expr *E) {
return isa<IntegerLiteral>(E) && cast<IntegerLiteral>(E)->getValue() == 0;
return (isa<IntegerLiteral>(E) &&
cast<IntegerLiteral>(E)->getValue() == 0) ||
(isa<CharacterLiteral>(E) &&
cast<CharacterLiteral>(E)->getValue() == 0);
};

// If we're memsetting or bzeroing 0 bytes, then this is likely an error.
Expand Down
2 changes: 2 additions & 0 deletions clang/test/Sema/transpose-memset.c
Expand Up @@ -11,8 +11,10 @@ int *ptr;
int main(void) {
memset(array, sizeof(array), 0); // expected-warning{{'size' argument to memset is '0'; did you mean to transpose the last two arguments?}} expected-note{{parenthesize the third argument to silence}}
memset(array, sizeof(array), 0xff); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}} expected-warning{{'memset' will always overflow; destination buffer has size 40, but size argument is 255}}
memset(array, sizeof(array), '\0'); // expected-warning{{'size' argument to memset is '0'; did you mean to transpose the last two arguments?}} expected-note{{parenthesize the third argument to silence}}
memset(ptr, sizeof(ptr), 0); // expected-warning{{'size' argument to memset is '0'; did you mean to transpose the last two arguments?}} expected-note{{parenthesize the third argument to silence}}
memset(ptr, sizeof(*ptr) * 10, 1); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
memset(ptr, sizeof(ptr), '\0'); // expected-warning{{'size' argument to memset is '0'; did you mean to transpose the last two arguments?}} expected-note{{parenthesize the third argument to silence}}
memset(ptr, 10 * sizeof(int *), 1); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
memset(ptr, 10 * sizeof(int *) + 10, 0xff); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
memset(ptr, sizeof(char) * sizeof(int *), 0xff); // expected-warning{{setting buffer to a 'sizeof' expression; did you mean to transpose the last two arguments?}} expected-note{{cast the second argument to 'int' to silence}}
Expand Down

0 comments on commit 3bef90d

Please sign in to comment.