/src/sudo/lib/util/strtoid.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2013-2020 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <sys/types.h> /* for id_t */ |
27 | | |
28 | | #ifdef HAVE_STDBOOL_H |
29 | | # include <stdbool.h> |
30 | | #else |
31 | | # include "compat/stdbool.h" |
32 | | #endif |
33 | | #include <ctype.h> |
34 | | #include <errno.h> |
35 | | #include <limits.h> |
36 | | |
37 | | #include "sudo_compat.h" |
38 | | #include "sudo_debug.h" |
39 | | #include "sudo_gettext.h" |
40 | | #include "sudo_util.h" |
41 | | |
42 | | /* |
43 | | * Make sure that the ID ends with a valid separator char. |
44 | | */ |
45 | | static bool |
46 | | valid_separator(const char *p, const char *ep, const char *sep) |
47 | 104 | { |
48 | 104 | bool valid = false; |
49 | | |
50 | 104 | if (ep != p) { |
51 | | /* check for valid separator (including '\0') */ |
52 | 104 | if (sep == NULL) |
53 | 104 | sep = ""; |
54 | 104 | do { |
55 | 104 | if (*ep == *sep) |
56 | 104 | valid = true; |
57 | 104 | } while (*sep++ != '\0'); |
58 | 104 | } |
59 | 104 | return valid; |
60 | 104 | } |
61 | | |
62 | | /* |
63 | | * Parse a uid/gid in string form. |
64 | | * If sep is non-NULL, it contains valid separator characters (e.g. comma, space) |
65 | | * If endp is non-NULL it is set to the next char after the ID. |
66 | | * On success, returns the parsed ID and clears errstr. |
67 | | * On error, returns 0 and sets errstr. |
68 | | */ |
69 | | id_t |
70 | | sudo_strtoidx_v1(const char *p, const char *sep, char **endp, const char **errstrp) |
71 | 104 | { |
72 | 104 | const char *errstr; |
73 | 104 | char *ep; |
74 | 104 | id_t ret; |
75 | 104 | debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL); |
76 | | |
77 | 104 | ret = sudo_strtonumx(p, INT_MIN, UINT_MAX, &ep, &errstr); |
78 | 104 | if (errstr == NULL) { |
79 | | /* |
80 | | * Disallow id -1 (UINT_MAX), which means "no change" |
81 | | * and check for a valid separator (if specified). |
82 | | */ |
83 | 104 | if (ret == (id_t)-1 || ret == (id_t)UINT_MAX || !valid_separator(p, ep, sep)) { |
84 | 0 | errstr = N_("invalid value"); |
85 | 0 | errno = EINVAL; |
86 | 0 | ret = 0; |
87 | 0 | } |
88 | 104 | } |
89 | 104 | if (errstrp != NULL) |
90 | 104 | *errstrp = errstr; |
91 | 104 | if (endp != NULL) |
92 | 0 | *endp = ep; |
93 | 104 | debug_return_id_t(ret); |
94 | 104 | } |
95 | | |
96 | | /* Backward compatibility */ |
97 | | id_t |
98 | | sudo_strtoid_v1(const char *p, const char *sep, char **endp, const char **errstrp) |
99 | 0 | { |
100 | 0 | return sudo_strtoidx_v1(p, sep, endp, errstrp); |
101 | 0 | } |
102 | | |
103 | | /* Simplified interface */ |
104 | | id_t |
105 | | sudo_strtoid_v2(const char *p, const char **errstrp) |
106 | 104 | { |
107 | 104 | return sudo_strtoidx_v1(p, NULL, NULL, errstrp); |
108 | 104 | } |