annotate lwcc/symbol.c @ 577:e49d24f4a9a5

Correct bug in the object file output code leading to stack corruption It turns out leaving a pointer to a stack allocated temporary in a persistent data structure is not conducive to correct program operation. Undo the export check setup in the object file output sequence so a pointer to stack allocated memory is not left hanging when the function returns. This seems to correct at least one mysterious crash bug, and possibly others. Thanks to Boisy Pitre for reporting the crash bug that led to this discovery, as well as a previous crash bug that likely has the same root cause. Additional thanks to Ciaran Anscomb whose debugger wielding wizardry revealed the exact location of this particular bit of unbrilliance.
author William Astle <lost@l-w.ca>
date Sat, 03 Aug 2024 14:30:06 -0600
parents 54f213c8fb81
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
296
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/symbol.c
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
3
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
5
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
7
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
12
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
17
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
21
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <string.h>
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
23
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
24 #include <lw_alloc.h>
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
25 #include <lw_string.h>
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
26
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
27 #include "cpp.h"
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include "symbol.h"
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
29 #include "token.h"
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
30
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
31 void symbol_free(struct symtab_e *s)
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
32 {
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
33 int i;
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 296
diff changeset
34
296
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
35 lw_free(s -> name);
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
36
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
37 for (i = 0; i < s -> nargs; i++)
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
38 lw_free(s -> params[i]);
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
39 lw_free(s -> params);
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 296
diff changeset
40 token_list_destroy(s -> tl);
296
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
41 }
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
42
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
43 struct symtab_e *symtab_find(struct preproc_info *pp, char *name)
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
44 {
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
45 struct symtab_e *s;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
46
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
47 for (s = pp -> sh; s; s = s -> next)
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
48 {
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
49 if (strcmp(s -> name, name) == 0)
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
50 {
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
51 return s;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
52 }
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
53 }
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
54 return NULL;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
55 }
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
56
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
57 void symtab_undef(struct preproc_info *pp, char *name)
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
58 {
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
59 struct symtab_e *s, **p;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
60
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
61 p = &(pp -> sh);
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
62 for (s = pp -> sh; s; s = s -> next)
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
63 {
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
64 if (strcmp(s -> name, name) == 0)
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
65 {
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
66 (*p) -> next = s -> next;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
67 symbol_free(s);
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
68 return;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
69 }
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
70 p = &((*p) -> next);
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
71 }
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
72 }
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
73
299
856caf91ffaa Added token list structure and switched some stuff to use it
William Astle <lost@l-w.ca>
parents: 296
diff changeset
74 void symtab_define(struct preproc_info *pp, char *name, struct token_list *def, int nargs, char **params, int vargs)
296
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
75 {
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
76 struct symtab_e *s;
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
77 int i;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
78
296
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
79 s = lw_alloc(sizeof(struct symtab_e));
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
80 s -> name = lw_strdup(name);
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
81 s -> tl = def;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
82 s -> nargs = nargs;
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
83 s -> params = NULL;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
84 if (params)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
85 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
86 s -> params = lw_alloc(sizeof(char *) * nargs);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
87 for (i = 0; i < nargs; i++)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
88 s -> params[i] = lw_strdup(params[i]);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
89 }
296
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
90 s -> vargs = vargs;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
91 s -> next = pp -> sh;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
92 pp -> sh = s;
83fcc1ed6ad6 Checkpoint lwcc development
William Astle <lost@l-w.ca>
parents:
diff changeset
93 }
305
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
94
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
95 void symtab_dump(struct preproc_info *pp)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
96 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
97 struct symtab_e *s;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
98 struct token *t;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
99 int i;
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
100
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
101 for (s = pp -> sh; s; s = s -> next)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
102 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
103 printf("%s", s -> name);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
104 if (s -> nargs >= 0)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
105 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
106 printf("(");
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
107 for (i = 0; i < s -> nargs; i++)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
108 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
109 if (i)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
110 printf(",");
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
111 printf("%s", s -> params[i]);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
112 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
113 if (s -> vargs)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
114 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
115 if (s -> nargs)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
116 printf(",");
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
117 printf("...");
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
118 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
119 printf(")");
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
120 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
121 printf(" => ");
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
122 if (s -> tl)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
123 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
124 for (t = s -> tl -> head; t; t = t -> next)
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
125 {
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
126 token_print(t, stdout);
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
127 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
128 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
129 printf("\n");
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
130 }
54f213c8fb81 Various bugfixes and output tuning
William Astle <lost@l-w.ca>
parents: 299
diff changeset
131 }