/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 | 64.5k | { |
51 | 64.5k | ssize_t len, total = 0; |
52 | 64.5k | size_t bufsize, linesize = 0; |
53 | 64.5k | char *cp, *line = NULL; |
54 | 64.5k | bool continued, comment; |
55 | 64.5k | debug_decl(sudo_parseln, SUDO_DEBUG_UTIL); |
56 | | |
57 | 64.5k | do { |
58 | 64.5k | comment = false; |
59 | 64.5k | continued = false; |
60 | 64.5k | len = getdelim(&line, &linesize, '\n', fp); |
61 | 64.5k | if (len == -1) |
62 | 38.8k | break; |
63 | 25.6k | if (lineno != NULL) |
64 | 0 | (*lineno)++; |
65 | | |
66 | | /* Remove trailing newline(s) if present. */ |
67 | 51.3k | while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) |
68 | 25.6k | line[--len] = '\0'; |
69 | | |
70 | | /* Remove comments or check for line continuation (but not both) */ |
71 | 25.6k | if ((cp = strchr(line, '#')) != NULL) { |
72 | 0 | if (cp == line || !ISSET(flags, PARSELN_COMM_BOL)) { |
73 | 0 | *cp = '\0'; |
74 | 0 | len = (ssize_t)(cp - line); |
75 | 0 | comment = true; |
76 | 0 | } |
77 | 0 | } |
78 | 25.6k | if (!comment && !ISSET(flags, PARSELN_CONT_IGN)) { |
79 | 0 | if (len > 0 && line[len - 1] == '\\' && (len == 1 || line[len - 2] != '\\')) { |
80 | 0 | line[--len] = '\0'; |
81 | 0 | continued = true; |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | /* Trim leading and trailing whitespace */ |
86 | 25.6k | if (!continued) { |
87 | 25.6k | while (len > 0 && isblank((unsigned char)line[len - 1])) |
88 | 0 | line[--len] = '\0'; |
89 | 25.6k | } |
90 | 25.6k | for (cp = line; isblank((unsigned char)*cp); cp++) |
91 | 0 | len--; |
92 | | |
93 | 25.6k | bufsize = (size_t)(total + len + 1); |
94 | 25.6k | if (*bufp == NULL || bufsize > *bufsizep) { |
95 | 25.6k | const size_t newsize = sudo_pow2_roundup(bufsize); |
96 | 25.6k | void *newbuf; |
97 | | |
98 | 25.6k | 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 | 25.6k | 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 | 25.6k | *bufp = newbuf; |
115 | 25.6k | *bufsizep = newsize; |
116 | 25.6k | } |
117 | 25.6k | memcpy(*bufp + total, cp, (size_t)(len + 1)); |
118 | 25.6k | total += len; |
119 | 25.6k | } while (continued); |
120 | 0 | free(line); |
121 | 64.5k | if (len == -1 && total == 0) |
122 | 38.8k | debug_return_ssize_t(-1); |
123 | 25.6k | debug_return_ssize_t(total); |
124 | 25.6k | } |
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 | } |