Coverage Report

Created: 2025-07-11 06:57

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