Coverage Report

Created: 2025-07-11 06:58

/src/sudo/lib/util/strtobool.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2010-2016 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
#include <string.h>
26
#ifdef HAVE_STRINGS_H
27
# include <strings.h>
28
#endif /* HAVE_STRINGS_H */
29
30
#include <sudo_compat.h>
31
#include <sudo_debug.h>
32
#include <sudo_util.h>
33
34
int
35
sudo_strtobool_v1(const char *str)
36
23.6k
{
37
23.6k
    debug_decl(sudo_strtobool, SUDO_DEBUG_UTIL);
38
39
23.6k
    switch (*str) {
40
4.12k
  case '0':
41
10.3k
  case '1':
42
10.3k
      if (str[1] == '\0')
43
9.81k
    debug_return_int(*str - '0');
44
560
      break;
45
1.04k
  case 'y':
46
2.08k
  case 'Y':
47
2.08k
      if (strcasecmp(str, "yes") == 0)
48
1.31k
    debug_return_int(1);
49
772
      break;
50
1.71k
  case 't':
51
2.39k
  case 'T':
52
2.39k
      if (strcasecmp(str, "true") == 0)
53
1.63k
    debug_return_int(1);
54
761
      break;
55
2.06k
  case 'o':
56
2.63k
  case 'O':
57
2.63k
      if (strcasecmp(str, "on") == 0)
58
1.10k
    debug_return_int(1);
59
1.52k
      if (strcasecmp(str, "off") == 0)
60
792
    debug_return_int(0);
61
732
      break;
62
975
  case 'n':
63
1.96k
  case 'N':
64
1.96k
      if (strcasecmp(str, "no") == 0)
65
1.22k
    debug_return_int(0);
66
742
      break;
67
1.16k
  case 'f':
68
1.92k
  case 'F':
69
1.92k
      if (strcasecmp(str, "false") == 0)
70
1.17k
    debug_return_int(0);
71
744
      break;
72
23.6k
    }
73
23.6k
    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
74
6.53k
  "invalid boolean value \"%s\"", str);
75
76
6.53k
    debug_return_int(-1);
77
6.53k
}