Skip to content

Commit 4572d72

Browse files
venk-ksaadeshps-mcw
authored andcommitted
[Clang][Sema] Add fortify warnings for strcat (llvm#168965)
Continue to add fortify warnings that are missing in Clang for string functions as part of llvm#142230
1 parent 512e8fb commit 4572d72

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
@@ -1263,6 +1263,8 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
12631263
switch (BuiltinID) {
12641264
default:
12651265
return;
1266+
case Builtin::BI__builtin_strcat:
1267+
case Builtin::BIstrcat:
12661268
case Builtin::BI__builtin_stpcpy:
12671269
case Builtin::BIstpcpy:
12681270
case Builtin::BI__builtin_strcpy:
@@ -1273,6 +1275,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
12731275
break;
12741276
}
12751277

1278+
case Builtin::BI__builtin___strcat_chk:
12761279
case Builtin::BI__builtin___stpcpy_chk:
12771280
case Builtin::BI__builtin___strcpy_chk: {
12781281
DiagID = diag::warn_fortify_strlen_overflow;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ void call_strcpy_nowarn(void) {
7676
__builtin_strcpy(dst, src);
7777
}
7878

79+
void call_strcat(void) {
80+
const char *const src = "abcd";
81+
char dst1[5];
82+
char dst2[4];
83+
__builtin_strcat(dst1, src);
84+
__builtin_strcat(dst2, src); // expected-warning {{'strcat' will always overflow; destination buffer has size 4, but the source string has length 5 (including NUL byte)}}
85+
}
86+
7987
void call_stpcpy(void) {
8088
const char *const src = "abcd";
8189
char dst1[5];

0 commit comments

Comments
 (0)