From: Laszlo Ersek Date: Thu, 10 Apr 2014 08:24:30 +0000 (+0200) Subject: cutils: tighten qemu_parse_fd() X-Git-Tag: TizenStudio_2.0_p2.3.2~208^2~858^2~32 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e9c5c1f40c949c5d2d7e1eeddf3caaed32e1c641;p=sdk%2Femulator%2Fqemu.git cutils: tighten qemu_parse_fd() qemu_parse_fd() used to handle at least the following strings incorrectly: o "-2": simply let through o "2147483648": returned as LONG_MAX==INT_MAX on ILP32 (with ERANGE ignored); implementation-defined behavior on LP64 Signed-off-by: Laszlo Ersek Reviewed-by: Eric Blake Signed-off-by: Luiz Capitulino --- diff --git a/util/cutils.c b/util/cutils.c index b337293..dbe7412 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -24,6 +24,8 @@ #include "qemu-common.h" #include "qemu/host-utils.h" #include +#include +#include #include "qemu/sockets.h" #include "qemu/iov.h" @@ -457,11 +459,16 @@ int parse_uint_full(const char *s, unsigned long long *value, int base) int qemu_parse_fd(const char *param) { - int fd; - char *endptr = NULL; + long fd; + char *endptr; + errno = 0; fd = strtol(param, &endptr, 10); - if (*endptr || (fd == 0 && param == endptr)) { + if (param == endptr /* no conversion performed */ || + errno != 0 /* not representable as long; possibly others */ || + *endptr != '\0' /* final string not empty */ || + fd < 0 /* invalid as file descriptor */ || + fd > INT_MAX /* not representable as int */) { return -1; } return fd;