]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
user-util: simplify uid parsing a bit
authorLennart Poettering <lennart@poettering.net>
Sun, 25 Oct 2015 21:37:43 +0000 (22:37 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 26 Oct 2015 00:24:38 +0000 (01:24 +0100)
src/basic/user-util.c

index 637391f4a79fb0ef4ee7cce0b5a7b52dcf033f9d..ebeac99d3121a7fc62b404320a7e9046071b77a4 100644 (file)
 bool uid_is_valid(uid_t uid) {
 
         /* Some libc APIs use UID_INVALID as special placeholder */
-        if (uid == (uid_t) 0xFFFFFFFF)
+        if (uid == (uid_t) UINT32_C(0xFFFFFFFF))
                 return false;
 
         /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
-        if (uid == (uid_t) 0xFFFF)
+        if (uid == (uid_t) UINT32_C(0xFFFF))
                 return false;
 
         return true;
 }
 
-int parse_uid(const char *s, uid_t* ret_uid) {
-        unsigned long ul = 0;
-        uid_t uid;
+int parse_uid(const char *s, uid_t *ret) {
+        uint32_t uid = 0;
         int r;
 
         assert(s);
 
-        r = safe_atolu(s, &ul);
+        assert_cc(sizeof(uid_t) == sizeof(uint32_t));
+        r = safe_atou32(s, &uid);
         if (r < 0)
                 return r;
 
-        uid = (uid_t) ul;
-
-        if ((unsigned long) uid != ul)
-                return -ERANGE;
-
         if (!uid_is_valid(uid))
                 return -ENXIO; /* we return ENXIO instead of EINVAL
                                 * here, to make it easy to distuingish
                                 * invalid numeric uids invalid
                                 * strings. */
 
-        if (ret_uid)
-                *ret_uid = uid;
+        if (ret)
+                *ret = uid;
 
         return 0;
 }