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:
@@ -81,6 +81,19 @@ int parse_mode(const char *s, mode_t *ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_ifindex(const char *s, int *ret) {
|
||||
int ifi, r;
|
||||
|
||||
r = safe_atoi(s, &ifi);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (ifi <= 0)
|
||||
return -EINVAL;
|
||||
|
||||
*ret = ifi;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_size(const char *t, uint64_t base, uint64_t *size) {
|
||||
|
||||
/* Soo, sometimes we want to parse IEC binary suffixes, and
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
int parse_boolean(const char *v) _pure_;
|
||||
int parse_pid(const char *s, pid_t* ret_pid);
|
||||
int parse_mode(const char *s, mode_t *ret);
|
||||
int parse_ifindex(const char *s, int *ret);
|
||||
|
||||
int parse_size(const char *t, uint64_t base, uint64_t *size);
|
||||
int parse_range(const char *t, unsigned *lower, unsigned *upper);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -332,9 +332,7 @@ int machine_load(Machine *m) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (safe_atoi(word, &ifi) < 0)
|
||||
continue;
|
||||
if (ifi <= 0)
|
||||
if (parse_ifindex(word, &ifi) < 0)
|
||||
continue;
|
||||
|
||||
if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
|
||||
|
||||
@@ -521,7 +521,7 @@ static int link_status_one(
|
||||
assert(rtnl);
|
||||
assert(name);
|
||||
|
||||
if (safe_atoi(name, &ifindex) >= 0 && ifindex > 0)
|
||||
if (parse_ifindex(name, &ifindex) >= 0)
|
||||
r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, ifindex);
|
||||
else {
|
||||
r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
|
||||
|
||||
@@ -103,7 +103,7 @@ int link_object_find(sd_bus *bus, const char *path, const char *interface, void
|
||||
if (r < 0)
|
||||
return 0;
|
||||
|
||||
r = safe_atoi(identifier, &ifindex);
|
||||
r = parse_ifindex(identifier, &ifindex);
|
||||
if (r < 0)
|
||||
return 0;
|
||||
|
||||
|
||||
@@ -300,8 +300,7 @@ static int parse_address(const char *s, int *family, union in_addr_union *addres
|
||||
|
||||
percent = strchr(s, '%');
|
||||
if (percent) {
|
||||
r = safe_atoi(percent+1, &ifi);
|
||||
if (r < 0 || ifi <= 0) {
|
||||
if (parse_ifindex(percent+1, &ifi) < 0) {
|
||||
ifi = if_nametoindex(percent+1);
|
||||
if (ifi <= 0)
|
||||
return -EINVAL;
|
||||
@@ -521,7 +520,7 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
case 'i': {
|
||||
int ifi;
|
||||
|
||||
if (safe_atoi(optarg, &ifi) >= 0 && ifi > 0)
|
||||
if (parse_ifindex(optarg, &ifi) >= 0)
|
||||
arg_ifindex = ifi;
|
||||
else {
|
||||
ifi = if_nametoindex(optarg);
|
||||
|
||||
Reference in New Issue
Block a user