diff mbox

linux-generic: fix usage of function strncpy

Message ID 1456219512-31696-1-git-send-email-hongbo.zhang@linaro.org
State Accepted
Commit 50333e89b6c35edfa1221a4fbb74fc34bea6865d
Headers show

Commit Message

Hongbo Zhang Feb. 23, 2016, 9:25 a.m. UTC
From: Hongbo Zhang <hongbo.zhang@linaro.org>

This is for https://bugs.linaro.org/show_bug.cgi?id=2030:
"Memory - illegal accesses  (BUFFER_SIZE_WARNING)
Calling strncpy with a maximum size argument of 128 bytes on destination
array "sysinfo->model_str[id]" of size 128 bytes might leave the
destination string unterminated."

In fact in the following code there is operation like this:
sysinfo->model_str[id][len - 1] = 0
to handle the last character of string, but is is also good to eliminate
this coding warning.

Signed-off-by: Hongbo Zhang <hongbo.zhang@linaro.org>
---
 platform/linux-generic/arch/mips64/odp_sysinfo_parse.c  | 2 +-
 platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c | 2 +-
 platform/linux-generic/arch/x86/odp_sysinfo_parse.c     | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

Comments

Mike Holmes Feb. 29, 2016, 4:23 p.m. UTC | #1
On 23 February 2016 at 04:25, <hongbo.zhang@linaro.org> wrote:

> From: Hongbo Zhang <hongbo.zhang@linaro.org>

>


Reviewed-by: Mike Holmes <mike.holmes@linaro.org>



>

> This is for https://bugs.linaro.org/show_bug.cgi?id=2030:

> "Memory - illegal accesses  (BUFFER_SIZE_WARNING)

> Calling strncpy with a maximum size argument of 128 bytes on destination

> array "sysinfo->model_str[id]" of size 128 bytes might leave the

> destination string unterminated."

>

> In fact in the following code there is operation like this:

> sysinfo->model_str[id][len - 1] = 0

> to handle the last character of string, but is is also good to eliminate

> this coding warning.

>

> Signed-off-by: Hongbo Zhang <hongbo.zhang@linaro.org>

> ---

>  platform/linux-generic/arch/mips64/odp_sysinfo_parse.c  | 2 +-

>  platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c | 2 +-

>  platform/linux-generic/arch/x86/odp_sysinfo_parse.c     | 2 +-

>  3 files changed, 3 insertions(+), 3 deletions(-)

>

> diff --git a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c

> b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c

> index 53074f7..d45b420 100644

> --- a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c

> +++ b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c

> @@ -39,7 +39,7 @@ int odp_cpuinfo_parser(FILE *file, odp_system_info_t

> *sysinfo)

>

>                                 pos = strchr(str, ':');

>                                 strncpy(sysinfo->model_str[id], pos + 2,

> -                                       sizeof(sysinfo->model_str[id]));

> +                                       sizeof(sysinfo->model_str[id]) -

> 1);

>                                 len = strlen(sysinfo->model_str[id]);

>                                 sysinfo->model_str[id][len - 1] = 0;

>                                 model = 1;

> diff --git a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c

> b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c

> index 99457ce..95200ee 100644

> --- a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c

> +++ b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c

> @@ -38,7 +38,7 @@ int odp_cpuinfo_parser(FILE *file, odp_system_info_t

> *sysinfo)

>

>                                 pos = strchr(str, ':');

>                                 strncpy(sysinfo->model_str[id], pos + 2,

> -                                       sizeof(sysinfo->model_str[id]));

> +                                       sizeof(sysinfo->model_str[id]) -

> 1);

>                                 len = strlen(sysinfo->model_str[id]);

>                                 sysinfo->model_str[id][len - 1] = 0;

>                                 model = 1;

> diff --git a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c

> b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c

> index 816629d..2ef49e4 100644

> --- a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c

> +++ b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c

> @@ -21,7 +21,7 @@ int odp_cpuinfo_parser(FILE *file, odp_system_info_t

> *sysinfo)

>                 if (pos) {

>                         pos = strchr(str, ':');

>                         strncpy(sysinfo->model_str[id], pos + 2,

> -                               sizeof(sysinfo->model_str[id]));

> +                               sizeof(sysinfo->model_str[id]) - 1);

>

>                         pos = strchr(sysinfo->model_str[id], '@');

>                         *(pos - 1) = '\0';

> --

> 2.1.4

>

> _______________________________________________

> lng-odp mailing list

> lng-odp@lists.linaro.org

> https://lists.linaro.org/mailman/listinfo/lng-odp

>




-- 
Mike Holmes
Technical Manager - Linaro Networking Group
Linaro.org <http://www.linaro.org/> *│ *Open source software for ARM SoCs
"Work should be fun and collborative, the rest follows"
Maxim Uvarov March 1, 2016, 3:39 p.m. UTC | #2
Merged,
Maxim.

On 02/29/16 19:23, Mike Holmes wrote:
>
>
> On 23 February 2016 at 04:25, <hongbo.zhang@linaro.org 
> <mailto:hongbo.zhang@linaro.org>> wrote:
>
>     From: Hongbo Zhang <hongbo.zhang@linaro.org
>     <mailto:hongbo.zhang@linaro.org>>
>
>
> Reviewed-by: Mike Holmes <mike.holmes@linaro.org 
> <mailto:mike.holmes@linaro.org>>
>
>
>     This is for https://bugs.linaro.org/show_bug.cgi?id=2030:
>     "Memory - illegal accesses  (BUFFER_SIZE_WARNING)
>     Calling strncpy with a maximum size argument of 128 bytes on
>     destination
>     array "sysinfo->model_str[id]" of size 128 bytes might leave the
>     destination string unterminated."
>
>     In fact in the following code there is operation like this:
>     sysinfo->model_str[id][len - 1] = 0
>     to handle the last character of string, but is is also good to
>     eliminate
>     this coding warning.
>
>     Signed-off-by: Hongbo Zhang <hongbo.zhang@linaro.org
>     <mailto:hongbo.zhang@linaro.org>>
>     ---
>      platform/linux-generic/arch/mips64/odp_sysinfo_parse.c  | 2 +-
>      platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c | 2 +-
>      platform/linux-generic/arch/x86/odp_sysinfo_parse.c     | 2 +-
>      3 files changed, 3 insertions(+), 3 deletions(-)
>
>     diff --git
>     a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
>     b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
>     index 53074f7..d45b420 100644
>     --- a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
>     +++ b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
>     @@ -39,7 +39,7 @@ int odp_cpuinfo_parser(FILE *file,
>     odp_system_info_t *sysinfo)
>
>                                     pos = strchr(str, ':');
>     strncpy(sysinfo->model_str[id], pos + 2,
>     -  sizeof(sysinfo->model_str[id]));
>     +  sizeof(sysinfo->model_str[id]) - 1);
>                                     len = strlen(sysinfo->model_str[id]);
>     sysinfo->model_str[id][len - 1] = 0;
>                                     model = 1;
>     diff --git
>     a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
>     b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
>     index 99457ce..95200ee 100644
>     --- a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
>     +++ b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
>     @@ -38,7 +38,7 @@ int odp_cpuinfo_parser(FILE *file,
>     odp_system_info_t *sysinfo)
>
>                                     pos = strchr(str, ':');
>     strncpy(sysinfo->model_str[id], pos + 2,
>     -  sizeof(sysinfo->model_str[id]));
>     +  sizeof(sysinfo->model_str[id]) - 1);
>                                     len = strlen(sysinfo->model_str[id]);
>     sysinfo->model_str[id][len - 1] = 0;
>                                     model = 1;
>     diff --git a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
>     b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
>     index 816629d..2ef49e4 100644
>     --- a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
>     +++ b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
>     @@ -21,7 +21,7 @@ int odp_cpuinfo_parser(FILE *file,
>     odp_system_info_t *sysinfo)
>                     if (pos) {
>                             pos = strchr(str, ':');
>                             strncpy(sysinfo->model_str[id], pos + 2,
>     -  sizeof(sysinfo->model_str[id]));
>     +  sizeof(sysinfo->model_str[id]) - 1);
>
>                             pos = strchr(sysinfo->model_str[id], '@');
>                             *(pos - 1) = '\0';
>     --
>     2.1.4
>
>     _______________________________________________
>     lng-odp mailing list
>     lng-odp@lists.linaro.org <mailto:lng-odp@lists.linaro.org>
>     https://lists.linaro.org/mailman/listinfo/lng-odp
>
>
>
>
> -- 
> Mike Holmes
> Technical Manager - Linaro Networking Group
> Linaro.org <http://www.linaro.org/>***│ *Open source software for ARM SoCs
> "Work should be fun and collborative, the rest follows"
>
>
>
> _______________________________________________
> lng-odp mailing list
> lng-odp@lists.linaro.org
> https://lists.linaro.org/mailman/listinfo/lng-odp
diff mbox

Patch

diff --git a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
index 53074f7..d45b420 100644
--- a/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
+++ b/platform/linux-generic/arch/mips64/odp_sysinfo_parse.c
@@ -39,7 +39,7 @@  int odp_cpuinfo_parser(FILE *file, odp_system_info_t *sysinfo)
 
 				pos = strchr(str, ':');
 				strncpy(sysinfo->model_str[id], pos + 2,
-					sizeof(sysinfo->model_str[id]));
+					sizeof(sysinfo->model_str[id]) - 1);
 				len = strlen(sysinfo->model_str[id]);
 				sysinfo->model_str[id][len - 1] = 0;
 				model = 1;
diff --git a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
index 99457ce..95200ee 100644
--- a/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
+++ b/platform/linux-generic/arch/powerpc/odp_sysinfo_parse.c
@@ -38,7 +38,7 @@  int odp_cpuinfo_parser(FILE *file, odp_system_info_t *sysinfo)
 
 				pos = strchr(str, ':');
 				strncpy(sysinfo->model_str[id], pos + 2,
-					sizeof(sysinfo->model_str[id]));
+					sizeof(sysinfo->model_str[id]) - 1);
 				len = strlen(sysinfo->model_str[id]);
 				sysinfo->model_str[id][len - 1] = 0;
 				model = 1;
diff --git a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
index 816629d..2ef49e4 100644
--- a/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
+++ b/platform/linux-generic/arch/x86/odp_sysinfo_parse.c
@@ -21,7 +21,7 @@  int odp_cpuinfo_parser(FILE *file, odp_system_info_t *sysinfo)
 		if (pos) {
 			pos = strchr(str, ':');
 			strncpy(sysinfo->model_str[id], pos + 2,
-				sizeof(sysinfo->model_str[id]));
+				sizeof(sysinfo->model_str[id]) - 1);
 
 			pos = strchr(sysinfo->model_str[id], '@');
 			*(pos - 1) = '\0';