OpenVAS Scanner 23.32.3
nasl_tree.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 2002-2004 Tenable Network Security
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "nasl_tree.h"
8
9#include "nasl_debug.h"
10#include "nasl_var.h"
11
12#include <glib.h> /* for g_free */
13#include <regex.h>
14#include <stdio.h> /* for printf, puts */
15#include <stdlib.h> /* for abort */
16#include <string.h> /* for memcpy */
17
18static tree_cell *
20{
21 return g_malloc0 (sizeof (tree_cell));
22}
23
26{
28 c->type = typ;
29 return c;
30}
31
33alloc_RE_cell (int lnb, int t, tree_cell *l, char *re_str, int *err_c)
34{
35 regex_t *re = g_malloc0 (sizeof (regex_t));
36 int e;
37
39 c->line_nb = lnb;
40 c->type = t; /* We could check the type... */
41 c->link[0] = l;
42 c->link[1] = FAKE_CELL;
43 e = regcomp (re, re_str, REG_EXTENDED | REG_NOSUB | REG_ICASE);
44 if (!e)
45 c->x.ref_val = re;
46 else
47 {
48 char errbuf[100];
49 regerror (e, re, errbuf, sizeof (errbuf));
50 nasl_perror (NULL, "Line %d: Cannot compile regex: %s (error %d: %s)\n",
51 lnb, re_str, e, errbuf);
52 g_free (re);
53 *err_c = *err_c + 1;
54 }
55 g_free (re_str);
56 return c;
57}
58
60alloc_expr_cell (int lnb, int t, tree_cell *l, tree_cell *r)
61{
63 c->line_nb = lnb;
64 c->type = t;
65 c->link[0] = l;
66 c->link[1] = r;
67
68 return c;
69}
70
73{
74 tree_cell *r;
75 int i;
76
77 if (tc == NULL)
78 return NULL;
79 else if (tc == FAKE_CELL)
80 return FAKE_CELL;
81
82 r = alloc_tree_cell ();
83 r->line_nb = tc->line_nb;
84 r->type = tc->type;
85 r->size = tc->size;
86
87 switch (tc->type)
88 {
89 case CONST_STR:
90 case CONST_DATA:
91 r->x.str_val = g_malloc0 (tc->size + 1);
92 memcpy (r->x.str_val, tc->x.str_val, tc->size);
93 break;
94 default:
95 r->x = tc->x;
96 break;
97 }
98
99 for (i = 0; i < 4; i++)
100 r->link[i] = dup_cell (tc->link[i]);
101 return r;
102}
103
104static void
106{
107 int i;
108 nasl_array *a;
109
110 if (c == NULL || c == FAKE_CELL)
111 return;
112 for (i = 0; i < 4; i++)
113 if (c->link[i] != NULL)
114 deref_cell (c->link[i]);
115
116 if (c->x.str_val != NULL)
117 switch (c->type)
118 {
119 case CONST_STR:
120 case CONST_DATA:
121#ifdef SCRATCH_FREED_MEMORY
122 if (c->size > 0)
123 memset (c->x.str_val, 0xFF, c->size);
124#endif
125 g_free (c->x.str_val);
126 break;
127
128 case CONST_REGEX:
129 case COMP_RE_MATCH:
130 case COMP_RE_NOMATCH:
131 if (c->x.ref_val != NULL)
132 {
133 regfree (c->x.ref_val);
134 g_free (c->x.ref_val);
135 }
136 break;
137
138 case DYN_ARRAY:
139 a = c->x.ref_val;
140 if (a != NULL)
141 {
142 free_array (a);
143 g_free (c->x.ref_val);
144 }
145 break;
146
147 case NODE_FUN_DEF:
148 case NODE_FUN_CALL:
149 case NODE_VAR:
150 case NODE_DECL:
151 case NODE_ARG:
152 case NODE_ARRAY_EL:
153 case NODE_FOREACH:
154 g_free (c->x.str_val);
155 break;
156 }
157#ifdef SCRATCH_FREED_MEMORY
158 memset (c, 0xFF, sizeof (*c));
159#endif
160 g_free (c);
161}
162
163void
165{
166 if (c == NULL || c == FAKE_CELL)
167 return;
168 c->ref_count++;
169 if (c->ref_count < 0)
170 {
171 nasl_perror (NULL, "ref_cell: ref count is negative!\n");
172 nasl_dump_tree (c);
173 abort ();
174 }
175}
176
177void
179{
180 if (c == NULL || c == FAKE_CELL)
181 return;
182 if (--c->ref_count < 0)
183 free_tree (c);
184}
185
186/* Debug */
187
188static char *node_names[] = {
189 "NODE_EMPTY", "NODE_IF_ELSE", "NODE_INSTR_L", "NODE_FOR",
190 "NODE_WHILE", "NODE_FOREACH", "NODE_REPEAT_UNTIL", "NODE_REPEATED",
191 "NODE_FUN_DEF", "NODE_FUN_CALL", "NODE_DECL", "NODE_ARG",
192 "NODE_RETURN", "NODE_BREAK", "NODE_CONTINUE",
193
194 "NODE_ARRAY_EL", "NODE_AFF", "NODE_VAR", "NODE_LOCAL",
195 "NODE_GLOBAL", "NODE_PLUS_EQ", "NODE_MINUS_EQ", "NODE_MULT_EQ",
196 "NODE_DIV_EQ", "NODE_MODULO_EQ",
197
198 "NODE_L_SHIFT_EQ", "NODE_R_SHIFT_EQ", "NODE_R_USHIFT_EQ", "EXPR_AND",
199 "EXPR_OR", "EXPR_NOT",
200
201 "EXPR_PLUS", "EXPR_MINUS", "EXPR_U_MINUS", "EXPR_MULT",
202 "EXPR_DIV", "EXPR_MODULO", "EXPR_EXPO",
203
204 "EXPR_BIT_AND", "EXPR_BIT_OR", "EXPR_BIT_XOR", "EXPR_BIT_NOT",
205 "EXPR_INCR", "EXPR_DECR", "EXPR_L_SHIFT", "EXPR_R_SHIFT",
206 "EXPR_R_USHIFT",
207
208 "COMP_MATCH", "COMP_NOMATCH", "COMP_RE_MATCH", "COMP_RE_NOMATCH",
209
210 "COMP_LT", "COMP_LE", "COMP_EQ", "COMP_NE",
211 "COMP_GT", "COMP_GE", "CONST_INT", "CONST_STR",
212 "CONST_DATA", "CONST_REGEX",
213
214 "ARRAY_ELEM",
215
216 "REF_VAR", "REF_ARRAY", "DYN_ARRAY"};
217
218static void
219prefix (int n, int i)
220{
221 int j;
222 for (j = 0; j < n; j++)
223 putchar (' ');
224 if (i <= 0)
225 fputs (" ", stdout);
226 else
227 printf ("%d: ", i);
228}
229
230char *
232{
233 static char txt[80];
234
235 if (c == NULL)
236 return "NULL";
237 else if (c == FAKE_CELL)
238 return "FAKE";
239 else
240 switch (c->type)
241 {
242 case CONST_INT:
243 snprintf (txt, sizeof (txt), "%ld", c->x.i_val);
244 break;
245 case CONST_STR:
246 case CONST_DATA: /* Beurk (English: Yuck) */
247 if ((unsigned int) c->size >= sizeof (txt) + 2)
248 {
249 snprintf (txt, sizeof (txt), "\"%s", c->x.str_val);
250 strcpy (txt + (sizeof (txt) - 5), "...\"");
251 }
252 else
253 snprintf (txt, sizeof (txt), "\"%s\"", c->x.str_val);
254 break;
255 default:
256 snprintf (txt, sizeof (txt), "???? (%s)", nasl_type_name (c->type));
257 break;
258 }
259 return txt;
260}
261
262static void
263dump_tree (const tree_cell *c, int n, int idx)
264{
265 int i;
266
267 if (c == NULL)
268 return;
269
270 prefix (n, idx);
271
272 if (c == FAKE_CELL)
273 {
274 puts ("* FAKE *");
275 return;
276 }
277
278 if (c->line_nb > 0)
279 printf ("L%d: ", c->line_nb);
280
281 if (c->type < 0
282 || (unsigned int) c->type >= sizeof (node_names) / sizeof (node_names[0]))
283 printf ("* UNKNOWN %d (0x%x)*\n", c->type, c->type);
284 else
285 printf ("%s (%d)\n", node_names[c->type], c->type);
286
287 prefix (n, idx);
288 printf ("Ref_count=%d", c->ref_count);
289 if (c->size > 0)
290 {
291 /*prefix(n, idx); */
292 printf ("\tSize=%d (0x%x)", c->size, c->size);
293 }
294 putchar ('\n');
295
296 switch (c->type)
297 {
298 case CONST_INT:
299 prefix (n, 0);
300 printf ("Val=%ld\n", c->x.i_val);
301 break;
302
303 case CONST_STR:
304 case CONST_DATA:
305 case NODE_VAR:
306 case NODE_FUN_DEF:
307 case NODE_FUN_CALL:
308 case NODE_DECL:
309 case NODE_ARG:
310 case NODE_ARRAY_EL:
311 case ARRAY_ELEM:
312 prefix (n, 0);
313 if (c->x.str_val == NULL)
314 printf ("Val=(null)\n");
315 else
316 printf ("Val=\"%s\"\n", c->x.str_val);
317 break;
318 case REF_VAR:
319 prefix (n, 0);
320 if (c->x.ref_val == NULL)
321 printf ("Ref=(null)\n");
322 else
323 {
324 named_nasl_var *v = c->x.ref_val;
325 printf ("Ref=(type=%d, name=%s, value=%s)\n", v->u.var_type,
326 v->var_name != NULL ? v->var_name : "(null)",
327 var2str (&v->u));
328 }
329 break;
330
331 case REF_ARRAY:
332 case DYN_ARRAY:
333 break;
334 }
335
336 for (i = 0; i < 4; i++)
337 {
338 dump_tree (c->link[i], n + 3, i + 1);
339 }
340}
341
342const char *
344{
345 static char txt4[4][32]; /* This function may be called 4 times in the same
346 expression */
347 static int i = 0;
348 char *txt;
349
350 if (i >= 4)
351 i = 0;
352 txt = txt4[i];
353
354 if (t >= 0 && (unsigned int) t < sizeof (node_names) / sizeof (node_names[0]))
355 snprintf (txt, 32, "%s (%d)", node_names[t], t);
356 else
357 snprintf (txt, 32, "*UNKNOWN* (%d)", t);
358 i++;
359 return txt;
360}
361
362void
364{
365 printf ("^^^^ %p ^^^^^\n", (void *) c);
366 if (c == NULL)
367 puts ("NULL CELL");
368 else if (c == FAKE_CELL)
369 puts ("FAKE CELL");
370 else
371 dump_tree (c, 0, 0);
372 printf ("vvvvvvvvvvvvvvvvvv\n");
373}
374
375char *
377{
378 static char txt[32];
379 if (c == NULL || c == FAKE_CELL || c->line_nb <= 0)
380 return "";
381 snprintf (txt, sizeof (txt), " at or near line %d ", c->line_nb);
382 return txt;
383}
384
385int
387{
388 if (pc == NULL || pc == FAKE_CELL)
389 return 1;
390 switch (pc->type)
391 {
392 case CONST_INT:
393 case CONST_STR:
394 case CONST_DATA:
395 case REF_ARRAY:
396 case DYN_ARRAY:
397 return 1;
398 default:
399 return 0;
400 }
401 /*NOTREACHED*/}
402
403 int
405 {
406 if (c == NULL || c == FAKE_CELL)
407 return 0;
408 else
409 return c->type;
410 }
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition nasl_debug.c:105
tree_cell * dup_cell(const tree_cell *tc)
Definition nasl_tree.c:72
void ref_cell(tree_cell *c)
Definition nasl_tree.c:164
char * dump_cell_val(const tree_cell *c)
Definition nasl_tree.c:231
void nasl_dump_tree(const tree_cell *c)
Definition nasl_tree.c:363
int cell_type(const tree_cell *c)
Definition nasl_tree.c:404
static tree_cell * alloc_tree_cell()
Definition nasl_tree.c:19
tree_cell * alloc_RE_cell(int lnb, int t, tree_cell *l, char *re_str, int *err_c)
Definition nasl_tree.c:33
const char * nasl_type_name(int t)
Definition nasl_tree.c:343
static char * node_names[]
Definition nasl_tree.c:188
int nasl_is_leaf(const tree_cell *pc)
Definition nasl_tree.c:386
static void free_tree(tree_cell *c)
Definition nasl_tree.c:105
static void dump_tree(const tree_cell *c, int n, int idx)
Definition nasl_tree.c:263
tree_cell * alloc_expr_cell(int lnb, int t, tree_cell *l, tree_cell *r)
Definition nasl_tree.c:60
tree_cell * alloc_typed_cell(int typ)
Definition nasl_tree.c:25
static void prefix(int n, int i)
Definition nasl_tree.c:219
char * get_line_nb(const tree_cell *c)
Definition nasl_tree.c:376
void deref_cell(tree_cell *c)
Definition nasl_tree.c:178
@ NODE_ARRAY_EL
Definition nasl_tree.h:29
@ NODE_FUN_CALL
Definition nasl_tree.h:22
@ CONST_DATA
Definition nasl_tree.h:82
@ COMP_RE_MATCH
Definition nasl_tree.h:69
@ NODE_VAR
Definition nasl_tree.h:31
@ NODE_ARG
Definition nasl_tree.h:24
@ NODE_FUN_DEF
Definition nasl_tree.h:21
@ ARRAY_ELEM
Definition nasl_tree.h:85
@ REF_VAR
Definition nasl_tree.h:88
@ DYN_ARRAY
Definition nasl_tree.h:90
@ CONST_REGEX
Definition nasl_tree.h:83
@ COMP_RE_NOMATCH
Definition nasl_tree.h:70
@ NODE_FOREACH
Definition nasl_tree.h:18
@ NODE_DECL
Definition nasl_tree.h:23
@ CONST_STR
Definition nasl_tree.h:80
@ CONST_INT
Definition nasl_tree.h:79
@ REF_ARRAY
Definition nasl_tree.h:89
struct TC tree_cell
#define FAKE_CELL
Definition nasl_tree.h:110
const char * var2str(anon_nasl_var *v)
Definition nasl_var.c:1062
void free_array(nasl_array *a)
Definition nasl_var.c:339
struct st_nasl_array nasl_array
struct st_n_nasl_var named_nasl_var
struct TC * link[4]
Definition nasl_tree.h:107
short ref_count
Definition nasl_tree.h:98
int size
Definition nasl_tree.h:99
long int i_val
Definition nasl_tree.h:104
union TC::@332262321161220155002104006201360276211317150140 x
short line_nb
Definition nasl_tree.h:96
char * str_val
Definition nasl_tree.h:103
void * ref_val
Definition nasl_tree.h:105
short type
Definition nasl_tree.h:95
char * var_name
Definition nasl_var.h:58
struct st_a_nasl_var u
Definition nasl_var.h:56