[dpdk-dev] proc-info: wrong sizeof argument in malloc function

Message ID 1494307409-20019-1-git-send-email-kubax.kozak@intel.com (mailing list archive)
State Accepted, archived
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Kuba Kozak May 9, 2017, 5:23 a.m. UTC
  From: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>

Coverity reported that an argument for sizeof was used improperly. 
We should allocate memory for value size that pointer points to,
instead of pointer size itself. 

Coverity issue: 144523, 144521
Fixes: 7ac16a3660c0 ("app/proc-info: support xstats by ID and by name")

Signed-off-by: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>
---
 app/proc_info/main.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Van Haaren, Harry May 9, 2017, 8:44 a.m. UTC | #1
> From: Kozak, KubaX
> Sent: Tuesday, May 9, 2017 6:23 AM
> To: dev@dpdk.org
> Cc: Van Haaren, Harry <harry.van.haaren@intel.com>; Jain, Deepak K <deepak.k.jain@intel.com>;
> Jastrzebski, MichalX K <michalx.k.jastrzebski@intel.com>; Kozak, KubaX <kubax.kozak@intel.com>
> Subject: [PATCH] proc-info: wrong sizeof argument in malloc function
> 
> From: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>
> 
> Coverity reported that an argument for sizeof was used improperly.
> We should allocate memory for value size that pointer points to,
> instead of pointer size itself.
> 
> Coverity issue: 144523, 144521
> Fixes: 7ac16a3660c0 ("app/proc-info: support xstats by ID and by name")
> 
> Signed-off-by: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>


Please consider merging these in 17.05, this is an important fix for 32 bit systems.

Acked-by: Harry van Haaren <harry.van.haaren@intel.com>



Details:
64 bit system: sizeof(uint64_t*) == sizeof(uint64_t)
32 bit system: sizeof(uint64_t*) != sizeof(uint64_t)

uint64_t *values;

> -	values = malloc(sizeof(values) * len);
> +	values = malloc(sizeof(*values) * len);

The change here ensures that we allocate sizeof(uint64_t) * len, instead of the incorrect sizeof(void *) * len.

Previously on 32 bit systems, only half of the memory required would be allocated.
  
Thomas Monjalon May 10, 2017, 4:50 p.m. UTC | #2
09/05/2017 10:44, Van Haaren, Harry:
> > From: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>
> > 
> > Coverity reported that an argument for sizeof was used improperly.
> > We should allocate memory for value size that pointer points to,
> > instead of pointer size itself.
> > 
> > Coverity issue: 144523, 144521
> > Fixes: 7ac16a3660c0 ("app/proc-info: support xstats by ID and by name")
> > 
> > Signed-off-by: Michal Jastrzebski <michalx.k.jastrzebski@intel.com>
> 
> 
> Please consider merging these in 17.05, this is an important fix for 32 bit systems.
> 
> Acked-by: Harry van Haaren <harry.van.haaren@intel.com>

Applied, thanks
  

Patch

diff --git a/app/proc_info/main.c b/app/proc_info/main.c
index 17a1c87..d4f6a82 100644
--- a/app/proc_info/main.c
+++ b/app/proc_info/main.c
@@ -434,7 +434,7 @@  static void collectd_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
 	int ret, i;
 	static const char *nic_stats_border = "########################";
 
-	values = malloc(sizeof(values) * len);
+	values = malloc(sizeof(*values) * len);
 	if (values == NULL) {
 		printf("Cannot allocate memory for xstats\n");
 		return;
@@ -486,7 +486,7 @@  static void collectd_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
 		printf("Cannot get xstats count\n");
 		return;
 	}
-	values = malloc(sizeof(values) * len);
+	values = malloc(sizeof(*values) * len);
 	if (values == NULL) {
 		printf("Cannot allocate memory for xstats\n");
 		return;