diff mbox series

[iproute2,1/1] ip netns: use strtol() instead of atoi()

Message ID 1509123327-31751-1-git-send-email-mrv@mojatatu.com
State Changes Requested, archived
Delegated to: stephen hemminger
Headers show
Series [iproute2,1/1] ip netns: use strtol() instead of atoi() | expand

Commit Message

Roman Mashak Oct. 27, 2017, 4:55 p.m. UTC
Use strtol-based API to parse and validate integer input; atoi() does not detect
errors and may yield undefined behaviour if result can't be represented.

Signed-off-by: Roman Mashak <mrv@mojatatu.com>
---
 ip/ipnetns.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Stephen Hemminger Oct. 31, 2017, 4:31 p.m. UTC | #1
On Fri, 27 Oct 2017 12:55:27 -0400
Roman Mashak <mrv@mojatatu.com> wrote:

> Use strtol-based API to parse and validate integer input; atoi() does not detect
> errors and may yield undefined behaviour if result can't be represented.
> 
> Signed-off-by: Roman Mashak <mrv@mojatatu.com>

Since network namespace is really an unsigned value, shouldn't it be
using get_unsigned?
Roman Mashak Oct. 31, 2017, 6:01 p.m. UTC | #2
Stephen Hemminger <stephen@networkplumber.org> writes:

> On Fri, 27 Oct 2017 12:55:27 -0400
> Roman Mashak <mrv@mojatatu.com> wrote:
>
>> Use strtol-based API to parse and validate integer input; atoi() does not detect
>> errors and may yield undefined behaviour if result can't be represented.
>> 
>> Signed-off-by: Roman Mashak <mrv@mojatatu.com>
>
> Since network namespace is really an unsigned value, shouldn't it be
> using get_unsigned?

Agree, I will resend v2.
diff mbox series

Patch

diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index afb4978..96da040 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -711,7 +711,8 @@  static int netns_set(int argc, char **argv)
 		return -1;
 	}
 	name = argv[0];
-	nsid = atoi(argv[1]);
+	if (get_integer(&nsid, argv[1], 0))
+		invarg("Invalid \"netnsid\" value\n", argv[1]);
 
 	snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
 	netns = open(netns_path, O_RDONLY | O_CLOEXEC);