mirror of https://github.com/xemu-project/xemu.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 <lersek@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
This commit is contained in:
parent
cb45de6798
commit
e9c5c1f40c
|
@ -24,6 +24,8 @@
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/host-utils.h"
|
#include "qemu/host-utils.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "qemu/sockets.h"
|
#include "qemu/sockets.h"
|
||||||
#include "qemu/iov.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 qemu_parse_fd(const char *param)
|
||||||
{
|
{
|
||||||
int fd;
|
long fd;
|
||||||
char *endptr = NULL;
|
char *endptr;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
fd = strtol(param, &endptr, 10);
|
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 -1;
|
||||||
}
|
}
|
||||||
return fd;
|
return fd;
|
||||||
|
|
Loading…
Reference in New Issue