Sun Mar 28 18:33:27 2021 UTC ()
lint: inline variable in initialization_init_array_using_string

No functional change.


(rillig)
diff -r1.170 -r1.171 src/usr.bin/xlint/lint1/init.c

cvs diff -r1.170 -r1.171 src/usr.bin/xlint/lint1/init.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/init.c 2021/03/28 18:28:22 1.170
+++ src/usr.bin/xlint/lint1/init.c 2021/03/28 18:33:27 1.171
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: init.c,v 1.170 2021/03/28 18:28:22 rillig Exp $ */ 1/* $NetBSD: init.c,v 1.171 2021/03/28 18:33:27 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: init.c,v 1.170 2021/03/28 18:28:22 rillig Exp $"); 40__RCSID("$NetBSD: init.c,v 1.171 2021/03/28 18:33:27 rillig Exp $");
41#endif 41#endif
42 42
43#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h> 44#include <string.h>
45 45
46#include "lint1.h" 46#include "lint1.h"
47 47
48 48
49/* 49/*
50 * Initialization 50 * Initialization
51 * 51 *
52 * Handles initializations of global or local objects, like in: 52 * Handles initializations of global or local objects, like in:
53 * 53 *
@@ -1169,27 +1169,26 @@ is_string_array(const type_t *tp, tspec_ @@ -1169,27 +1169,26 @@ is_string_array(const type_t *tp, tspec_
1169 if (tp == NULL || tp->t_tspec != ARRAY) 1169 if (tp == NULL || tp->t_tspec != ARRAY)
1170 return false; 1170 return false;
1171 st = tp->t_subt->t_tspec; 1171 st = tp->t_subt->t_tspec;
1172 return t == CHAR 1172 return t == CHAR
1173 ? st == CHAR || st == UCHAR || st == SCHAR 1173 ? st == CHAR || st == UCHAR || st == SCHAR
1174 : st == WCHAR; 1174 : st == WCHAR;
1175} 1175}
1176 1176
1177/* Initialize a character array or wchar_t array with a string literal. */ 1177/* Initialize a character array or wchar_t array with a string literal. */
1178static bool 1178static bool
1179initialization_init_array_using_string(struct initialization *in, tnode_t *tn) 1179initialization_init_array_using_string(struct initialization *in, tnode_t *tn)
1180{ 1180{
1181 struct brace_level *level; 1181 struct brace_level *level;
1182 int len; 
1183 strg_t *strg; 1182 strg_t *strg;
1184 1183
1185 if (tn->tn_op != STRING) 1184 if (tn->tn_op != STRING)
1186 return false; 1185 return false;
1187 1186
1188 debug_enter(); 1187 debug_enter();
1189 1188
1190 level = in->brace_level; 1189 level = in->brace_level;
1191 strg = tn->tn_string; 1190 strg = tn->tn_string;
1192 1191
1193 /* 1192 /*
1194 * Check if we have an array type which can be initialized by 1193 * Check if we have an array type which can be initialized by
1195 * the string. 1194 * the string.
@@ -1203,40 +1202,37 @@ initialization_init_array_using_string(s @@ -1203,40 +1202,37 @@ initialization_init_array_using_string(s
1203 1202
1204 } else if (is_string_array(level->bl_type, strg->st_tspec)) { 1203 } else if (is_string_array(level->bl_type, strg->st_tspec)) {
1205 debug_step("type is an array"); 1204 debug_step("type is an array");
1206 1205
1207 /* 1206 /*
1208 * If the array is already partly initialized, we are 1207 * If the array is already partly initialized, we are
1209 * wrong here. 1208 * wrong here.
1210 */ 1209 */
1211 if (level->bl_remaining != level->bl_type->t_dim) 1210 if (level->bl_remaining != level->bl_type->t_dim)
1212 goto nope; 1211 goto nope;
1213 } else 1212 } else
1214 goto nope; 1213 goto nope;
1215 1214
1216 /* Get length without trailing NUL character. */ 
1217 len = strg->st_len; 
1218 
1219 if (level->bl_array_of_unknown_size) { 1215 if (level->bl_array_of_unknown_size) {
1220 level->bl_array_of_unknown_size = false; 1216 level->bl_array_of_unknown_size = false;
1221 level->bl_type->t_dim = len + 1; 1217 level->bl_type->t_dim = (int)(strg->st_len + 1);
1222 setcomplete(level->bl_type, true); 1218 setcomplete(level->bl_type, true);
1223 } else { 1219 } else {
1224 /* 1220 /*
1225 * TODO: check for buffer overflow in the object to be 1221 * TODO: check for buffer overflow in the object to be
1226 * initialized 1222 * initialized
1227 */ 1223 */
1228 /* XXX: double-check for off-by-one error */ 1224 /* XXX: double-check for off-by-one error */
1229 if (level->bl_type->t_dim < len) { 1225 if (level->bl_type->t_dim < (int)strg->st_len) {
1230 /* non-null byte ignored in string initializer */ 1226 /* non-null byte ignored in string initializer */
1231 warning(187); 1227 warning(187);
1232 } 1228 }
1233 1229
1234 /* 1230 /*
1235 * TODO: C99 6.7.8p14 allows a string literal to be enclosed 1231 * TODO: C99 6.7.8p14 allows a string literal to be enclosed
1236 * in optional redundant braces, just like scalars. Add tests 1232 * in optional redundant braces, just like scalars. Add tests
1237 * for this. 1233 * for this.
1238 */ 1234 */
1239 } 1235 }
1240 1236
1241 /* In every case the array is initialized completely. */ 1237 /* In every case the array is initialized completely. */
1242 level->bl_remaining = 0; 1238 level->bl_remaining = 0;