Annotation of embedaddon/sudo/common/atoid.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
                      3:  *
                      4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     15:  */
                     16: 
                     17: #include <config.h>
                     18: 
                     19: #include <sys/types.h>
                     20: 
                     21: #include <stdio.h>
                     22: #ifdef STDC_HEADERS
                     23: # include <stdlib.h>
                     24: # include <stddef.h>
                     25: #else
                     26: # ifdef HAVE_STDLIB_H
                     27: #  include <stdlib.h>
                     28: # endif
                     29: #endif /* STDC_HEADERS */
                     30: #if defined(HAVE_STDINT_H)
                     31: # include <stdint.h>
                     32: #elif defined(HAVE_INTTYPES_H)
                     33: # include <inttypes.h>
                     34: #endif
                     35: #ifdef HAVE_STDBOOL_H
                     36: # include <stdbool.h>
                     37: #else
                     38: # include "compat/stdbool.h"
                     39: #endif
                     40: #include <errno.h>
                     41: #include <limits.h>
                     42: 
                     43: #define DEFAULT_TEXT_DOMAIN    "sudo"
1.1.1.2 ! misho      44: #include "gettext.h"           /* must be included before missing.h */
1.1       misho      45: 
                     46: #include "missing.h"
                     47: #include "sudo_debug.h"
1.1.1.2 ! misho      48: #include "sudo_util.h"
1.1       misho      49: 
                     50: /*
                     51:  * Parse a uid/gid in string form.
                     52:  * If sep is non-NULL, it contains valid separator characters (e.g. comma, space)
                     53:  * If endp is non-NULL it is set to the next char after the ID.
                     54:  * On success, returns the parsed ID and clears errstr.
                     55:  * On error, returns 0 and sets errstr.
                     56:  */
                     57: id_t
                     58: atoid(const char *p, const char *sep, char **endp, const char **errstr)
                     59: {
                     60:     char *ep;
                     61:     id_t rval = 0;
                     62:     bool valid = false;
                     63:     debug_decl(atoid, SUDO_DEBUG_UTIL)
                     64: 
                     65:     if (sep == NULL)
                     66:        sep = "";
                     67:     errno = 0;
                     68:     if (*p == '-') {
                     69:        long lval = strtol(p, &ep, 10);
                     70:        if (ep != p) {
                     71:            /* check for valid separator (including '\0') */
                     72:            do {
                     73:                if (*ep == *sep)
                     74:                    valid = true;
                     75:            } while (*sep++ != '\0');
                     76:        }
                     77:        if (!valid) {
1.1.1.2 ! misho      78:            if (errstr != NULL)
        !            79:                *errstr = N_("invalid value");
1.1       misho      80:            errno = EINVAL;
                     81:            goto done;
                     82:        }
1.1.1.2 ! misho      83:        if ((errno == ERANGE && lval == LONG_MAX) || lval > INT_MAX) {
1.1       misho      84:            errno = ERANGE;
1.1.1.2 ! misho      85:            if (errstr != NULL)
        !            86:                *errstr = N_("value too large");
        !            87:            goto done;
        !            88:        }
        !            89:        if ((errno == ERANGE && lval == LONG_MIN) || lval < INT_MIN) {
        !            90:            errno = ERANGE;
        !            91:            if (errstr != NULL)
        !            92:                *errstr = N_("value too small");
1.1       misho      93:            goto done;
                     94:        }
                     95:        rval = (id_t)lval;
                     96:     } else {
                     97:        unsigned long ulval = strtoul(p, &ep, 10);
                     98:        if (ep != p) {
                     99:            /* check for valid separator (including '\0') */
                    100:            do {
                    101:                if (*ep == *sep)
                    102:                    valid = true;
                    103:            } while (*sep++ != '\0');
                    104:        }
                    105:        if (!valid) {
1.1.1.2 ! misho     106:            if (errstr != NULL)
        !           107:                *errstr = N_("invalid value");
1.1       misho     108:            errno = EINVAL;
                    109:            goto done;
                    110:        }
                    111:        if ((errno == ERANGE && ulval == ULONG_MAX) || ulval > UINT_MAX) {
                    112:            errno = ERANGE;
1.1.1.2 ! misho     113:            if (errstr != NULL)
        !           114:                *errstr = N_("value too large");
1.1       misho     115:            goto done;
                    116:        }
                    117:        rval = (id_t)ulval;
                    118:     }
1.1.1.2 ! misho     119:     if (errstr != NULL)
        !           120:        *errstr = NULL;
1.1       misho     121:     if (endp != NULL)
                    122:        *endp = ep;
                    123: done:
                    124:     debug_return_int(rval);
                    125: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>