parse-util: introduce parse_ifindex() and make use of it everywhere

We have enough places where we parse an ifindex, hence introduce a
proper parsing function for it, that verifies all parameters.
This commit is contained in:
Lennart Poettering
2015-11-03 00:02:00 +01:00
parent 6a37c68469
commit 6ad623a3f7
8 changed files with 22 additions and 18 deletions
+2 -7
View File
@@ -356,13 +356,10 @@ int device_set_ifindex(sd_device *device, const char *_ifindex) {
assert(device);
assert(_ifindex);
r = safe_atoi(_ifindex, &ifindex);
r = parse_ifindex(_ifindex, &ifindex);
if (r < 0)
return r;
if (ifindex <= 0)
return -EINVAL;
r = device_add_property_internal(device, "IFINDEX", _ifindex);
if (r < 0)
return r;
@@ -632,11 +629,9 @@ _public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) {
struct ifreq ifr = {};
int ifindex;
r = safe_atoi(&id[1], &ifr.ifr_ifindex);
r = parse_ifindex(&id[1], &ifr.ifr_ifindex);
if (r < 0)
return r;
else if (ifr.ifr_ifindex <= 0)
return -EINVAL;
sk = socket(PF_INET, SOCK_DGRAM, 0);
if (sk < 0)
+1 -3
View File
@@ -932,9 +932,7 @@ _public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
*(char*) (mempcpy(buf, word, l)) = 0;
if (safe_atoi(buf, &ifi) < 0)
continue;
if (ifi <= 0)
if (parse_ifindex(buf, &ifi) < 0)
continue;
if (!GREEDY_REALLOC(ni, allocated, nr+1)) {