Coverage Report

Created: 2025-10-10 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/lib/util/parseln.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2007, 2013-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
#include <config.h>
20
21
#include <ctype.h>
22
#include <errno.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#ifdef HAVE_STDBOOL_H
27
# include <stdbool.h>
28
#else
29
# include <compat/stdbool.h>
30
#endif
31
32
#include <sudo_compat.h>
33
#include <sudo_util.h>
34
#include <sudo_debug.h>
35
36
/*
37
 * Read a line of input, honoring line continuation chars.
38
 * Remove comments and strip off leading and trailing spaces.
39
 * Returns the line length and updates the buf and bufsize pointers.
40
 * XXX - just use a struct w/ state, including getdelim buffer?
41
 *       could also make comment char and line continuation configurable
42
 */
43
ssize_t
44
sudo_parseln_v2(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp, int flags)
45
26.3k
{
46
26.3k
    ssize_t len, total = 0;
47
26.3k
    size_t bufsize, linesize = 0;
48
26.3k
    char *cp, *line = NULL;
49
26.3k
    bool continued, comment;
50
26.3k
    debug_decl(sudo_parseln, SUDO_DEBUG_UTIL);
51
52
27.1k
    do {
53
27.1k
  comment = false;
54
27.1k
  continued = false;
55
27.1k
  len = getdelim(&line, &linesize, '\n', fp);
56
27.1k
  if (len == -1)
57
871
      break;
58
26.3k
  if (lineno != NULL)
59
26.3k
      (*lineno)++;
60
61
  /* Remove trailing newline(s) if present. */
62
49.7k
  while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))
63
23.4k
      line[--len] = '\0';
64
65
  /* Remove comments or check for line continuation (but not both) */
66
26.3k
  if ((cp = strchr(line, '#')) != NULL) {
67
1.00k
      if (cp == line || !ISSET(flags, PARSELN_COMM_BOL)) {
68
1.00k
    *cp = '\0';
69
1.00k
    len = (ssize_t)(cp - line);
70
1.00k
    comment = true;
71
1.00k
      }
72
1.00k
  }
73
26.3k
  if (!comment && !ISSET(flags, PARSELN_CONT_IGN)) {
74
25.3k
      if (len > 0 && line[len - 1] == '\\' && (len == 1 || line[len - 2] != '\\')) {
75
845
    line[--len] = '\0';
76
845
    continued = true;
77
845
      }
78
25.3k
  }
79
80
  /* Trim leading and trailing whitespace */
81
26.3k
  if (!continued) {
82
25.8k
      while (len > 0 && isblank((unsigned char)line[len - 1]))
83
378
    line[--len] = '\0';
84
25.4k
  }
85
26.3k
  for (cp = line; isblank((unsigned char)*cp); cp++)
86
570
      len--;
87
88
26.3k
  bufsize = (size_t)(total + len + 1);
89
26.3k
  if (*bufp == NULL || bufsize > *bufsizep) {
90
3.98k
      const size_t newsize = sudo_pow2_roundup(bufsize);
91
3.98k
      void *newbuf;
92
93
3.98k
      if (newsize < bufsize) {
94
    /* overflow */
95
0
    errno = ENOMEM;
96
0
    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
97
0
        "unable to allocate memory");
98
0
    len = -1;
99
0
    total = 0;
100
0
    break;
101
0
      }
102
3.98k
      if ((newbuf = realloc(*bufp, newsize)) == NULL) {
103
0
    sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
104
0
        "unable to allocate memory");
105
0
    len = -1;
106
0
    total = 0;
107
0
    break;
108
0
      }
109
3.98k
      *bufp = newbuf;
110
3.98k
      *bufsizep = newsize;
111
3.98k
  }
112
26.3k
  memcpy(*bufp + total, cp, (size_t)(len + 1));
113
26.3k
  total += len;
114
26.3k
    } while (continued);
115
26.3k
    free(line);
116
26.3k
    if (len == -1 && total == 0)
117
804
  debug_return_ssize_t(-1);
118
25.5k
    debug_return_ssize_t(total);
119
25.5k
}
120
121
ssize_t
122
sudo_parseln_v1(char **bufp, size_t *bufsizep, unsigned int *lineno, FILE *fp)
123
0
{
124
0
    return sudo_parseln_v2(bufp, bufsizep, lineno, fp, 0);
125
0
}