Skip to content

[Clang][Sema] Add fortify warnings for stpcpy #141646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,8 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
switch (BuiltinID) {
default:
return;
case Builtin::BI__builtin_stpcpy:
case Builtin::BIstpcpy:
case Builtin::BI__builtin_strcpy:
case Builtin::BIstrcpy: {
DiagID = diag::warn_fortify_strlen_overflow;
Expand All @@ -1265,6 +1267,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
break;
}

case Builtin::BI__builtin___stpcpy_chk:
case Builtin::BI__builtin___strcpy_chk: {
DiagID = diag::warn_fortify_strlen_overflow;
SourceSize = ComputeStrLenArgument(1);
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Sema/warn-fortify-source.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ void call_strcpy_nowarn(void) {
__builtin_strcpy(dst, src);
}

void call_stpcpy(void) {
const char *const src = "abcd";
char dst1[5];
char dst2[4];
__builtin_stpcpy(dst1, src);
__builtin_stpcpy(dst2, src); // expected-warning {{'stpcpy' will always overflow; destination buffer has size 4, but the source string has length 5 (including NUL byte)}}
}

void call_memmove(void) {
char s1[10], s2[20];
__builtin_memmove(s2, s1, 20);
Expand Down