[dpdk-dev] app/procinfo: fix strncpy count issue

Message ID 1519131456-9928-1-git-send-email-radu.nicolau@intel.com (mailing list archive)
State Superseded, archived
Headers

Checks

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

Commit Message

Radu Nicolau Feb. 20, 2018, 12:57 p.m. UTC
  Change strncpy count parameter to MAX_LONG_OPT_SZ-1 to avoid
overwriting the last NULL character and for consistency.

Fixes: 2deb6b5246d7 ("app/procinfo: add collectd format and host id")
Coverity issue: 30688

Cc: stable@dpdk.org

Signed-off-by: Radu Nicolau <radu.nicolau@intel.com>
---
 app/proc_info/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Bruce Richardson Feb. 20, 2018, 1:29 p.m. UTC | #1
On Tue, Feb 20, 2018 at 12:57:36PM +0000, Radu Nicolau wrote:
> Change strncpy count parameter to MAX_LONG_OPT_SZ-1 to avoid
> overwriting the last NULL character and for consistency.
> 
> Fixes: 2deb6b5246d7 ("app/procinfo: add collectd format and host id")
> Coverity issue: 30688
> 
> Cc: stable@dpdk.org
> 
> Signed-off-by: Radu Nicolau <radu.nicolau@intel.com>
> ---
strncpy is a very nasty function that is easy to get wrong. Rather than
fixing it's off by one errors, I think a better fix is to use snprintf
as is done in most other places.

/Bruce
  

Patch

diff --git a/app/proc_info/main.c b/app/proc_info/main.c
index 2f53e3c..5f0b745 100644
--- a/app/proc_info/main.c
+++ b/app/proc_info/main.c
@@ -159,7 +159,7 @@  proc_info_preparse_args(int argc, char **argv)
 				proc_info_usage(prgname);
 				return -1;
 			}
-			strncpy(host_id, argv[i+1], sizeof(host_id));
+			strncpy(host_id, argv[i+1], MAX_LONG_OPT_SZ-1);
 		}
 	}