diff mbox

[i-g-t,2/3] Remove extra '\0' in strncpy in igt_save_module_param

Message ID 0d8325c242cc19f74149bfe721e395bf4de951df.1527639529.git.rodrigosiqueiramelo@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Rodrigo Siqueira May 30, 2018, 12:46 a.m. UTC
This patch fix the following gcc warning:

warning: ‘strncpy’ specified bound 32 equals destination size
[-Wstringop-truncation]
  strncpy(data->name, name, PARAM_NAME_MAX_SZ);

This error happens due to the '\0' character appended by strncpy. Notice
that reduces by one in the total of bytes to be copied, in this case, is
harmless because the strings received in the parameter already have
'\0'.

Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
---
 lib/igt_aux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Arkadiusz Hiler June 4, 2018, 11:04 a.m. UTC | #1
Title is slightly missleading. We don't remove extra NULL characer from
anywhere. We just account for it by subtracting 1 from the destination
buffer size.

So maybe "Account for NULL character when using strncpy"?

On Tue, May 29, 2018 at 09:46:55PM -0300, Rodrigo Siqueira wrote:
> This patch fix the following gcc warning:
> 
> warning: ‘strncpy’ specified bound 32 equals destination size
> [-Wstringop-truncation]
>   strncpy(data->name, name, PARAM_NAME_MAX_SZ);
> 
> This error happens due to the '\0' character appended by strncpy. Notice
> that reduces by one in the total of bytes to be copied, in this case, is
> harmless because the strings received in the parameter already have
> '\0'.
> 
> Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
> ---
>  lib/igt_aux.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/igt_aux.c b/lib/igt_aux.c
> index 5dd2761e..06f586d6 100644
> --- a/lib/igt_aux.c
> +++ b/lib/igt_aux.c
> @@ -1240,7 +1240,7 @@ static void igt_save_module_param(const char *name, const char *file_path)
>  	data = calloc(1, sizeof (*data));
>  	igt_assert(data);
>  
> -	strncpy(data->name, name, PARAM_NAME_MAX_SZ);
> +	strncpy(data->name, name, PARAM_NAME_MAX_SZ - 1); // -1 because of '\0'

I am not necessary sure that the comment is needed, as "buflen - 1" is
quite typical.
diff mbox

Patch

diff --git a/lib/igt_aux.c b/lib/igt_aux.c
index 5dd2761e..06f586d6 100644
--- a/lib/igt_aux.c
+++ b/lib/igt_aux.c
@@ -1240,7 +1240,7 @@  static void igt_save_module_param(const char *name, const char *file_path)
 	data = calloc(1, sizeof (*data));
 	igt_assert(data);
 
-	strncpy(data->name, name, PARAM_NAME_MAX_SZ);
+	strncpy(data->name, name, PARAM_NAME_MAX_SZ - 1); // -1 because of '\0'
 
 	fd = open(file_path, O_RDONLY);
 	igt_assert(fd >= 0);