chiark / gitweb /
socket-util: added check of return value
[elogind.git] / src / shared / socket-util.c
index 618c928f06d3b615faffa585453329097d44e7b6..42ea545917da025099f67c0b255a3ede0e4c921a 100644 (file)
@@ -6,16 +6,16 @@
   Copyright 2010 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
   (at your option) any later version.
 
   systemd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  General Public License for more details.
+  Lesser General Public License for more details.
 
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
@@ -35,6 +35,7 @@
 #include "macro.h"
 #include "util.h"
 #include "mkdir.h"
+#include "path-util.h"
 #include "socket-util.h"
 #include "missing.h"
 
@@ -166,7 +167,8 @@ int socket_address_parse(SocketAddress *a, const char *s) {
                 } else {
 
                         /* Just a port */
-                        if ((r = safe_atou(s, &u)) < 0)
+                        r = safe_atou(s, &u);
+                        if (r < 0)
                                 return r;
 
                         if (u <= 0 || u > 0xFFFF)
@@ -192,7 +194,7 @@ int socket_address_parse(SocketAddress *a, const char *s) {
 int socket_address_parse_netlink(SocketAddress *a, const char *s) {
         int family;
         unsigned group = 0;
-        char* sfamily = NULL;
+        _cleanup_free_ char *sfamily = NULL;
         assert(a);
         assert(s);
 
@@ -203,13 +205,9 @@ int socket_address_parse_netlink(SocketAddress *a, const char *s) {
         if (sscanf(s, "%ms %u", &sfamily, &group) < 1)
                 return errno ? -errno : -EINVAL;
 
-        if ((family = netlink_family_from_string(sfamily)) < 0)
-                if (safe_atoi(sfamily, &family) < 0) {
-                        free(sfamily);
-                        return -EINVAL;
-                }
-
-        free(sfamily);
+        family = netlink_family_from_string(sfamily);
+        if (family < 0)
+                return -EINVAL;
 
         a->sockaddr.nl.nl_family = AF_NETLINK;
         a->sockaddr.nl.nl_groups = group;
@@ -365,13 +363,12 @@ int socket_address_print(const SocketAddress *a, char **p) {
         }
 
         case AF_NETLINK: {
-                const char *sfamily;
-
-                if ((sfamily = netlink_family_to_string(a->protocol)))
-                        r = asprintf(p, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
-                else
-                        r = asprintf(p, "%i %u", a->protocol, a->sockaddr.nl.nl_groups);
+                char _cleanup_free_ *sfamily = NULL;
 
+                r = netlink_family_to_string_alloc(a->protocol, &sfamily);
+                if (r < 0)
+                        return r;
+                r = asprintf(p, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
                 if (r < 0)
                         return -ENOMEM;
 
@@ -538,7 +535,7 @@ static const char* const netlink_family_table[] = {
         [NETLINK_ECRYPTFS] = "ecryptfs"
 };
 
-DEFINE_STRING_TABLE_LOOKUP(netlink_family, int);
+DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
 
 static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
         [SOCKET_ADDRESS_DEFAULT] = "default",