]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: getlong(): Use the usual -1 as an error code
authorAlejandro Colomar <alx@kernel.org>
Fri, 1 Dec 2023 19:23:48 +0000 (20:23 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 5 Jan 2024 22:54:55 +0000 (16:54 -0600)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
14 files changed:
lib/getdef.c
lib/getlong.c
lib/limits.c
lib/sgetspent.c
lib/shadow.c
lib/strtoday.c
src/chage.c
src/chgpasswd.c
src/chpasswd.c
src/faillog.c
src/newusers.c
src/passwd.c
src/useradd.c
src/usermod.c

index 32ec8135c2e973c953054aed59a2ef317c87be36..99f8ea5d643f701fd17b21e1be196d7ff5e4c8ab 100644 (file)
@@ -245,7 +245,7 @@ int getdef_num (const char *item, int dflt)
                return dflt;
        }
 
-       if (   (getlong (d->value, &val) == 0)
+       if (   (getlong(d->value, &val) == -1)
            || (val > INT_MAX)
            || (val < -1)) {
                fprintf (shadow_logfd,
@@ -280,7 +280,7 @@ unsigned int getdef_unum (const char *item, unsigned int dflt)
                return dflt;
        }
 
-       if (   (getlong (d->value, &val) == 0)
+       if (   (getlong(d->value, &val) == -1)
            || (val < 0)
            || (val > INT_MAX)) {
                fprintf (shadow_logfd,
@@ -315,8 +315,7 @@ long getdef_long (const char *item, long dflt)
                return dflt;
        }
 
-       if (   (getlong (d->value, &val) == 0)
-           || (val < -1)) {
+       if (getlong(d->value, &val) == -1 || val < -1) {
                fprintf (shadow_logfd,
                         _("configuration error - cannot parse %s value: '%s'"),
                         item, d->value);
index 8c8083fbc991b8c90f7a47db8571842f7fb44b26..1f85f2885a400b0494e7face305cc04d52fd4921 100644 (file)
@@ -4,33 +4,33 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
+
 #include <config.h>
 
 #ident "$Id$"
 
 #include <stdlib.h>
 #include <errno.h>
+
 #include "prototypes.h"
 
+
 /*
  * getlong - extract a long integer provided by the numstr string in *result
  *
  * It supports decimal, hexadecimal or octal representations.
- *
- * Returns 0 on failure, 1 on success.
  */
-int getlong (const char *numstr, /*@out@*/long *result)
+int
+getlong(const char *numstr, /*@out@*/long *result)
 {
-       long val;
-       char *endptr;
+       char  *endptr;
+       long  val;
 
        errno = 0;
        val = strtol(numstr, &endptr, 0);
-       if (('\0' == *numstr) || ('\0' != *endptr) || (0 != errno)) {
-               return 0;
-       }
+       if (('\0' == *numstr) || ('\0' != *endptr) || (0 != errno))
+               return -1;
 
        *result = val;
-       return 1;
+       return 0;
 }
-
index 1aa4dc296697bee17fccdc8a25b9e3cf05e23040..9c6f1d9a9e97a476cf0656a84d0f7d3498e9e739 100644 (file)
@@ -89,7 +89,7 @@ static int set_prio (const char *value)
 {
        long prio;
 
-       if (   (getlong (value, &prio) == 0)
+       if (   (getlong(value, &prio) == -1)
            || (prio != (int) prio)) {
                return 0;
        }
@@ -482,7 +482,7 @@ void setup_limits (const struct passwd *info)
                        if (strncmp (cp, "pri=", 4) == 0) {
                                long  inc;
 
-                               if (   (getlong (cp + 4, &inc) == 1)
+                               if (   (getlong(cp + 4, &inc) == 0)
                                    && (inc >= -20) && (inc <= 20)) {
                                        errno = 0;
                                        if (   (nice (inc) != -1)
@@ -500,8 +500,7 @@ void setup_limits (const struct passwd *info)
                        }
                        if (strncmp (cp, "ulimit=", 7) == 0) {
                                long  blocks;
-
-                               if (   (getlong (cp + 7, &blocks) == 0)
+                               if (   (getlong(cp + 7, &blocks) == -1)
                                    || (blocks != (int) blocks)
                                    || (set_filesize_limit (blocks) != 0)) {
                                        SYSLOG ((LOG_WARN,
index 0bdc21f15a0a7ede51d4de67ff91070b5e6591b9..12817542126f2ee74c0c8d883809abde64d76d2c 100644 (file)
@@ -92,7 +92,7 @@ struct spwd *sgetspent (const char *string)
 
        if (fields[2][0] == '\0') {
                spwd.sp_lstchg = -1;
-       } else if (   (getlong (fields[2], &spwd.sp_lstchg) == 0)
+       } else if (   (getlong(fields[2], &spwd.sp_lstchg) == -1)
                   || (spwd.sp_lstchg < 0)) {
                return 0;
        }
@@ -103,7 +103,7 @@ struct spwd *sgetspent (const char *string)
 
        if (fields[3][0] == '\0') {
                spwd.sp_min = -1;
-       } else if (   (getlong (fields[3], &spwd.sp_min) == 0)
+       } else if (   (getlong(fields[3], &spwd.sp_min) == -1)
                   || (spwd.sp_min < 0)) {
                return 0;
        }
@@ -114,7 +114,7 @@ struct spwd *sgetspent (const char *string)
 
        if (fields[4][0] == '\0') {
                spwd.sp_max = -1;
-       } else if (   (getlong (fields[4], &spwd.sp_max) == 0)
+       } else if (   (getlong(fields[4], &spwd.sp_max) == -1)
                   || (spwd.sp_max < 0)) {
                return 0;
        }
@@ -139,7 +139,7 @@ struct spwd *sgetspent (const char *string)
 
        if (fields[5][0] == '\0') {
                spwd.sp_warn = -1;
-       } else if (   (getlong (fields[5], &spwd.sp_warn) == 0)
+       } else if (   (getlong(fields[5], &spwd.sp_warn) == -1)
                   || (spwd.sp_warn < 0)) {
                return 0;
        }
@@ -151,7 +151,7 @@ struct spwd *sgetspent (const char *string)
 
        if (fields[6][0] == '\0') {
                spwd.sp_inact = -1;
-       } else if (   (getlong (fields[6], &spwd.sp_inact) == 0)
+       } else if (   (getlong(fields[6], &spwd.sp_inact) == -1)
                   || (spwd.sp_inact < 0)) {
                return 0;
        }
@@ -163,7 +163,7 @@ struct spwd *sgetspent (const char *string)
 
        if (fields[7][0] == '\0') {
                spwd.sp_expire = -1;
-       } else if (   (getlong (fields[7], &spwd.sp_expire) == 0)
+       } else if (   (getlong(fields[7], &spwd.sp_expire) == -1)
                   || (spwd.sp_expire < 0)) {
                return 0;
        }
index 4c4650b1f860a4ff4bff4344b118579492a9fb3e..fca077a9e6c26905fa986ff12595a4666e31d4cd 100644 (file)
@@ -166,7 +166,7 @@ static struct spwd *my_sgetspent (const char *string)
        if (fields[2][0] == '\0') {
                spwd.sp_lstchg = -1;
        } else {
-               if (getlong (fields[2], &spwd.sp_lstchg) == 0) {
+               if (getlong(fields[2], &spwd.sp_lstchg) == -1) {
 #ifdef USE_NIS
                        if (nis_used) {
                                spwd.sp_lstchg = -1;
@@ -185,7 +185,7 @@ static struct spwd *my_sgetspent (const char *string)
        if (fields[3][0] == '\0') {
                spwd.sp_min = -1;
        } else {
-               if (getlong (fields[3], &spwd.sp_min) == 0) {
+               if (getlong(fields[3], &spwd.sp_min) == -1) {
 #ifdef USE_NIS
                        if (nis_used) {
                                spwd.sp_min = -1;
@@ -206,7 +206,7 @@ static struct spwd *my_sgetspent (const char *string)
        if (fields[4][0] == '\0') {
                spwd.sp_max = -1;
        } else {
-               if (getlong (fields[4], &spwd.sp_max) == 0) {
+               if (getlong(fields[4], &spwd.sp_max) == -1) {
 #ifdef USE_NIS
                        if (nis_used) {
                                spwd.sp_max = -1;
@@ -239,7 +239,7 @@ static struct spwd *my_sgetspent (const char *string)
        if (fields[5][0] == '\0') {
                spwd.sp_warn = -1;
        } else {
-               if (getlong (fields[5], &spwd.sp_warn) == 0) {
+               if (getlong(fields[5], &spwd.sp_warn) == -1) {
 #ifdef USE_NIS
                        if (nis_used) {
                                spwd.sp_warn = -1;
@@ -261,7 +261,7 @@ static struct spwd *my_sgetspent (const char *string)
        if (fields[6][0] == '\0') {
                spwd.sp_inact = -1;
        } else {
-               if (getlong (fields[6], &spwd.sp_inact) == 0) {
+               if (getlong(fields[6], &spwd.sp_inact) == -1) {
 #ifdef USE_NIS
                        if (nis_used) {
                                spwd.sp_inact = -1;
@@ -283,7 +283,7 @@ static struct spwd *my_sgetspent (const char *string)
        if (fields[7][0] == '\0') {
                spwd.sp_expire = -1;
        } else {
-               if (getlong (fields[7], &spwd.sp_expire) == 0) {
+               if (getlong(fields[7], &spwd.sp_expire) == -1) {
 #ifdef USE_NIS
                        if (nis_used) {
                                spwd.sp_expire = -1;
index d4c22af2399d96efe396dd8e8fb2a7a807b8adf1..ad1ed61f4309f8c6a8f3fcb3831c3fadbff7eb3e 100644 (file)
@@ -62,7 +62,7 @@ long strtoday (const char *str)
        }
        if (isnum) {
                long retdate;
-               if (getlong (str, &retdate) == 0) {
+               if (getlong(str, &retdate) == -1) {
                        return -2;
                }
                return retdate;
index 4277e9d819710ee1d23cc167bcf72fb0e8047165..e13848c90967610528e3274d3380ede9c30a3e27 100644 (file)
@@ -167,14 +167,14 @@ static int new_fields (void)
 
        SNPRINTF(buf, "%ld", mindays);
        change_field (buf, sizeof buf, _("Minimum Password Age"));
-       if (   (getlong (buf, &mindays) == 0)
+       if (   (getlong(buf, &mindays) == -1)
            || (mindays < -1)) {
                return 0;
        }
 
        SNPRINTF(buf, "%ld", maxdays);
        change_field (buf, sizeof buf, _("Maximum Password Age"));
-       if (   (getlong (buf, &maxdays) == 0)
+       if (   (getlong(buf, &maxdays) == -1)
            || (maxdays < -1)) {
                return 0;
        }
@@ -198,14 +198,14 @@ static int new_fields (void)
 
        SNPRINTF(buf, "%ld", warndays);
        change_field (buf, sizeof buf, _("Password Expiration Warning"));
-       if (   (getlong (buf, &warndays) == 0)
+       if (   (getlong(buf, &warndays) == -1)
            || (warndays < -1)) {
                return 0;
        }
 
        SNPRINTF(buf, "%ld", inactdays);
        change_field (buf, sizeof buf, _("Password Inactive"));
-       if (   (getlong (buf, &inactdays) == 0)
+       if (   (getlong(buf, &inactdays) == -1)
            || (inactdays < -1)) {
                return 0;
        }
@@ -393,7 +393,7 @@ static void process_flags (int argc, char **argv)
                        break;
                case 'I':
                        Iflg = true;
-                       if (   (getlong (optarg, &inactdays) == 0)
+                       if (   (getlong(optarg, &inactdays) == -1)
                            || (inactdays < -1)) {
                                fprintf (stderr,
                                         _("%s: invalid numeric argument '%s'\n"),
@@ -406,7 +406,7 @@ static void process_flags (int argc, char **argv)
                        break;
                case 'm':
                        mflg = true;
-                       if (   (getlong (optarg, &mindays) == 0)
+                       if (   (getlong(optarg, &mindays) == -1)
                            || (mindays < -1)) {
                                fprintf (stderr,
                                         _("%s: invalid numeric argument '%s'\n"),
@@ -416,7 +416,7 @@ static void process_flags (int argc, char **argv)
                        break;
                case 'M':
                        Mflg = true;
-                       if (   (getlong (optarg, &maxdays) == 0)
+                       if (   (getlong(optarg, &maxdays) == -1)
                            || (maxdays < -1)) {
                                fprintf (stderr,
                                         _("%s: invalid numeric argument '%s'\n"),
@@ -430,7 +430,7 @@ static void process_flags (int argc, char **argv)
                        break;
                case 'W':
                        Wflg = true;
-                       if (   (getlong (optarg, &warndays) == 0)
+                       if (   (getlong(optarg, &warndays) == -1)
                            || (warndays < -1)) {
                                fprintf (stderr,
                                         _("%s: invalid numeric argument '%s'\n"),
index d7cdd76a9bb1ec31042a67b03c610cbd4c747d4a..41dadc50825bdbccb1ed0f665c0a72f7113169ed 100644 (file)
@@ -195,19 +195,19 @@ static void process_flags (int argc, char **argv)
                        }
 #if defined(USE_SHA_CRYPT)
                        if (  (   ((0 == strcmp (crypt_method, "SHA256")) || (0 == strcmp (crypt_method, "SHA512")))
-                              && (0 == getlong(optarg, &sha_rounds)))) {
+                              && (-1 == getlong(optarg, &sha_rounds)))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_SHA_CRYPT */
 #if defined(USE_BCRYPT)
                         if ((   (0 == strcmp (crypt_method, "BCRYPT"))
-                              && (0 == getlong(optarg, &bcrypt_rounds)))) {
+                              && (-1 == getlong(optarg, &bcrypt_rounds)))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_BCRYPT */
 #if defined(USE_YESCRYPT)
                         if ((   (0 == strcmp (crypt_method, "YESCRYPT"))
-                              && (0 == getlong(optarg, &yescrypt_cost)))) {
+                              && (-1 == getlong(optarg, &yescrypt_cost)))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_YESCRYPT */
index c2546ec50ca3fa29fc87442de8735a368741bfb0..3a4bd4fe770d15856fbbee3480dd44247e9a5bea 100644 (file)
@@ -190,19 +190,19 @@ static void process_flags (int argc, char **argv)
                         bad_s = 0;
 #if defined(USE_SHA_CRYPT)
                        if ((IS_CRYPT_METHOD("SHA256") || IS_CRYPT_METHOD("SHA512"))
-                           && (0 == getlong(optarg, &sha_rounds))) {
+                           && (-1 == getlong(optarg, &sha_rounds))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_SHA_CRYPT */
 #if defined(USE_BCRYPT)
                         if (IS_CRYPT_METHOD("BCRYPT")
-                           && (0 == getlong(optarg, &bcrypt_rounds))) {
+                           && (-1 == getlong(optarg, &bcrypt_rounds))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_BCRYPT */
 #if defined(USE_YESCRYPT)
                         if (IS_CRYPT_METHOD("YESCRYPT")
-                           && (0 == getlong(optarg, &yescrypt_cost))) {
+                           && (-1 == getlong(optarg, &yescrypt_cost))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_YESCRYPT */
index e4eca198e9a8052453941bddb1adb298d1fe8131..83439505ff79d4f831e1e34a560c7136e11c5342 100644 (file)
@@ -547,7 +547,7 @@ int main (int argc, char **argv)
                                usage (E_SUCCESS);
                                /*@notreached@*/break;
                        case 'l':
-                               if (getlong (optarg, &fail_locktime) == 0) {
+                               if (getlong(optarg, &fail_locktime) == -1) {
                                        fprintf (stderr,
                                                 _("%s: invalid numeric argument '%s'\n"),
                                                 Prog, optarg);
@@ -559,7 +559,7 @@ int main (int argc, char **argv)
                        {
                                long  lmax;
 
-                               if (   (getlong (optarg, &lmax) == 0)
+                               if (   (getlong(optarg, &lmax) == -1)
                                    || ((long)(short) lmax != lmax)) {
                                        fprintf (stderr,
                                                 _("%s: invalid numeric argument '%s'\n"),
@@ -576,7 +576,7 @@ int main (int argc, char **argv)
                        case 'R': /* no-op, handled in process_root_flag () */
                                break;
                        case 't':
-                               if (getlong (optarg, &days) == 0) {
+                               if (getlong(optarg, &days) == -1) {
                                        fprintf (stderr,
                                                 _("%s: invalid numeric argument '%s'\n"),
                                                 Prog, optarg);
index bb34e9701747f468010efc2743f38a934ece4e83..6eb3350552f50b3d3aa63cf1951392b7a0e6a510 100644 (file)
@@ -673,19 +673,19 @@ static void process_flags (int argc, char **argv)
                        }
 #if defined(USE_SHA_CRYPT)
                        if (  (   ((0 == strcmp (crypt_method, "SHA256")) || (0 == strcmp (crypt_method, "SHA512")))
-                              && (0 == getlong(optarg, &sha_rounds)))) {
+                              && (-1 == getlong(optarg, &sha_rounds)))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_SHA_CRYPT */
 #if defined(USE_BCRYPT)
                         if ((   (0 == strcmp (crypt_method, "BCRYPT"))
-                              && (0 == getlong(optarg, &bcrypt_rounds)))) {
+                              && (-1 == getlong(optarg, &bcrypt_rounds)))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_BCRYPT */
 #if defined(USE_YESCRYPT)
                         if ((   (0 == strcmp (crypt_method, "YESCRYPT"))
-                              && (0 == getlong(optarg, &yescrypt_cost)))) {
+                              && (-1 == getlong(optarg, &yescrypt_cost)))) {
                             bad_s = 1;
                         }
 #endif                         /* USE_YESCRYPT */
index f494a9257ee7266c2d1eca40ba51e8dc1d7d71dd..efa444097dbd5ddd05d8616fb5cedba6d7a62d8f 100644 (file)
@@ -775,7 +775,7 @@ int main (int argc, char **argv)
                                usage (E_SUCCESS);
                                /*@notreached@*/break;
                        case 'i':
-                               if (   (getlong (optarg, &inact) == 0)
+                               if (   (getlong(optarg, &inact) == -1)
                                    || (inact < -1)) {
                                        fprintf (stderr,
                                                 _("%s: invalid numeric argument '%s'\n"),
@@ -794,7 +794,7 @@ int main (int argc, char **argv)
                                anyflag = true;
                                break;
                        case 'n':
-                               if (   (getlong (optarg, &age_min) == 0)
+                               if (   (getlong(optarg, &age_min) == -1)
                                    || (age_min < -1)) {
                                        fprintf (stderr,
                                                 _("%s: invalid numeric argument '%s'\n"),
@@ -829,7 +829,7 @@ int main (int argc, char **argv)
                                anyflag = true;
                                break;
                        case 'w':
-                               if (   (getlong (optarg, &warn) == 0)
+                               if (   (getlong(optarg, &warn) == -1)
                                    || (warn < -1)) {
                                        (void) fprintf (stderr,
                                                        _("%s: invalid numeric argument '%s'\n"),
@@ -840,7 +840,7 @@ int main (int argc, char **argv)
                                anyflag = true;
                                break;
                        case 'x':
-                               if (   (getlong (optarg, &age_max) == 0)
+                               if (   (getlong(optarg, &age_max) == -1)
                                    || (age_max < -1)) {
                                        (void) fprintf (stderr,
                                                        _("%s: invalid numeric argument '%s'\n"),
index 31d89686e0eb22ae507b584af4a026c07fb39ede..3437a5721f52a19f07cbff961aee8ddbf898566c 100644 (file)
@@ -412,7 +412,7 @@ static void get_defaults (void)
                 * Default Password Inactive value
                 */
                else if (MATCH (buf, DINACT)) {
-                       if (   (getlong (cp, &def_inactive) == 0)
+                       if (   (getlong(cp, &def_inactive) == -1)
                            || (def_inactive < -1)) {
                                fprintf (stderr,
                                         _("%s: invalid numeric argument '%s'\n"),
@@ -1315,7 +1315,7 @@ static void process_flags (int argc, char **argv)
                                eflg = true;
                                break;
                        case 'f':
-                               if (   (getlong (optarg, &def_inactive) == 0)
+                               if (   (getlong(optarg, &def_inactive) == -1)
                                    || (def_inactive < -1)) {
                                        fprintf (stderr,
                                                 _("%s: invalid numeric argument '%s'\n"),
index 6ec67da342c02dd5a558955d428e95eee9c16fd7..8ff0202da2deb7e8215dbe31b045c3f4a2129b72 100644 (file)
@@ -1067,7 +1067,7 @@ static void process_flags (int argc, char **argv)
                                eflg = true;
                                break;
                        case 'f':
-                               if (   (getlong (optarg, &user_newinactive) == 0)
+                               if (   (getlong(optarg, &user_newinactive) == -1)
                                    || (user_newinactive < -1)) {
                                        fprintf (stderr,
                                                 _("%s: invalid numeric argument '%s'\n"),