From d3284d5d54e348fb24138ccedca9a43a698ec21e Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 26 Mar 2018 18:38:26 +0200 Subject: [PATCH] std.cfg: Add strncpy_s and strncat_s (#1136) --- cfg/std.cfg | 46 +++++++++++++++++++++++++++++++++++ test/cfg/std.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/cfg/std.cfg b/cfg/std.cfg index 716c345a4..ab155d205 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -4581,6 +4581,29 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun 0: + + + false + + + + + + + + + + 0: + + + + + + + + + + @@ -4618,6 +4641,29 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun 0: + + + false + + + + + + + + + + 0: + + + + + + + + + + false diff --git a/test/cfg/std.c b/test/cfg/std.c index 68951e530..59835eb4b 100644 --- a/test/cfg/std.c +++ b/test/cfg/std.c @@ -53,6 +53,19 @@ void bufferAccessOutOfBounds(void) strncpy(a,"a",6); // cppcheck-suppress redundantCopy strncpy(a,"abcdefgh",4); + // valid call + strncpy_s(a,5,"abcd",5); + // string will be truncated, error is returned, but no buffer overflow + strncpy_s(a,5,"abcde",6); + // cppcheck-suppress bufferAccessOutOfBounds + strncpy_s(a,5,"a",6); + strncpy_s(a,5,"abcdefgh",4); + // valid call + strncat_s(a,5,"1",2); + // cppcheck-suppress bufferAccessOutOfBounds + strncat_s(a,10,"1",2); + // cppcheck-suppress bufferAccessOutOfBounds + strncat_s(a,5,"1",5); fread(a,1,5,stdin); // cppcheck-suppress bufferAccessOutOfBounds fread(a,1,6,stdin); @@ -169,10 +182,18 @@ void nullpointer(int value) // cppcheck-suppress nullPointer strncpy(0,0,1); // cppcheck-suppress nullPointer + strncpy_s(0,1,1,1); + // cppcheck-suppress nullPointer + strncpy_s(1,1,0,1); + // cppcheck-suppress nullPointer wcsncpy(0,0,1); // cppcheck-suppress nullPointer strncat(0,0,1); // cppcheck-suppress nullPointer + strncat_s(0,1,1,1); + // cppcheck-suppress nullPointer + strncat_s(1,1,0,1); + // cppcheck-suppress nullPointer wcsncat(0,0,1); // cppcheck-suppress ignoredReturnValue // cppcheck-suppress nullPointer @@ -3022,6 +3043,28 @@ void uninivar_strncpy(void) (void)strncpy(s,ct,n); } +void uninivar_strncpy_s(char *Ct, size_t N1, char *S, size_t N2) +{ + char *ct; + char *s; + size_t n1; + size_t n2; + + // cppcheck-suppress uninitvar + (void)strncpy_s(ct,n1,s,n2); + // cppcheck-suppress uninitvar + (void)strncpy_s(ct,N1,S,N2); + // cppcheck-suppress uninitvar + (void)strncpy_s(Ct,n1,S,N2); + // cppcheck-suppress uninitvar + (void)strncpy_s(Ct,N1,s,N2); + // cppcheck-suppress uninitvar + (void)strncpy_s(Ct,N1,S,n2); + + // no warning is expected for + (void)strncpy_s(Ct,N1,S,N2); +} + void uninivar_strpbrk(void) { char *cs; @@ -3048,6 +3091,28 @@ void uninivar_strncat(char *Ct, char *S, size_t N) (void)strncat(Ct,S,N); } +void uninivar_strncat_s(char *Ct, size_t N1, char *S, size_t N2) +{ + char *ct; + char *s; + size_t n1; + size_t n2; + + // cppcheck-suppress uninitvar + (void)strncat_s(ct,n1,s,n2); + // cppcheck-suppress uninitvar + (void)strncat_s(ct,N1,S,N2); + // cppcheck-suppress uninitvar + (void)strncat_s(Ct,n1,S,N2); + // cppcheck-suppress uninitvar + (void)strncat_s(Ct,N1,s,N2); + // cppcheck-suppress uninitvar + (void)strncat_s(Ct,N1,S,n2); + + // no warning is expected for + (void)strncat_s(Ct,N1,S,N2); +} + void uninivar_wcsncat(wchar_t *Ct, wchar_t *S, size_t N) { wchar_t *ct;