diff mbox series

lib: fwts_framework: use strlcpy instead of strncpy, fixes gcc-10 build errors

Message ID 20191104115239.137035-1-colin.king@canonical.com
State Accepted
Headers show
Series lib: fwts_framework: use strlcpy instead of strncpy, fixes gcc-10 build errors | expand

Commit Message

Colin Ian King Nov. 4, 2019, 11:52 a.m. UTC
From: Colin Ian King <colin.king@canonical.com>

In function ‘fwts_framework_strtrunc’,
    inlined from ‘fwts_framework_minor_test_progress’ at fwts_framework.c:470:3:
fwts_framework.c:374:3: error: ‘strncpy’ output may be truncated copying between 0 and 247 bytes from a string of length 1023 [-Werror=stringop-truncation]
  374 |   strncpy(dest, src, max);
      |   ^~~~~~~~~~~~~~~~~~~~~~~
In function ‘fwts_framework_strtrunc’,
    inlined from ‘fwts_framework_run_test.isra’ at fwts_framework.c:596:4:
fwts_framework.c:374:3: error: ‘strncpy’ specified bound 1024 equals destination size [-Werror=stringop-truncation]
  374 |   strncpy(dest, src, max);
      |   ^~~~~~~~~~~~~~~~~~~~~~~
In function ‘fwts_framework_strtrunc’,
    inlined from ‘fwts_framework_run_test.isra’ at fwts_framework.c:695:4:
fwts_framework.c:374:3: error: ‘strncpy’ specified bound 1024 equals destination size [-Werror=stringop-truncation]
  374 |   strncpy(dest, src, max);
      |   ^~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_framework.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Alex Hung Nov. 6, 2019, 5:05 p.m. UTC | #1
On 2019-11-04 4:52 a.m., Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> In function ‘fwts_framework_strtrunc’,
>     inlined from ‘fwts_framework_minor_test_progress’ at fwts_framework.c:470:3:
> fwts_framework.c:374:3: error: ‘strncpy’ output may be truncated copying between 0 and 247 bytes from a string of length 1023 [-Werror=stringop-truncation]
>   374 |   strncpy(dest, src, max);
>       |   ^~~~~~~~~~~~~~~~~~~~~~~
> In function ‘fwts_framework_strtrunc’,
>     inlined from ‘fwts_framework_run_test.isra’ at fwts_framework.c:596:4:
> fwts_framework.c:374:3: error: ‘strncpy’ specified bound 1024 equals destination size [-Werror=stringop-truncation]
>   374 |   strncpy(dest, src, max);
>       |   ^~~~~~~~~~~~~~~~~~~~~~~
> In function ‘fwts_framework_strtrunc’,
>     inlined from ‘fwts_framework_run_test.isra’ at fwts_framework.c:695:4:
> fwts_framework.c:374:3: error: ‘strncpy’ specified bound 1024 equals destination size [-Werror=stringop-truncation]
>   374 |   strncpy(dest, src, max);
>       |   ^~~~~~~~~~~~~~~~~~~~~~~
> cc1: all warnings being treated as errors
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_framework.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
> index 698fa49a..8bc7b0e0 100644
> --- a/src/lib/src/fwts_framework.c
> +++ b/src/lib/src/fwts_framework.c
> @@ -26,6 +26,7 @@
>  #include <ctype.h>
>  #include <time.h>
>  #include <getopt.h>
> +#include <bsd/string.h>
>  #include <sys/utsname.h>
>  #include <sys/time.h>
>  
> @@ -371,9 +372,9 @@ static void fwts_framework_show_tests(fwts_framework *fw, const bool full)
>  static void fwts_framework_strtrunc(char *dest, const char *src, size_t max)
>  {
>  	if (src) {
> -		strncpy(dest, src, max);
> +		strlcpy(dest, src, max);
>  		if ((max > 3) && (strlen(src) > max)) {
> -			dest[max-1] = 0;
> +			dest[max-1] = '\0';
>  			dest[max-2] = '.';
>  			dest[max-3] = '.';
>  		}
> 

Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu Nov. 7, 2019, 7:54 a.m. UTC | #2
On 11/4/19 7:52 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> In function ‘fwts_framework_strtrunc’,
>     inlined from ‘fwts_framework_minor_test_progress’ at fwts_framework.c:470:3:
> fwts_framework.c:374:3: error: ‘strncpy’ output may be truncated copying between 0 and 247 bytes from a string of length 1023 [-Werror=stringop-truncation]
>   374 |   strncpy(dest, src, max);
>       |   ^~~~~~~~~~~~~~~~~~~~~~~
> In function ‘fwts_framework_strtrunc’,
>     inlined from ‘fwts_framework_run_test.isra’ at fwts_framework.c:596:4:
> fwts_framework.c:374:3: error: ‘strncpy’ specified bound 1024 equals destination size [-Werror=stringop-truncation]
>   374 |   strncpy(dest, src, max);
>       |   ^~~~~~~~~~~~~~~~~~~~~~~
> In function ‘fwts_framework_strtrunc’,
>     inlined from ‘fwts_framework_run_test.isra’ at fwts_framework.c:695:4:
> fwts_framework.c:374:3: error: ‘strncpy’ specified bound 1024 equals destination size [-Werror=stringop-truncation]
>   374 |   strncpy(dest, src, max);
>       |   ^~~~~~~~~~~~~~~~~~~~~~~
> cc1: all warnings being treated as errors
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_framework.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
> index 698fa49a..8bc7b0e0 100644
> --- a/src/lib/src/fwts_framework.c
> +++ b/src/lib/src/fwts_framework.c
> @@ -26,6 +26,7 @@
>  #include <ctype.h>
>  #include <time.h>
>  #include <getopt.h>
> +#include <bsd/string.h>
>  #include <sys/utsname.h>
>  #include <sys/time.h>
>  
> @@ -371,9 +372,9 @@ static void fwts_framework_show_tests(fwts_framework *fw, const bool full)
>  static void fwts_framework_strtrunc(char *dest, const char *src, size_t max)
>  {
>  	if (src) {
> -		strncpy(dest, src, max);
> +		strlcpy(dest, src, max);
>  		if ((max > 3) && (strlen(src) > max)) {
> -			dest[max-1] = 0;
> +			dest[max-1] = '\0';
>  			dest[max-2] = '.';
>  			dest[max-3] = '.';
>  		}

Acked-by: Ivan Hu <ivan.hu@canonical.com>
diff mbox series

Patch

diff --git a/src/lib/src/fwts_framework.c b/src/lib/src/fwts_framework.c
index 698fa49a..8bc7b0e0 100644
--- a/src/lib/src/fwts_framework.c
+++ b/src/lib/src/fwts_framework.c
@@ -26,6 +26,7 @@ 
 #include <ctype.h>
 #include <time.h>
 #include <getopt.h>
+#include <bsd/string.h>
 #include <sys/utsname.h>
 #include <sys/time.h>
 
@@ -371,9 +372,9 @@  static void fwts_framework_show_tests(fwts_framework *fw, const bool full)
 static void fwts_framework_strtrunc(char *dest, const char *src, size_t max)
 {
 	if (src) {
-		strncpy(dest, src, max);
+		strlcpy(dest, src, max);
 		if ((max > 3) && (strlen(src) > max)) {
-			dest[max-1] = 0;
+			dest[max-1] = '\0';
 			dest[max-2] = '.';
 			dest[max-3] = '.';
 		}