[dpdk-dev,v3] examples/ipsec-secgw: replace strncpy with strlcpy

Message ID 1525882266-2161-1-git-send-email-reshma.pattan@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers

Checks

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

Commit Message

Pattan, Reshma May 9, 2018, 4:11 p.m. UTC
  Use strlcpy instead of strncpy.
Use strcpy where boundchecks on destination is not needed.

Fixes: 0d547ed037 ("examples/ipsec-secgw: support configuration file")
Fixes: 07b156199f ("examples/ipsec-secgw: fix configuration string termination")
Fixes: a1469c319f ("examples/ipsec-secgw: fix configuration parsing")
Cc: stable@dpdk.org
CC: Zhang,Roy Fan <roy.fan.zhang@intel.com>

Signed-off-by: Reshma Pattan <reshma.pattan@intel.com>
---
v3: fix checkpatch warnings.
---
 examples/ipsec-secgw/parser.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
  

Comments

De Lara Guarch, Pablo May 11, 2018, 4:38 p.m. UTC | #1
> -----Original Message-----
> From: stable [mailto:stable-bounces@dpdk.org] On Behalf Of Reshma Pattan
> Sent: Wednesday, May 9, 2018 5:11 PM
> To: dev@dpdk.org
> Cc: Richardson, Bruce <bruce.richardson@intel.com>; stable@dpdk.org; Zhang,
> Roy Fan <roy.fan.zhang@intel.com>; Pattan, Reshma
> <reshma.pattan@intel.com>
> Subject: [dpdk-stable] [PATCH v3] examples/ipsec-secgw: replace strncpy with
> strlcpy
> 
> Use strlcpy instead of strncpy.
> Use strcpy where boundchecks on destination is not needed.
> 
> Fixes: 0d547ed037 ("examples/ipsec-secgw: support configuration file")
> Fixes: 07b156199f ("examples/ipsec-secgw: fix configuration string
> termination")
> Fixes: a1469c319f ("examples/ipsec-secgw: fix configuration parsing")
> Cc: stable@dpdk.org
> CC: Zhang,Roy Fan <roy.fan.zhang@intel.com>
> 
> Signed-off-by: Reshma Pattan <reshma.pattan@intel.com>

Acked-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
  
Thomas Monjalon May 13, 2018, 9:52 p.m. UTC | #2
> > Use strlcpy instead of strncpy.
> > Use strcpy where boundchecks on destination is not needed.
> > 
> > Fixes: 0d547ed037 ("examples/ipsec-secgw: support configuration file")
> > Fixes: 07b156199f ("examples/ipsec-secgw: fix configuration string
> > termination")
> > Fixes: a1469c319f ("examples/ipsec-secgw: fix configuration parsing")
> > Cc: stable@dpdk.org
> > CC: Zhang,Roy Fan <roy.fan.zhang@intel.com>
> > 
> > Signed-off-by: Reshma Pattan <reshma.pattan@intel.com>
> 
> Acked-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>

Applied, thanks
  

Patch

diff --git a/examples/ipsec-secgw/parser.c b/examples/ipsec-secgw/parser.c
index 2403b564d..e2e342908 100644
--- a/examples/ipsec-secgw/parser.c
+++ b/examples/ipsec-secgw/parser.c
@@ -3,6 +3,7 @@ 
  */
 #include <rte_common.h>
 #include <rte_crypto.h>
+#include <rte_string_fns.h>
 
 #include <cmdline_parse_string.h>
 #include <cmdline_parse_num.h>
@@ -207,19 +208,20 @@  inet_pton6(const char *src, unsigned char *dst)
 int
 parse_ipv4_addr(const char *token, struct in_addr *ipv4, uint32_t *mask)
 {
-	char ip_str[256] = {0};
+	char ip_str[INET_ADDRSTRLEN] = {0};
 	char *pch;
 
 	pch = strchr(token, '/');
 	if (pch != NULL) {
-		strncpy(ip_str, token, pch - token);
+		strlcpy(ip_str, token, RTE_MIN((unsigned int long)(pch - token),
+					sizeof(ip_str)));
 		pch += 1;
 		if (is_str_num(pch) != 0)
 			return -EINVAL;
 		if (mask)
 			*mask = atoi(pch);
 	} else {
-		strncpy(ip_str, token, sizeof(ip_str) - 1);
+		strlcpy(ip_str, token, sizeof(ip_str));
 		if (mask)
 			*mask = 0;
 	}
@@ -241,14 +243,15 @@  parse_ipv6_addr(const char *token, struct in6_addr *ipv6, uint32_t *mask)
 
 	pch = strchr(token, '/');
 	if (pch != NULL) {
-		strncpy(ip_str, token, pch - token);
+		strlcpy(ip_str, token, RTE_MIN((unsigned int long)(pch - token),
+					sizeof(ip_str)));
 		pch += 1;
 		if (is_str_num(pch) != 0)
 			return -EINVAL;
 		if (mask)
 			*mask = atoi(pch);
 	} else {
-		strncpy(ip_str, token, sizeof(ip_str) - 1);
+		strlcpy(ip_str, token, sizeof(ip_str));
 		if (mask)
 			*mask = 0;
 	}
@@ -515,9 +518,7 @@  parse_cfg_file(const char *cfg_filename)
 				goto error_exit;
 			}
 
-			strncpy(str + strlen(str), oneline,
-				strlen(oneline));
-
+			strcpy(str + strlen(str), oneline);
 			continue;
 		}
 
@@ -528,8 +529,7 @@  parse_cfg_file(const char *cfg_filename)
 				cfg_filename, line_num);
 			goto error_exit;
 		}
-		strncpy(str + strlen(str), oneline,
-			strlen(oneline));
+		strcpy(str + strlen(str), oneline);
 
 		str[strlen(str)] = '\n';
 		if (cmdline_parse(cl, str) < 0) {