]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Rewrite sd_machine_get_ifindices() to avoid FOREACH_WORD()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 Jul 2020 10:56:51 +0000 (12:56 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 9 Sep 2020 07:34:54 +0000 (09:34 +0200)
If we fail to parse the index, the failure is propogated as -EUNCLEAN.
(-EINVAL would be confused with invalid args to the function itself.)

man/sd_machine_get_class.xml
src/libsystemd/sd-login/sd-login.c
src/systemd/sd-login.h

index cd259c863f9fa5e6a8125ba6b7e83028ff1430c9..0a0d601899c2880efbee4d369bbc48926002c215 100644 (file)
@@ -35,7 +35,7 @@
       <funcprototype>
         <funcdef>int <function>sd_machine_get_ifindices</function></funcdef>
         <paramdef>const char* <parameter>machine</parameter></paramdef>
-        <paramdef>int **<parameter>ifindices</parameter></paramdef>
+        <paramdef>int **<parameter>ret_ifindices</parameter></paramdef>
       </funcprototype>
     </funcsynopsis>
   </refsynopsisdiv>
index 14e046872064abb84edb6494fdd890d103120399..3828fa58e48ad0219aebb5ccbba0cfcf6986e869 100644 (file)
@@ -894,47 +894,47 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
         return 0;
 }
 
-_public_ int sd_machine_get_ifindices(const char *machine, int **ifindices) {
-        _cleanup_free_ char *netif = NULL;
-        size_t l, allocated = 0, nr = 0;
-        int *ni = NULL;
-        const char *p, *word, *state;
+_public_ int sd_machine_get_ifindices(const char *machine, int **ret_ifindices) {
+        _cleanup_free_ char *netif_line = NULL;
+        const char *p;
         int r;
 
         assert_return(machine_name_is_valid(machine), -EINVAL);
-        assert_return(ifindices, -EINVAL);
+        assert_return(ret_ifindices, -EINVAL);
 
         p = strjoina("/run/systemd/machines/", machine);
-        r = parse_env_file(NULL, p, "NETIF", &netif);
+        r = parse_env_file(NULL, p, "NETIF", &netif_line);
         if (r == -ENOENT)
                 return -ENXIO;
         if (r < 0)
                 return r;
-        if (!netif) {
-                *ifindices = NULL;
+        if (!netif_line) {
+                *ret_ifindices = NULL;
                 return 0;
         }
 
-        FOREACH_WORD(word, l, netif, state) {
-                char buf[l+1];
-                int ifi;
+        _cleanup_strv_free_ char **tt = strv_split(netif_line, NULL);
+        if (!tt)
+                return -ENOMEM;
 
-                *(char*) (mempcpy(buf, word, l)) = 0;
+        size_t n = 0;
+        int *ifindices = new(int, strv_length(tt));
+        if (!ifindices)
+                return -ENOMEM;
 
-                ifi = parse_ifindex(buf);
-                if (ifi < 0)
-                        continue;
+        for (size_t i = 0; tt[i]; i++) {
+                int ind;
 
-                if (!GREEDY_REALLOC(ni, allocated, nr+1)) {
-                        free(ni);
-                        return -ENOMEM;
-                }
+                ind = parse_ifindex(tt[i]);
+                if (ind < 0)
+                        /* Return -EUCLEAN to distinguish from -EINVAL for invalid args */
+                        return ind == -EINVAL ? -EUCLEAN : ind;
 
-                ni[nr++] = ifi;
+                ifindices[n++] = ind;
         }
 
-        *ifindices = ni;
-        return nr;
+        *ret_ifindices = ifindices;
+        return n;
 }
 
 static int MONITOR_TO_FD(sd_login_monitor *m) {
index 360f44d341e8418f0f6ae85d1eea935489726ac6..6a8c20625910f8a406c371b4bee9cb3eadbc7363 100644 (file)
@@ -199,7 +199,7 @@ int sd_seat_can_graphical(const char *seat);
 int sd_machine_get_class(const char *machine, char **clazz);
 
 /* Return the list if host-side network interface indices of a machine */
-int sd_machine_get_ifindices(const char *machine, int **ifindices);
+int sd_machine_get_ifindices(const char *machine, int **ret_ifindices);
 
 /* Get all seats, store in *seats. Returns the number of seats. If
  * seats is NULL, this only returns the number of seats. */