/src/sudo/lib/util/strtomode.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2013-2015 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/stat.h> |
27 | | |
28 | | #include <stdlib.h> |
29 | | #include <errno.h> |
30 | | |
31 | | #include "sudo_compat.h" |
32 | | #include "sudo_debug.h" |
33 | | #include "sudo_gettext.h" |
34 | | #include "sudo_util.h" |
35 | | |
36 | | /* |
37 | | * Parse an octal file mode in the range [0, 0777]. |
38 | | * On success, returns the parsed mode and clears errstr. |
39 | | * On error, returns 0 and sets errstr. |
40 | | */ |
41 | | int |
42 | | sudo_strtomode_v1(const char *cp, const char **errstr) |
43 | 56.0k | { |
44 | 56.0k | char *ep; |
45 | 56.0k | long lval; |
46 | 56.0k | debug_decl(sudo_strtomode, SUDO_DEBUG_UTIL); |
47 | | |
48 | 56.0k | errno = 0; |
49 | 56.0k | lval = strtol(cp, &ep, 8); |
50 | 56.0k | if (ep == cp || *ep != '\0') { |
51 | 40 | if (errstr != NULL) |
52 | 40 | *errstr = N_("invalid value"); |
53 | 40 | errno = EINVAL; |
54 | 40 | debug_return_int(0); |
55 | 40 | } |
56 | 55.9k | if (lval < 0 || lval > ACCESSPERMS) { |
57 | 159 | if (errstr != NULL) |
58 | 159 | *errstr = lval < 0 ? N_("value too small") : N_("value too large"); |
59 | 159 | errno = ERANGE; |
60 | 159 | debug_return_int(0); |
61 | 159 | } |
62 | 55.8k | if (errstr != NULL) |
63 | 55.8k | *errstr = NULL; |
64 | 55.8k | debug_return_int((int)lval); |
65 | 55.8k | } |