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