2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2017 Todd C. Miller <Todd.Miller@sudo.ws>
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.
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.
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
26 #include <sys/types.h>
32 #endif /* HAVE_STRING_H */
35 #endif /* HAVE_STRINGS_H */
40 #include "sudo_compat.h"
41 #include "sudoers_debug.h"
45 * Parse a command timeout in sudoers in the format 1d2h3m4s
46 * (days, hours, minutes, seconds) or a number of seconds with no suffix.
47 * Returns the number of seconds or -1 on error.
50 parse_timeout(const char *timestr)
52 debug_decl(parse_timeout, SUDOERS_DEBUG_PARSER)
53 const char suffixes[] = "dhms";
54 const char *cp = timestr;
63 /* Parse number, must be present and positive. */
65 l = strtol(cp, &ep, 10);
71 if (errno == ERANGE || l < 0 || l > INT_MAX)
74 /* Find a matching suffix or return an error. */
76 ch = tolower((unsigned char)*ep++);
77 while (suffixes[idx] != ch) {
78 if (suffixes[idx] == '\0') {
89 if (l > INT_MAX / (24 * 60 * 60))
94 if (l > INT_MAX / (60 * 60))
104 if (l > INT_MAX - timeout)
110 } while (*cp != '\0');
112 debug_return_int(timeout);
115 debug_return_int(-1);