inet_listen_opts: add error checking
PATCH 1/1 inet_listen_opts: add error checking
Don't use atoi() function which doesn't detect errors, switch to strtol and error out on failures. Also add a range check while being at it.
--- util/qemu-sockets.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
util/qemu-sockets.c +13 -2
@@ -133,8 +133,19 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) ai.ai_family =PF_INET6 ;/* lookup */ -if (port_offset) -snprintf (port,sizeof (port),"%d" ,atoi (port) + port_offset); +if (port_offset) { +unsigned long long baseport; +if (parse_uint_full (port, &baseport,10 ) <0 ) { +error_setg (errp,"can't convert to a number: %s" , port); +return -1 ; + } +if (base_port >65535 || + baseport + port_offset >65535 ) { +error_setg (errp,"port %s out of range" , port); +return -1 ; + } +snprintf (port,sizeof (port),"%d" , (int )baseport + port_offset); + } rc =getaddrinfo (strlen (addr) ? addr :NULL , port, &ai, &res);if (rc !=0 ) { error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
Signature · 1 line
1.8.3.1
Quoted 5 lines · Gerd Hoffmann
Don't use
atoi()function which doesn't detect errors, switch to strtol and error out on failures. Also add a range check while being at it.Signed-off-by Gerd Hoffmann <[email protected]>
Quoted 23 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Gerd Hoffmann
Sent: Wednesday, May 21, 2014 6:53 PM
To: [email protected]
Cc: Gerd Hoffmann
Subject: [Qemu-devel] [PATCH]inet_listen_opts: add error checkingDon't use
atoi()function which doesn't detect errors, switch to strtol and error out on failures. Also add a range check while being at it.Signed-off-by Gerd Hoffmann <[email protected]>--- util/qemu-sockets.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)util/qemu-sockets.c +0
-0
util/qemu-sockets.c int inet_listen_opts(QemuOpts *opts, int port_offset, · line 138
Quoted 8 lines
util/qemu-sockets.c +2
-2@@ -133,8 +133,19 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) ai.ai_family = PF_INET6;/* lookup */ -if (port_offset) -snprintf (port,sizeof (port),"%d" ,atoi (port) + port_offset); +if (port_offset) { +unsigned long long baseport;
unsigned long long is not necessary, unsigned long is enough.
util/qemu-sockets.c int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) · line 142
Quoted 5 lines
util/qemu-sockets.c +5
-0+if (parse_uint_full (port, &baseport,10 ) <0 ) { +error_setg (errp,"can't convert to a number: %s" , port); +return -1 ; + } + if (base_port > 65535 ||
base_port is undeclared.
Quoted 8 lines
util/qemu-sockets.c +6
-0+ baseport + port_offset > 65535) { +error_setg (errp,"port %s out of range" , port); +return -1 ; + } +snprintf (port,sizeof (port),"%d" , (int )baseport + port_offset); + } rc =getaddrinfo (strlen (addr) ? addr :NULL , port, &ai, &res);if (rc !=0 ) {
Quoted 5 lines
error_setg(errp, "address resolution failed for %s:%s: %s", addr,
port,
--
1.8.3.1
Best regards,
-Gonglei
Quoted 9 lines
util/qemu-sockets.c int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) · line 137
Quoted 5 lines
/* lookup */util/qemu-sockets.c +2
-2-if (port_offset) -snprintf (port,sizeof (port),"%d" ,atoi (port) + port_offset); +if (port_offset) { +unsigned long long baseport;unsigned long long is not necessary, unsigned long is enough.
Quoted 1 line
+ if (
parse_uint_full(port, &baseport, 10)< 0) {
parse_uint_full prototype uses "unsigned long long". And on 32bit machines it actually is a difference (long long is 64bit integer whereas long is 32bit only).
Quoted 6 lines
util/qemu-sockets.c int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) · line 142
Quoted 4 lines
util/qemu-sockets.c +4
-0+error_setg (errp,"can't convert to a number: %s" , port); +return -1 ; + } + if (base_port > 65535 ||
base_portis undeclared.
Oops, I'll fix.
cheers,
Gerd
Quoted 13 lines
-----Original Message-----
From: Gerd Hoffmann [mailto:[email protected]]
Sent: Thursday, May 22, 2014 1:44 PM
To: Gonglei (Arei)
Cc: [email protected]
Subject: Re: [Qemu-devel] [PATCH]inet_listen_opts: add error checkingQuoted 6 lines
Quoted 5 lines
/* lookup */util/qemu-sockets.c +2
-2-if (port_offset) -snprintf (port,sizeof (port),"%d" ,atoi (port) + port_offset); +if (port_offset) { +unsigned long long baseport;
Quoted 8 lines
Quoted 3 lines
unsigned long long is not necessary, unsigned long is enough.
Quoted 1 line
+ if (
parse_uint_full(port, &baseport, 10)< 0) {
parse_uint_fullprototype uses "unsigned long long". And on 32bit machines it actually is a difference (long long is 64bit integer whereas long is 32bit only).
Okay, Thanks.
Best regards,
-Gonglei