Sat Feb 26 19:01:09 2022 UTC ()
lint: extract code for handling statement expressions from the grammar

This prepares the fix of the memory corruption bug that is demonstrated
in t_integration.sh, test case assertion_failures.

No functional change.


(rillig)
diff -r1.379 -r1.380 src/usr.bin/xlint/lint1/cgram.y
diff -r1.144 -r1.145 src/usr.bin/xlint/lint1/externs1.h
diff -r1.402 -r1.403 src/usr.bin/xlint/lint1/tree.c

cvs diff -r1.379 -r1.380 src/usr.bin/xlint/lint1/cgram.y (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/cgram.y 2022/01/15 23:21:34 1.379
+++ src/usr.bin/xlint/lint1/cgram.y 2022/02/26 19:01:09 1.380
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1%{ 1%{
2/* $NetBSD: cgram.y,v 1.379 2022/01/15 23:21:34 rillig Exp $ */ 2/* $NetBSD: cgram.y,v 1.380 2022/02/26 19:01:09 rillig Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl 6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved. 7 * All Rights Reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#if defined(__RCSID) && !defined(lint) 37#if defined(__RCSID) && !defined(lint)
38__RCSID("$NetBSD: cgram.y,v 1.379 2022/01/15 23:21:34 rillig Exp $"); 38__RCSID("$NetBSD: cgram.y,v 1.380 2022/02/26 19:01:09 rillig Exp $");
39#endif 39#endif
40 40
41#include <limits.h> 41#include <limits.h>
42#include <stdlib.h> 42#include <stdlib.h>
43#include <string.h> 43#include <string.h>
44 44
45#include "lint1.h" 45#include "lint1.h"
46 46
47extern char *yytext; 47extern char *yytext;
48 48
49/* 49/*
50 * Contains the level of current declaration, used for symbol table entries. 50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body. 51 * 0 is the top-level, > 0 is inside a function body.
@@ -508,36 +508,29 @@ postfix_expression: @@ -508,36 +508,29 @@ postfix_expression:
508 } 508 }
509 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */ 509 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
510 sym_t *tmp = mktempsym($2); 510 sym_t *tmp = mktempsym($2);
511 begin_initialization(tmp); 511 begin_initialization(tmp);
512 cgram_declare(tmp, true, NULL); 512 cgram_declare(tmp, true, NULL);
513 } init_lbrace initializer_list comma_opt init_rbrace { 513 } init_lbrace initializer_list comma_opt init_rbrace {
514 if (!Sflag) 514 if (!Sflag)
515 /* compound literals are a C9X/GCC extension */ 515 /* compound literals are a C9X/GCC extension */
516 gnuism(319); 516 gnuism(319);
517 $$ = build_name(*current_initsym(), false); 517 $$ = build_name(*current_initsym(), false);
518 end_initialization(); 518 end_initialization();
519 } 519 }
520 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list { 520 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list {
521 block_level--; 521 do_statement_expr($3);
522 mem_block_level--; 
523 begin_initialization(mktempsym(dup_type($3->tn_type))); 
524 mem_block_level++; 
525 block_level++; 
526 /* ({ }) is a GCC extension */ 
527 gnuism(320); 
528 } compound_statement_rbrace T_RPAREN { 522 } compound_statement_rbrace T_RPAREN {
529 $$ = build_name(*current_initsym(), false); 523 $$ = end_statement_expr();
530 end_initialization(); 
531 } 524 }
532 ; 525 ;
533 526
534comma_opt: /* helper for 'postfix_expression' */ 527comma_opt: /* helper for 'postfix_expression' */
535 /* empty */ 528 /* empty */
536 | T_COMMA 529 | T_COMMA
537 ; 530 ;
538 531
539/* 532/*
540 * The inner part of a GCC statement-expression of the form ({ ... }). 533 * The inner part of a GCC statement-expression of the form ({ ... }).
541 * 534 *
542 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html 535 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
543 */ 536 */

cvs diff -r1.144 -r1.145 src/usr.bin/xlint/lint1/externs1.h (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/externs1.h 2021/12/21 21:04:08 1.144
+++ src/usr.bin/xlint/lint1/externs1.h 2022/02/26 19:01:09 1.145
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: externs1.h,v 1.144 2021/12/21 21:04:08 rillig Exp $ */ 1/* $NetBSD: externs1.h,v 1.145 2022/02/26 19:01:09 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -252,26 +252,29 @@ extern tnode_t *build_sizeof(const type_ @@ -252,26 +252,29 @@ extern tnode_t *build_sizeof(const type_
252extern tnode_t *build_offsetof(const type_t *, const sym_t *); 252extern tnode_t *build_offsetof(const type_t *, const sym_t *);
253extern tnode_t *build_alignof(const type_t *); 253extern tnode_t *build_alignof(const type_t *);
254extern tnode_t *cast(tnode_t *, type_t *); 254extern tnode_t *cast(tnode_t *, type_t *);
255extern tnode_t *build_function_argument(tnode_t *, tnode_t *); 255extern tnode_t *build_function_argument(tnode_t *, tnode_t *);
256extern tnode_t *build_function_call(tnode_t *, bool, tnode_t *); 256extern tnode_t *build_function_call(tnode_t *, bool, tnode_t *);
257extern val_t *constant(tnode_t *, bool); 257extern val_t *constant(tnode_t *, bool);
258extern void expr(tnode_t *, bool, bool, bool, bool); 258extern void expr(tnode_t *, bool, bool, bool, bool);
259extern void check_expr_misc(const tnode_t *, bool, bool, bool, 259extern void check_expr_misc(const tnode_t *, bool, bool, bool,
260 bool, bool, bool); 260 bool, bool, bool);
261extern bool constant_addr(const tnode_t *, const sym_t **, ptrdiff_t *); 261extern bool constant_addr(const tnode_t *, const sym_t **, ptrdiff_t *);
262extern strg_t *cat_strings(strg_t *, strg_t *); 262extern strg_t *cat_strings(strg_t *, strg_t *);
263extern unsigned int type_size_in_bits(const type_t *); 263extern unsigned int type_size_in_bits(const type_t *);
264 264
 265void do_statement_expr(tnode_t *);
 266tnode_t *end_statement_expr(void);
 267
265/* 268/*
266 * func.c 269 * func.c
267 */ 270 */
268extern sym_t *funcsym; 271extern sym_t *funcsym;
269extern bool reached; 272extern bool reached;
270extern bool warn_about_unreachable; 273extern bool warn_about_unreachable;
271extern bool seen_fallthrough; 274extern bool seen_fallthrough;
272extern int nargusg; 275extern int nargusg;
273extern pos_t argsused_pos; 276extern pos_t argsused_pos;
274extern int nvararg; 277extern int nvararg;
275extern pos_t vapos; 278extern pos_t vapos;
276extern int printflike_argnum; 279extern int printflike_argnum;
277extern pos_t printflike_pos; 280extern pos_t printflike_pos;

cvs diff -r1.402 -r1.403 src/usr.bin/xlint/lint1/tree.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/tree.c 2021/12/21 15:33:20 1.402
+++ src/usr.bin/xlint/lint1/tree.c 2022/02/26 19:01:09 1.403
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tree.c,v 1.402 2021/12/21 15:33:20 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.403 2022/02/26 19:01:09 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: tree.c,v 1.402 2021/12/21 15:33:20 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.403 2022/02/26 19:01:09 rillig Exp $");
41#endif 41#endif
42 42
43#include <float.h> 43#include <float.h>
44#include <limits.h> 44#include <limits.h>
45#include <math.h> 45#include <math.h>
46#include <signal.h> 46#include <signal.h>
47#include <stdlib.h> 47#include <stdlib.h>
48#include <string.h> 48#include <string.h>
49 49
50#include "lint1.h" 50#include "lint1.h"
51#include "cgram.h" 51#include "cgram.h"
52 52
53static tnode_t *build_integer_constant(tspec_t, int64_t); 53static tnode_t *build_integer_constant(tspec_t, int64_t);
@@ -4528,13 +4528,35 @@ check_precedence_confusion(tnode_t *tn) @@ -4528,13 +4528,35 @@ check_precedence_confusion(tnode_t *tn)
4528 lint_assert(is_binary(tn)); 4528 lint_assert(is_binary(tn));
4529 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left) 4529 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4530 continue; 4530 continue;
4531 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left) 4531 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4532 continue; 4532 continue;
4533 4533
4534 if (is_confusing_precedence(tn->tn_op, 4534 if (is_confusing_precedence(tn->tn_op,
4535 ln->tn_op, ln->tn_parenthesized, 4535 ln->tn_op, ln->tn_parenthesized,
4536 rn->tn_op, rn->tn_parenthesized)) { 4536 rn->tn_op, rn->tn_parenthesized)) {
4537 /* precedence confusion possible: parenthesize! */ 4537 /* precedence confusion possible: parenthesize! */
4538 warning(169); 4538 warning(169);
4539 } 4539 }
4540} 4540}
 4541
 4542void
 4543do_statement_expr(tnode_t *tn)
 4544{
 4545 block_level--;
 4546 mem_block_level--;
 4547 /* Use the initialization code as temporary symbol storage. */
 4548 begin_initialization(mktempsym(dup_type(tn->tn_type)));
 4549 mem_block_level++;
 4550 block_level++;
 4551 /* ({ }) is a GCC extension */
 4552 gnuism(320);
 4553
 4554}
 4555
 4556tnode_t *
 4557end_statement_expr(void)
 4558{
 4559 tnode_t *tn = build_name(*current_initsym(), false);
 4560 end_initialization();
 4561 return tn;
 4562}