lint: add test for assertion failure in initialization

The 'cnt = level->bl_type->t_tspec == STRUCT ? 2 : 1;' in
initialization_push_struct_or_union is obviously wrong since not every
struct has exactly 1 remaining member after the first member that has an
initializer with designation.

This bug started its life in init.c 1.12 from 2002-10-21, a little over
18 years ago.
This commit is contained in:
rillig 2021-03-28 19:53:58 +00:00
parent 42f643d6d2
commit af1032a21b
2 changed files with 29 additions and 8 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: d_c99_init.c,v 1.18 2021/03/28 18:48:32 rillig Exp $ */
/* $NetBSD: d_c99_init.c,v 1.19 2021/03/28 19:53:58 rillig Exp $ */
# 3 "d_c99_init.c"
/*
@ -243,3 +243,24 @@ char message_with_suffix[] = {
/* expect+1: too many array initializers */
'\n',
};
struct ten {
int i0;
int i1;
int i2;
int i3;
int i4;
int i5;
int i6;
int i7;
int i8;
int i9;
};
struct ten ten = {
.i3 = 3,
4,
// FIXME: assertion "level->bl_type->t_tspec == ARRAY" failed in brace_level_extend_if_array_of_unknown_size
// 5,
// 6,
};

View File

@ -1,4 +1,4 @@
/* $NetBSD: init.c,v 1.172 2021/03/28 19:30:08 rillig Exp $ */
/* $NetBSD: init.c,v 1.173 2021/03/28 19:53:58 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: init.c,v 1.172 2021/03/28 19:30:08 rillig Exp $");
__RCSID("$NetBSD: init.c,v 1.173 2021/03/28 19:53:58 rillig Exp $");
#endif
#include <stdlib.h>
@ -589,8 +589,8 @@ brace_level_look_up_member(const struct brace_level *level, const char *name)
/* TODO: merge duplicate code */
static sym_t *
brace_level_look_up_member_named(struct brace_level *level, const char *name,
int *count)
brace_level_look_up_first_member_named(struct brace_level *level,
const char *name, int *count)
{
sym_t *m;
@ -609,7 +609,7 @@ brace_level_look_up_member_named(struct brace_level *level, const char *name,
/* TODO: merge duplicate code */
static sym_t *
brace_level_look_up_member_unnamed(struct brace_level *level, int *count)
brace_level_look_up_first_member_unnamed(struct brace_level *level, int *count)
{
sym_t *m;
@ -830,10 +830,10 @@ initialization_push_struct_or_union(struct initialization *in)
level->bl_seen_named_member ? ", seen named member" : "");
if (in->designation.head != NULL)
m = brace_level_look_up_member_named(level,
m = brace_level_look_up_first_member_named(level,
in->designation.head->name, &cnt);
else
m = brace_level_look_up_member_unnamed(level, &cnt);
m = brace_level_look_up_first_member_unnamed(level, &cnt);
if (in->designation.head != NULL) {
if (m == NULL) {