2 * Copyright (c) 2017 Todd C. Miller <Todd.Miller@sudo.ws>
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.
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.
18 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
19 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
24 #include <sys/types.h>
30 #endif /* HAVE_STRING_H */
33 #endif /* HAVE_STRINGS_H */
38 #include "sudo_compat.h"
39 #include "sudoers_debug.h"
43 * Parse a command timeout in sudoers in the format 1d2h3m4s
44 * (days, hours, minutes, seconds) or a number of seconds with no suffix.
45 * Returns the number of seconds or -1 on error.
48 parse_timeout(const char *timestr)
50 debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER)
51 const char suffixes[] = "dhms";
52 const char *cp = timestr;
61 /* Parse number, must be present and positive. */
63 l = strtol(cp, &ep, 10);
69 if (errno == ERANGE || l < 0 || l > INT_MAX)
72 /* Find a matching suffix or return an error. */
74 ch = tolower((unsigned char)*ep++);
75 while (suffixes[idx] != ch) {
76 if (suffixes[idx] == '\0') {
87 if (l > INT_MAX / (24 * 60 * 60))
92 if (l > INT_MAX / (60 * 60))
102 if (l > INT_MAX - timeout)
108 } while (*cp != '\0');
110 debug_return_int(timeout);
113 debug_return_int(-1);