Module Name: src
Committed By: rillig
Date: Sat Mar 30 17:12:26 UTC 2024
Modified Files:
src/tests/usr.bin/xlint/lint1: msg_168.c platform_ilp32_int.c
platform_lp64.c queries.c
src/usr.bin/xlint/lint1: err.c init.c tree.c
Log Message:
lint: do not convert array subscripts from size_t to ptrdiff_t
The C standards do not specify a fixed type for an array subscript, it
just has to be an integer type. Previously, query 4 fired for the
ubiquitous expression 'ptr[sz]' when sz had type 'size_t'.
The test platform_ilp32_long is unaffected by this change, as the
integer constant 0x80000000 has type 'unsigned int', while size_t is
'unsigned long' on those platforms, and even though the types 'unsigned
int' and 'unsigned long' have the same value space, there's still a
conversion, at least for now.
To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/tests/usr.bin/xlint/lint1/msg_168.c
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/platform_ilp32_int.c
cvs rdiff -u -r1.13 -r1.14 src/tests/usr.bin/xlint/lint1/platform_lp64.c
cvs rdiff -u -r1.24 -r1.25 src/tests/usr.bin/xlint/lint1/queries.c
cvs rdiff -u -r1.237 -r1.238 src/usr.bin/xlint/lint1/err.c
cvs rdiff -u -r1.267 -r1.268 src/usr.bin/xlint/lint1/init.c
cvs rdiff -u -r1.630 -r1.631 src/usr.bin/xlint/lint1/tree.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/tests/usr.bin/xlint/lint1/msg_168.c
diff -u src/tests/usr.bin/xlint/lint1/msg_168.c:1.12 src/tests/usr.bin/xlint/lint1/msg_168.c:1.13
--- src/tests/usr.bin/xlint/lint1/msg_168.c:1.12 Sat Mar 30 16:47:45 2024
+++ src/tests/usr.bin/xlint/lint1/msg_168.c Sat Mar 30 17:12:26 2024
@@ -1,7 +1,7 @@
-/* $NetBSD: msg_168.c,v 1.12 2024/03/30 16:47:45 rillig Exp $ */
+/* $NetBSD: msg_168.c,v 1.13 2024/03/30 17:12:26 rillig Exp $ */
# 3 "msg_168.c"
-// Test for message: array subscript %jd cannot be > %d [168]
+// Test for message: array subscript %ju cannot be > %d [168]
/* lint1-extra-flags: -X 351 */
Index: src/tests/usr.bin/xlint/lint1/platform_ilp32_int.c
diff -u src/tests/usr.bin/xlint/lint1/platform_ilp32_int.c:1.7 src/tests/usr.bin/xlint/lint1/platform_ilp32_int.c:1.8
--- src/tests/usr.bin/xlint/lint1/platform_ilp32_int.c:1.7 Sat Mar 30 16:47:45 2024
+++ src/tests/usr.bin/xlint/lint1/platform_ilp32_int.c Sat Mar 30 17:12:26 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: platform_ilp32_int.c,v 1.7 2024/03/30 16:47:45 rillig Exp $ */
+/* $NetBSD: platform_ilp32_int.c,v 1.8 2024/03/30 17:12:26 rillig Exp $ */
# 3 "platform_ilp32_int.c"
/*
@@ -44,17 +44,13 @@ array_index(void)
/* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */
/* expect+1: warning: array subscript -2147483648 cannot be negative [167] */
u8 += u8_buf[2147483648];
- /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */
- /* expect+1: warning: array subscript -2147483648 cannot be negative [167] */
+ /* expect+1: warning: array subscript 2147483648 cannot be > 19 [168] */
u8 += u8_buf[0x80000000];
- /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */
- /* expect+1: warning: array subscript -1 cannot be negative [167] */
+ /* expect+1: warning: array subscript 4294967295 cannot be > 19 [168] */
u8 += u8_buf[0xffffffff];
- /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */
- /* expect+1: warning: array subscript -2147483648 cannot be negative [167] */
+ /* expect+1: warning: array subscript 2147483648 cannot be > 19 [168] */
u8 += u8_buf[0x80000000];
- /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */
- /* expect+1: warning: array subscript -1 cannot be negative [167] */
+ /* expect+1: warning: array subscript 4294967295 cannot be > 19 [168] */
u8 += u8_buf[0xffffffff];
/* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */
/* expect+1: warning: array subscript -1 cannot be negative [167] */
@@ -71,19 +67,15 @@ array_index(void)
/* expect+2: warning: '-2147483648 * 8' overflows 'int' [141] */
/* expect+1: warning: array subscript -268435456 cannot be negative [167] */
u64 += u64_buf[2147483648];
- /* expect+3: warning: conversion of 'unsigned int' to 'int' is out of range [119] */
- /* expect+2: warning: '-2147483648 * 8' overflows 'int' [141] */
- /* expect+1: warning: array subscript -268435456 cannot be negative [167] */
+ /* expect+1: warning: '2147483648 * 8' overflows 'unsigned int' [141] */
u64 += u64_buf[0x80000000];
- /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */
- /* expect+1: warning: array subscript -1 cannot be negative [167] */
+ /* expect+2: warning: '4294967295 * 8' overflows 'unsigned int' [141] */
+ /* expect+1: warning: array subscript 536870911 cannot be > 19 [168] */
u64 += u64_buf[0xffffffff];
- /* expect+3: warning: conversion of 'unsigned int' to 'int' is out of range [119] */
- /* expect+2: warning: '-2147483648 * 8' overflows 'int' [141] */
- /* expect+1: warning: array subscript -268435456 cannot be negative [167] */
+ /* expect+1: warning: '2147483648 * 8' overflows 'unsigned int' [141] */
u64 += u64_buf[0x80000000];
- /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */
- /* expect+1: warning: array subscript -1 cannot be negative [167] */
+ /* expect+2: warning: '4294967295 * 8' overflows 'unsigned int' [141] */
+ /* expect+1: warning: array subscript 536870911 cannot be > 19 [168] */
u64 += u64_buf[0xffffffff];
/* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */
/* expect+1: warning: array subscript -1 cannot be negative [167] */
Index: src/tests/usr.bin/xlint/lint1/platform_lp64.c
diff -u src/tests/usr.bin/xlint/lint1/platform_lp64.c:1.13 src/tests/usr.bin/xlint/lint1/platform_lp64.c:1.14
--- src/tests/usr.bin/xlint/lint1/platform_lp64.c:1.13 Sat Mar 30 16:47:45 2024
+++ src/tests/usr.bin/xlint/lint1/platform_lp64.c Sat Mar 30 17:12:26 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: platform_lp64.c,v 1.13 2024/03/30 16:47:45 rillig Exp $ */
+/* $NetBSD: platform_lp64.c,v 1.14 2024/03/30 17:12:26 rillig Exp $ */
# 3 "platform_lp64.c"
/*
@@ -70,7 +70,7 @@ array_index(void)
u8 += u8_buf[0xffffffff];
/* expect+1: warning: array subscript 72057594037927935 cannot be > 19 [168] */
u8 += u8_buf[0x00ffffffffffffff];
- /* expect+1: warning: array subscript -1 cannot be negative [167] */
+ /* expect+1: warning: array subscript 18446744073709551615 cannot be > 19 [168] */
u8 += u8_buf[0xffffffffffffffff];
/* expect+1: warning: array subscript 16777215 cannot be > 19 [168] */
@@ -100,6 +100,7 @@ array_index(void)
/* expect+2: warning: '9223372036854775807 * 8' overflows 'long' [141] */
/* expect+1: warning: array subscript 1152921504606846975 cannot be > 19 [168] */
u64 += u64_buf[0x7fffffffffffffff];
- /* expect+1: warning: array subscript -1 cannot be negative [167] */
+ /* expect+2: warning: '18446744073709551615 * 8' overflows 'unsigned long' [141] */
+ /* expect+1: warning: array subscript 2305843009213693951 cannot be > 19 [168] */
u64 += u64_buf[0xffffffffffffffff];
}
Index: src/tests/usr.bin/xlint/lint1/queries.c
diff -u src/tests/usr.bin/xlint/lint1/queries.c:1.24 src/tests/usr.bin/xlint/lint1/queries.c:1.25
--- src/tests/usr.bin/xlint/lint1/queries.c:1.24 Fri Feb 2 19:07:58 2024
+++ src/tests/usr.bin/xlint/lint1/queries.c Sat Mar 30 17:12:26 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: queries.c,v 1.24 2024/02/02 19:07:58 rillig Exp $ */
+/* $NetBSD: queries.c,v 1.25 2024/03/30 17:12:26 rillig Exp $ */
# 3 "queries.c"
/*
@@ -34,6 +34,7 @@ typedef double _Complex c64_t;
typedef char *str_t;
typedef const char *cstr_t;
typedef volatile char *vstr_t;
+typedef typeof(sizeof 0) size_t;
_Bool cond;
@@ -101,7 +102,7 @@ Q3(int i, unsigned u)
}
unsigned long long
-Q4(signed char *ptr, int i, unsigned long long ull)
+Q4(signed char *ptr, int i, unsigned long long ull, size_t sz)
{
/*
@@ -126,6 +127,8 @@ Q4(signed char *ptr, int i, unsigned lon
/* expect+1: implicit conversion changes sign from 'int' to 'unsigned int' [Q3] */
u32 = u32 & u8;
+ s8 = ptr[sz];
+
/*
* The conversion from 'signed char' to 'int' is done by the integer
* promotions (C11 6.3.1.1p2), not by the usual arithmetic
@@ -351,9 +354,9 @@ Q9(int x)
return (0.0);
case 9:
return
-# 355 "queries.c" 3 4
+# 358 "queries.c" 3 4
((void *)0)
-# 357 "queries.c"
+# 360 "queries.c"
/* expect+1: warning: illegal combination of integer 'int' and pointer 'pointer to void' [183] */
;
case 10:
Index: src/usr.bin/xlint/lint1/err.c
diff -u src/usr.bin/xlint/lint1/err.c:1.237 src/usr.bin/xlint/lint1/err.c:1.238
--- src/usr.bin/xlint/lint1/err.c:1.237 Sat Mar 30 16:47:44 2024
+++ src/usr.bin/xlint/lint1/err.c Sat Mar 30 17:12:26 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: err.c,v 1.237 2024/03/30 16:47:44 rillig Exp $ */
+/* $NetBSD: err.c,v 1.238 2024/03/30 17:12:26 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: err.c,v 1.237 2024/03/30 16:47:44 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.238 2024/03/30 17:12:26 rillig Exp $");
#endif
#include <limits.h>
@@ -223,7 +223,7 @@ static const char *const msgs[] = {
"constant truncated by assignment", // 165
"precision lost in bit-field assignment", // 166
"array subscript %jd cannot be negative", // 167
- "array subscript %jd cannot be > %d", // 168
+ "array subscript %ju cannot be > %d", // 168
"precedence confusion possible: parenthesize!", // 169
"first operand of '?' must have scalar type", // 170
"cannot assign to '%s' from '%s'", // 171
Index: src/usr.bin/xlint/lint1/init.c
diff -u src/usr.bin/xlint/lint1/init.c:1.267 src/usr.bin/xlint/lint1/init.c:1.268
--- src/usr.bin/xlint/lint1/init.c:1.267 Sat Mar 30 16:47:44 2024
+++ src/usr.bin/xlint/lint1/init.c Sat Mar 30 17:12:26 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: init.c,v 1.267 2024/03/30 16:47:44 rillig Exp $ */
+/* $NetBSD: init.c,v 1.268 2024/03/30 17:12:26 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -38,7 +38,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: init.c,v 1.267 2024/03/30 16:47:44 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.268 2024/03/30 17:12:26 rillig Exp $");
#endif
#include <stdlib.h>
@@ -801,8 +801,8 @@ initialization_add_designator_subscript(
}
if (!tp->t_incomplete_array && subscript >= (size_t)tp->u.dimension) {
- /* array subscript %jd cannot be > %d */
- error(168, (intmax_t)subscript, tp->u.dimension - 1);
+ /* array subscript %ju cannot be > %d */
+ error(168, (uintmax_t)subscript, tp->u.dimension - 1);
subscript = 0; /* suppress further errors */
}
Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.630 src/usr.bin/xlint/lint1/tree.c:1.631
--- src/usr.bin/xlint/lint1/tree.c:1.630 Sat Mar 30 16:47:44 2024
+++ src/usr.bin/xlint/lint1/tree.c Sat Mar 30 17:12:26 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: tree.c,v 1.630 2024/03/30 16:47:44 rillig Exp $ */
+/* $NetBSD: tree.c,v 1.631 2024/03/30 17:12:26 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.630 2024/03/30 16:47:44 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.631 2024/03/30 17:12:26 rillig Exp $");
#endif
#include <float.h>
@@ -1183,14 +1183,17 @@ build_plus_minus(op_t op, bool sys, tnod
}
/* pointer +- integer */
- if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
- lint_assert(is_integer(rn->tn_type->t_tspec));
+ tspec_t lt = ln->tn_type->t_tspec;
+ tspec_t rt = rn->tn_type->t_tspec;
+ if (lt == PTR && rt != PTR) {
+ lint_assert(is_integer(rt));
check_ctype_macro_invocation(ln, rn);
check_enum_array_index(ln, rn);
tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
- if (rn->tn_type->t_tspec != elsz->tn_type->t_tspec)
+ tspec_t szt = elsz->tn_type->t_tspec;
+ if (rt != szt && rt != unsigned_type(szt))
rn = convert(NOOP, 0, elsz->tn_type, rn);
tnode_t *prod = build_op(MULT, sys, rn->tn_type, rn, elsz);
@@ -1201,8 +1204,8 @@ build_plus_minus(op_t op, bool sys, tnod
}
/* pointer - pointer */
- if (rn->tn_type->t_tspec == PTR) {
- lint_assert(ln->tn_type->t_tspec == PTR);
+ if (rt == PTR) {
+ lint_assert(lt == PTR);
lint_assert(op == MINUS);
type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
@@ -4438,7 +4441,7 @@ proceed:;
/* array subscript %jd cannot be negative */
warning(167, (intmax_t)con);
else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
- /* array subscript %jd cannot be > %d */
+ /* array subscript %ju cannot be > %d */
warning(168, (uintmax_t)con, dim - 1);
}