Skip to content

Commit ee46630

Browse files
[Clang][Sema] Add fortify warnings for stpcpy (#141646)
As mentioned in #142230, I am adding fortify warnings for functions missing in Clang and I am starting with stpcpy.
1 parent 3cb967a commit ee46630

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,8 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
12571257
switch (BuiltinID) {
12581258
default:
12591259
return;
1260+
case Builtin::BI__builtin_stpcpy:
1261+
case Builtin::BIstpcpy:
12601262
case Builtin::BI__builtin_strcpy:
12611263
case Builtin::BIstrcpy: {
12621264
DiagID = diag::warn_fortify_strlen_overflow;
@@ -1265,6 +1267,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
12651267
break;
12661268
}
12671269

1270+
case Builtin::BI__builtin___stpcpy_chk:
12681271
case Builtin::BI__builtin___strcpy_chk: {
12691272
DiagID = diag::warn_fortify_strlen_overflow;
12701273
SourceSize = ComputeStrLenArgument(1);

clang/test/Sema/warn-fortify-source.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ void call_strcpy_nowarn(void) {
7171
__builtin_strcpy(dst, src);
7272
}
7373

74+
void call_stpcpy(void) {
75+
const char *const src = "abcd";
76+
char dst1[5];
77+
char dst2[4];
78+
__builtin_stpcpy(dst1, src);
79+
__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)}}
80+
}
81+
7482
void call_memmove(void) {
7583
char s1[10], s2[20];
7684
__builtin_memmove(s2, s1, 20);

0 commit comments

Comments
 (0)