mem: memory leaks of hubedir caused by strdup
PATCH 1/1 mem: memory leaks of hubedir caused by strdup
--- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 18 +++++++++--------- 1 files changed, 9 insertions(+), 9 deletions(-)
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +9 -9
@@ -169,8 +169,8 @@return size; } -static const char * -get_hugepage_dir (uint64_t hugepage_sz) +static int +get_hugepage_dir (uint64_t hugepage_sz,char *hugedir,int len) {enum proc_mount_fieldnames {DEVICE =0 ,
@@ -188,7 +188,7 @@const char split_tok =' ' ;char *splitstr[_FIELDNAME_MAX];char buf[BUFSIZ ]; -char *retval =NULL ; +int retval =-1 ;FILE *fd =fopen (proc_mounts,"r" );if (fd ==NULL )
@@ -215,7 +215,8 @@/* if no explicit page size, the default page size is compared */ if (pagesz_str ==NULL ){if (hugepage_sz == default_size){ - retval =strdup (splitstr[MOUNTPT ]); +snprintf (hugedir, len,"%s" , splitstr[MOUNTPT ]); + retval =0 ;break ; } }
@@ -223,7 +224,8 @@else {uint64_t pagesz =rte_str_to_size (&pagesz_str[pagesize_opt_len]);if (pagesz == hugepage_sz) { - retval =strdup (splitstr[MOUNTPT ]); +snprintf (hugedir, len,"%s" , splitstr[MOUNTPT ]); + retval =0 ;break ; } }
@@ -351,7 +353,6 @@for (dirent =readdir (dir); dirent !=NULL ; dirent =readdir (dir)) {struct hugepage_info *hpi; -const char *hugedir;if (strncmp (dirent->d_name, dirent_start_text, dirent_start_len) !=0 )
@@ -363,10 +364,10 @@ hpi = &internal_config.hugepage_info[num_sizes]; hpi->hugepage_sz =rte_str_to_size (&dirent->d_name[dirent_start_len]); - hugedir =get_hugepage_dir (hpi->hugepage_sz);/* first, check if we have a mountpoint */ -if (hugedir ==NULL ) { +if (get_hugepage_dir (hpi->hugepage_sz, + hpi->hugedir,sizeof (hpi->hugedir)) <0 ) {uint32_t num_pages; num_pages =get_num_hugepages (dirent->d_name);
@@ -378,7 +379,6 @@ num_pages, hpi->hugepage_sz);continue ; } -snprintf (hpi->hugedir,sizeof (hpi->hugedir),"%s" , hugedir);/* try to obtain a writelock */ hpi->lock_descriptor =open (hpi->hugedir,O_RDONLY );
Signature · 1 line
1.7.1
Quoted 4 lines · Yangchao Zhou
Fixes cb97d93e9d3bb5607681085d20acaca1aa16deb1("mem: share hugepage info primary and secondary")Signed-off-by Yangchao Zhou <[email protected]>---
Please use proper formatting for "fixes" line (refer to DPDK docs for correct way to generate it). Also, this fixes a coverity defect, please also add the following line:
Coverity issue: 272585
That said, for actual patch contents,
Thanks for fixing this!
Signature · 2 lines
Thanks, Anatoly
PATCH v2 1/1 mem: memory leaks of hubedir caused by strdup
Coverity issue: 272585
--- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 18 +++++++++--------- 1 files changed, 9 insertions(+), 9 deletions(-)
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +9 -9
@@ -169,8 +169,8 @@return size; } -static const char * -get_hugepage_dir (uint64_t hugepage_sz) +static int +get_hugepage_dir (uint64_t hugepage_sz,char *hugedir,int len) {enum proc_mount_fieldnames {DEVICE =0 ,
@@ -188,7 +188,7 @@const char split_tok =' ' ;char *splitstr[_FIELDNAME_MAX];char buf[BUFSIZ ]; -char *retval =NULL ; +int retval =-1 ;FILE *fd =fopen (proc_mounts,"r" );if (fd ==NULL )
@@ -215,7 +215,8 @@/* if no explicit page size, the default page size is compared */ if (pagesz_str ==NULL ){if (hugepage_sz == default_size){ - retval =strdup (splitstr[MOUNTPT ]); +snprintf (hugedir, len,"%s" , splitstr[MOUNTPT ]); + retval =0 ;break ; } }
@@ -223,7 +224,8 @@else {uint64_t pagesz =rte_str_to_size (&pagesz_str[pagesize_opt_len]);if (pagesz == hugepage_sz) { - retval =strdup (splitstr[MOUNTPT ]); +snprintf (hugedir, len,"%s" , splitstr[MOUNTPT ]); + retval =0 ;break ; } }
@@ -351,7 +353,6 @@for (dirent =readdir (dir); dirent !=NULL ; dirent =readdir (dir)) {struct hugepage_info *hpi; -const char *hugedir;if (strncmp (dirent->d_name, dirent_start_text, dirent_start_len) !=0 )
@@ -363,10 +364,10 @@ hpi = &internal_config.hugepage_info[num_sizes]; hpi->hugepage_sz =rte_str_to_size (&dirent->d_name[dirent_start_len]); - hugedir =get_hugepage_dir (hpi->hugepage_sz);/* first, check if we have a mountpoint */ -if (hugedir ==NULL ) { +if (get_hugepage_dir (hpi->hugepage_sz, + hpi->hugedir,sizeof (hpi->hugedir)) <0 ) {uint32_t num_pages; num_pages =get_num_hugepages (dirent->d_name);
@@ -378,7 +379,6 @@ num_pages, hpi->hugepage_sz);continue ; } -snprintf (hpi->hugedir,sizeof (hpi->hugedir),"%s" , hugedir);/* try to obtain a writelock */ hpi->lock_descriptor =open (hpi->hugedir,O_RDONLY );
Signature · 1 line
1.7.1
17/04/2018 12:06, Yangchao Zhou:
Quoted 5 lines
Coverity issue: 272585
Fixes cb97d93e9d3b ("mem: share hugepage info primary and secondary")Signed-off-by Yangchao Zhou <[email protected]>Acked-by Anatoly Burakov <[email protected]>
Better to provide a small explanation.
Review
Quoted 2 lines
(quoted diff) +1
-1- retval = strdup(splitstr[MOUNTPT]); + snprintf(hugedir, len, "%s", splitstr[MOUNTPT]);
I think it is candidate to be replaced by strlcpy. Please check
Quoted 7 lines · Thomas Monjalon
17/04/2018 12:06, Yangchao Zhou:
Quoted 5 lines
Coverity issue: 272585
Fixes cb97d93e9d3b ("mem: share hugepage info primary and secondary")Signed-off-by Yangchao Zhou <[email protected]>Acked-by Anatoly Burakov <[email protected]>
Quoted 8 lines
Better to provide a small explanation.
Review
Quoted 2 lines
(quoted diff) +1
-1- retval = strdup(splitstr[MOUNTPT]); + snprintf(hugedir, len, "%s", splitstr[MOUNTPT]);I think it is candidate to be replaced by strlcpy. Please check
Yes, it seems that strlcpy thingie was merged without much fanfare. I'll be submitting a patch fixing various usages of snprintf in my recent commits. I'm inclined to leave this as is for this commit, as it's not the purpose of this fix.
Signature · 2 lines
Thanks, Anatoly
As Burakov said, for no other reason, I just followed the old version.
Quoted 27 lines · Anatoly
Quoted 11 lines · Thomas Monjalon
17/04/2018 12:06, Yangchao Zhou:
Quoted 5 lines
Coverity issue: 272585
Fixes cb97d93e9d3b ("mem: share hugepage info primary and secondary")Signed-off-by Yangchao Zhou <[email protected]>Acked-by Anatoly Burakov <[email protected]>Better to provide a small explanation.
Quoted 2 lines
(quoted diff) +1
-1- retval = strdup(splitstr[MOUNTPT]); + snprintf(hugedir, len, "%s",splitstr[MOUNTPT]);
Quoted 4 lines
I think it is candidate to be replaced by strlcpy. Please check
Yes, it seems that strlcpy thingie was merged without much fanfare. I'll be submitting a patch fixing various usages of snprintf in my recent commits. I'm inclined to leave this as is for this commit, as it's not the purpose of this fix.
--
Thanks,
Anatoly
I see no reason to accept this patch, replacing strdup by snprintf, given that we have strlcpy.
Please do a v3 with strlcpy.
17/04/2018 13:16, zhouyangchao:
Quoted 27 lines
As Burakov said, for no other reason, I just followed the old version.
Quoted 22 lines · Anatoly
Quoted 11 lines · Thomas Monjalon
17/04/2018 12:06, Yangchao Zhou:
Quoted 5 lines
Coverity issue: 272585
Fixes cb97d93e9d3b ("mem: share hugepage info primary and secondary")Signed-off-by Yangchao Zhou <[email protected]>Acked-by Anatoly Burakov <[email protected]>Better to provide a small explanation.
Quoted 2 lines
(quoted diff) +1
-1- retval = strdup(splitstr[MOUNTPT]); + snprintf(hugedir, len, "%s",splitstr[MOUNTPT]);
Quoted 4 lines
I think it is candidate to be replaced by strlcpy. Please check
Yes, it seems that strlcpy thingie was merged without much fanfare. I'll be submitting a patch fixing various usages of snprintf in my recent commits. I'm inclined to leave this as is for this commit, as it's not the purpose of this fix.
Quoted 3 lines · Thomas Monjalon
I see no reason to accept this patch, replacing strdup by snprintf, given that we have strlcpy.
Please do a v3 with strlcpy.
OK. Please also fix the typo in patch headline :)
Signature · 2 lines
Thanks, Anatoly
PATCH v3 1/1 mem: fix memory leaks of hugedir and replace snprintf
The hugedir returned by get_hugepage_dir is allocated by strdup
but not released. Replace snprintf with a more suitable strlcpy.
Coverity issue: 272585
--- lib/librte_eal/bsdapp/eal/eal_hugepage_info.c | 2 +- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 18 +++++++++--------- lib/librte_eal/linuxapp/eal/eal_memory.c | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-)
lib/librte_eal/bsdapp/eal/eal_hugepage_info.c +1 -1
@@ -96,7 +96,7 @@RTE_LOG (INFO ,EAL ,"Contigmem driver has %d buffers, each of size %dKB\n" , num_buffers, (int )(buffer_size>>10 )); -snprintf (hpi->hugedir,sizeof (hpi->hugedir),"%s" ,CONTIGMEM_DEV ); +strlcpy (hpi->hugedir,CONTIGMEM_DEV ,sizeof (hpi->hugedir)); hpi->hugepage_sz = buffer_size; hpi->num_pages[0 ] = num_buffers; hpi->lock_descriptor = fd;
lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +9 -9
@@ -169,8 +169,8 @@return size; } -static const char * -get_hugepage_dir (uint64_t hugepage_sz) +static int +get_hugepage_dir (uint64_t hugepage_sz,char *hugedir,int len) {enum proc_mount_fieldnames {DEVICE =0 ,
@@ -188,7 +188,7 @@const char split_tok =' ' ;char *splitstr[_FIELDNAME_MAX];char buf[BUFSIZ ]; -char *retval =NULL ; +int retval =-1 ;FILE *fd =fopen (proc_mounts,"r" );if (fd ==NULL )
@@ -215,7 +215,8 @@/* if no explicit page size, the default page size is compared */ if (pagesz_str ==NULL ){if (hugepage_sz == default_size){ - retval =strdup (splitstr[MOUNTPT ]); +strlcpy (hugedir, splitstr[MOUNTPT ], len); + retval =0 ;break ; } }
@@ -223,7 +224,8 @@else {uint64_t pagesz =rte_str_to_size (&pagesz_str[pagesize_opt_len]);if (pagesz == hugepage_sz) { - retval =strdup (splitstr[MOUNTPT ]); +strlcpy (hugedir, splitstr[MOUNTPT ], len); + retval =0 ;break ; } }
@@ -351,7 +353,6 @@for (dirent =readdir (dir); dirent !=NULL ; dirent =readdir (dir)) {struct hugepage_info *hpi; -const char *hugedir;if (strncmp (dirent->d_name, dirent_start_text, dirent_start_len) !=0 )
@@ -363,10 +364,10 @@ hpi = &internal_config.hugepage_info[num_sizes]; hpi->hugepage_sz =rte_str_to_size (&dirent->d_name[dirent_start_len]); - hugedir =get_hugepage_dir (hpi->hugepage_sz);/* first, check if we have a mountpoint */ -if (hugedir ==NULL ) { +if (get_hugepage_dir (hpi->hugepage_sz, + hpi->hugedir,sizeof (hpi->hugedir)) <0 ) {uint32_t num_pages; num_pages =get_num_hugepages (dirent->d_name);
@@ -378,7 +379,6 @@ num_pages, hpi->hugepage_sz);continue ; } -snprintf (hpi->hugedir,sizeof (hpi->hugedir),"%s" , hugedir);/* try to obtain a writelock */ hpi->lock_descriptor =open (hpi->hugedir,O_RDONLY );
lib/librte_eal/linuxapp/eal/eal_memory.c +2 -2
@@ -1160,8 +1160,8 @@ void numa_error(char *where)for (socket =0 ; socket <RTE_MAX_NUMA_NODES && total_mem !=0 ; socket++) {/* skips if the memory on specific socket wasn't requested */ for (i =0 ; i < num_hp_info && memory[socket] !=0 ; i++){ -snprintf (hp_used[i].hugedir,sizeof (hp_used[i].hugedir), -"%s" , hp_info[i].hugedir); +strlcpy (hp_used[i].hugedir, hp_info[i].hugedir, +sizeof (hp_used[i].hugedir)); hp_used[i].num_pages[socket] =RTE_MIN ( memory[socket] / hp_info[i].hugepage_sz, hp_info[i].num_pages[socket]);
Signature · 1 line
1.7.1
Thomas Monjalon
AppliedRe: [PATCH v3] mem: fix memory leaks of hugedir and replace snprintf
18/04/2018 05:09, Yangchao Zhou:
Quoted 8 lines
The hugedir returned by
get_hugepage_diris allocated by strdup
but not released. Replace snprintf with a more suitable strlcpy.Coverity issue: 272585
Fixes cb97d93e9d3b ("mem: share hugepage info primary and secondary")Signed-off-by Yangchao Zhou <[email protected]>Acked-by Anatoly Burakov <[email protected]>
There was a misunderstanding.
You replaced some snprintf which were not involved in the leaks. But I think it's fine.
Applied, thanks
Re: [PATCH v3] mem: fix memory leaks of hugedir and replace snprintf
Quoted 7 lines · Thomas Monjalon
18/04/2018 05:09, Yangchao Zhou:
Quoted 6 lines
The hugedir returned by
get_hugepage_diris allocated by strdup
but not released. Replace snprintf with a more suitable strlcpy.Coverity issue: 272585
Fixes cb97d93e9d3b ("mem: share hugepage info primary and secondary")
Quoted 9 lines
Quoted 2 lines
Signed-off-by Yangchao Zhou <[email protected]>Acked-by Anatoly Burakov <[email protected]>There was a misunderstanding.
You replaced some snprintf which were not involved in the leaks. But I think it's fine.Applied, thanks
I'll check to see if i have to rebase my strlcpy patchset.
Signature · 2 lines
Thanks, Anatoly