Skip to contentSkip to conversation

banned.h: mark strncat() as banned

PATCH In review Eric Wong 3 participants 3 messages Jan 2, 2019 – Jan 3, 2019

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

PATCH 1/1 banned.h: mark strncat() as banned

strncat() has the same quadratic behavior as strcat() and is difficult-to-read and bug-prone. While it hasn't yet been a problem in git iself, strncat() found it's way into 'master' of cgit and caused segfaults on my system.

---
 banned.h | 2 ++
 1 file changed, 2 insertions(+)
banned.h +2 -0
index 28f5937035..447af24807 100644
@@ -16,6 +16,8 @@
 #define strcat(x,y) BANNED(strcat)
 #undef strncpy
 #define strncpy(x,y,n) BANNED(strncpy)
+#undef strncat
+#define strncat(x,y,n) BANNED(strncat)
 
 #undef sprintf
 #undef vsprintf
Signature · 1 line
EW
#1 lore raw
Quoted 4 lines · Eric Wong

strncat() has the same quadratic behavior as strcat() and is difficult-to-read and bug-prone. While it hasn't yet been a problem in git iself, strncat() found it's way into 'master'

s/iself/itself/

Quoted 3 lines

of cgit and caused segfaults on my system.

#2 lore raw
In reply to Eric Wong
Quoted 4 lines · Eric Wong

strncat() has the same quadratic behavior as strcat() and is difficult-to-read and bug-prone. While it hasn't yet been a problem in git iself, strncat() found it's way into 'master' of cgit and caused segfaults on my system.

I'm in favor of this.

It doesn't have the "oops, I didn't NUL-terminate for you" problem that strncpy() has. But it actually has the opposite problem! It will always place a NUL, and you have to feed it sizeof(dst)-1 to avoid an overflow.

So I think it's important for safety (though I'd be fine banning it on the quadratic grounds alone ;) ).

-Peff