preproc: Fix nil dereference on error paths
[nasm.git] / asm / preproc-nop.c
blob575896edc4d921fbcaf828d54e8a1580d56fb672
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * This is a null preprocessor which just copies lines from input
36 * to output. It's used when someone explicitly requests that NASM
37 * not preprocess their source file.
40 #include "compiler.h"
42 #include "nctype.h"
43 #include <time.h>
45 #include "nasm.h"
46 #include "nasmlib.h"
47 #include "error.h"
48 #include "preproc.h"
49 #include "listing.h"
51 #define BUF_DELTA 512
53 static FILE *nop_fp;
54 static int32_t nop_lineinc;
56 static void nop_init(void)
58 /* Nothing to do */
61 static void nop_reset(const char *file, enum preproc_mode mode,
62 struct strlist *deplist)
64 (void)mode; /* placate compilers */
66 src_set(0, file);
67 nop_lineinc = 1;
68 nop_fp = nasm_open_read(file, NF_TEXT);
70 if (!nop_fp)
71 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
73 strlist_add(deplist, file);
76 static char *nop_getline(void)
78 char *buffer, *p, *q;
79 int bufsize;
81 bufsize = BUF_DELTA;
82 buffer = nasm_malloc(BUF_DELTA);
83 src_set_linnum(src_get_linnum() + nop_lineinc);
85 while (1) { /* Loop to handle %line */
86 p = buffer;
87 while (1) { /* Loop to handle long lines */
88 q = fgets(p, bufsize - (p - buffer), nop_fp);
89 if (!q)
90 break;
91 p += strlen(p);
92 if (p > buffer && p[-1] == '\n')
93 break;
94 if (p - buffer > bufsize - 10) {
95 int offset;
96 offset = p - buffer;
97 bufsize += BUF_DELTA;
98 buffer = nasm_realloc(buffer, bufsize);
99 p = buffer + offset;
103 if (!q && p == buffer) {
104 nasm_free(buffer);
105 return NULL;
109 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
110 * them are present at the end of the line.
112 buffer[strcspn(buffer, "\r\n\032")] = '\0';
114 if (!nasm_strnicmp(buffer, "%line", 5)) {
115 int32_t ln;
116 int li;
117 char *nm = nasm_malloc(strlen(buffer));
118 int conv = sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm);
119 if (conv >= 2) {
120 if (!pp_noline)
121 src_set(ln, conv >= 3 ? nm : NULL);
122 nop_lineinc = li;
124 nasm_free(nm);
125 if (conv >= 2)
126 continue;
128 break;
131 lfmt->line(LIST_READ, buffer);
133 return buffer;
136 static void nop_cleanup_pass(void)
138 if (nop_fp) {
139 fclose(nop_fp);
140 nop_fp = NULL;
144 static void nop_cleanup_session(void)
146 /* Nothing we need to do */
149 static void nop_extra_stdmac(macros_t *macros)
151 (void)macros;
154 static void nop_pre_define(char *definition)
156 (void)definition;
159 static void nop_pre_undefine(char *definition)
161 (void)definition;
164 static void nop_pre_include(char *fname)
166 (void)fname;
169 static void nop_pre_command(const char *what, char *string)
171 (void)what;
172 (void)string;
175 static void nop_include_path(struct strlist *list)
177 (void)list;
180 static void nop_error_list_macros(errflags severity)
182 (void)severity;
185 const struct preproc_ops preproc_nop = {
186 nop_init,
187 nop_reset,
188 nop_getline,
189 nop_cleanup_pass,
190 nop_cleanup_session,
191 nop_extra_stdmac,
192 nop_pre_define,
193 nop_pre_undefine,
194 nop_pre_include,
195 nop_pre_command,
196 nop_include_path,
197 nop_error_list_macros,