Module Name: src
Committed By: rillig
Date: Sun Jan 7 21:19:42 UTC 2024
Modified Files:
src/tests/usr.bin/xlint/lint1: msg_107.c msg_141.c msg_305.c
Log Message:
tests/lint: test operators, integer overflow, conversions
To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/msg_107.c
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/msg_141.c
cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_305.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_107.c
diff -u src/tests/usr.bin/xlint/lint1/msg_107.c:1.6 src/tests/usr.bin/xlint/lint1/msg_107.c:1.7
--- src/tests/usr.bin/xlint/lint1/msg_107.c:1.6 Fri Jul 7 19:45:22 2023
+++ src/tests/usr.bin/xlint/lint1/msg_107.c Sun Jan 7 21:19:42 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: msg_107.c,v 1.6 2023/07/07 19:45:22 rillig Exp $ */
+/* $NetBSD: msg_107.c,v 1.7 2024/01/07 21:19:42 rillig Exp $ */
# 3 "msg_107.c"
// Test for message: operands of '%s' have incompatible types '%s' and '%s' [107]
@@ -12,3 +12,12 @@ compare(double d, void *ptr)
/* expect+1: error: operands of '==' have incompatible types 'double' and 'pointer to void' [107] */
return d == ptr;
}
+
+static inline void
+typeok_minus(const char *p, int i, double d)
+{
+ if (p[i] != p[(int)d])
+ return;
+ /* expect+1: error: operands of '-' have incompatible types 'pointer to const char' and 'double' [107] */
+ return (p - i) - (p - d);
+}
Index: src/tests/usr.bin/xlint/lint1/msg_141.c
diff -u src/tests/usr.bin/xlint/lint1/msg_141.c:1.7 src/tests/usr.bin/xlint/lint1/msg_141.c:1.8
--- src/tests/usr.bin/xlint/lint1/msg_141.c:1.7 Sun Jul 9 11:01:27 2023
+++ src/tests/usr.bin/xlint/lint1/msg_141.c Sun Jan 7 21:19:42 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: msg_141.c,v 1.7 2023/07/09 11:01:27 rillig Exp $ */
+/* $NetBSD: msg_141.c,v 1.8 2024/01/07 21:19:42 rillig Exp $ */
# 3 "msg_141.c"
// Test for message: operator '%s' produces integer overflow [141]
@@ -9,13 +9,13 @@
* Before tree.c 1.347 from 2021-08-23, lint wrongly warned about integer
* overflow in '-'.
*/
-int signed_int_max = (1u << 31) - 1;
+int signed_int_max = (1U << 31) - 1;
/*
* Before tree.c 1.347 from 2021-08-23, lint wrongly warned about integer
* overflow in '-'.
*/
-unsigned int unsigned_int_max = (1u << 31) - 1;
+unsigned int unsigned_int_max = (1U << 31) - 1;
/* expect+1: warning: operator '+' produces integer overflow [141] */
int int_overflow = (1 << 30) + (1 << 30);
@@ -27,7 +27,184 @@ unsigned int intermediate_overflow = (1
unsigned int no_overflow = (1U << 30) + (1 << 30);
/* expect+1: warning: operator '-' produces integer overflow [141] */
-unsigned int unsigned_int_min = 0u - (1u << 31);
+unsigned int unsigned_int_min = 0U - (1U << 31);
/* expect+1: warning: operator '-' produces integer overflow [141] */
-unsigned int unsigned_int_min_unary = -(1u << 31);
+unsigned int unsigned_int_min_unary = -(1U << 31);
+
+enum {
+ INT_MAX = 2147483647,
+ INT_MIN = -INT_MAX - 1,
+};
+
+unsigned long long overflow_unsigned[] = {
+
+ // unary '+'
+
+ +0U,
+ +~0U,
+ +~0ULL,
+
+ // unary '-'
+
+ -0U,
+ -~0U,
+ /* expect+1: warning: operator '-' produces integer overflow [141] */
+ -(1ULL << 63),
+ /* expect+1: warning: operator '-' produces integer overflow [141] */
+ -(1U << 31),
+
+ // '~'
+
+ ~0U,
+ ~~0U,
+ ~0ULL,
+ ~~0ULL,
+
+ // '*'
+
+ 0x10001U * 0xffffU,
+ /* expect+1: warning: operator '*' produces integer overflow [141] */
+ 0x10000U * 0x10000U,
+ 0x10000ULL * 0x10000U,
+ /* expect+1: warning: operator '*' produces integer overflow [141] */
+ 0x100000000U * 0x100000000U,
+
+ // '/'
+
+ 0xFFFFFFFFU / ~0U,
+ /* expect+1: error: division by 0 [139] */
+ 0xFFFFFFFFU / 0U,
+
+ // '%'
+
+ 0xFFFFFFFFU % ~0U,
+ /* expect+1: error: modulus by 0 [140] */
+ 0xFFFFFFFFU % 0U,
+
+ // '+'
+
+ 0U + 0U,
+ ~0U + 0U,
+ 0U + ~0U,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ ~0U + 1U,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ 1U + ~0U,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ ~0U + ~0U,
+
+ // '-'
+
+ 0U - 0U,
+ 0U - 1U,
+ 0U - ~0U,
+
+ // '<<'
+
+ 256U << 23,
+ /* expect+1: warning: operator '<<' produces integer overflow [141] */
+ 256U << 24,
+
+ // '>>'
+
+ 10U >> 1,
+ -10U >> 1,
+
+ // bitwise operators
+
+ 1U & -1U,
+ 1U ^ -1U,
+ 1U | -1U,
+};
+
+long long overflow_signed[] = {
+
+ // unary '+'
+
+ +(unsigned char)255,
+ +0x7fffffffffffffff,
+
+ // unary '-'
+
+ -(unsigned char)255,
+ -0x7fffffffffffffff,
+
+ // '~'
+
+ ~0,
+ ~1,
+ ~1,
+ ~-1,
+
+ // '*'
+
+ /* expect+1: warning: operator '*' produces integer overflow [141] */
+ 0x10000 * 0x8000,
+ /* expect+1: warning: operator '*' produces integer overflow [141] */
+ 0x10000 * 0x10000,
+ 0x10000LL * 0x10000,
+ /* TODO: expect+1: warning: operator '*' produces integer overflow [141] */
+ // TODO: don't abort: 0x100000000 * 0x100000000
+
+ // '/'
+
+ -1 / INT_MIN, // TODO: integer overflow
+ -1 / INT_MAX,
+
+ // '%'
+
+ -1 % INT_MIN,
+ -1 % INT_MAX,
+ INT_MIN % -1,
+ INT_MAX % -1,
+
+ // '+'
+
+ INT_MIN + 0,
+ 0 + INT_MAX,
+ INT_MAX + 0,
+ 0 + INT_MIN,
+ INT_MIN + INT_MAX,
+ INT_MAX + INT_MIN,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ INT_MIN + -1,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ -1 + INT_MIN,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ INT_MIN + INT_MIN,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ INT_MAX + 1,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ 1 + INT_MAX,
+ /* expect+1: warning: operator '+' produces integer overflow [141] */
+ INT_MAX + INT_MAX,
+
+ // '-'
+
+ INT_MIN - 0,
+ INT_MIN - -1,
+ INT_MAX - 0,
+ INT_MAX - 1,
+ /* expect+1: warning: operator '-' produces integer overflow [141] */
+ INT_MIN - 1,
+ /* expect+1: warning: operator '-' produces integer overflow [141] */
+ INT_MAX - -1,
+
+ // '<<'
+
+ 256 << 23, // TODO: overflow
+ /* expect+1: warning: operator '<<' produces integer overflow [141] */
+ 256 << 24,
+
+ // '>>'
+
+ 10 >> 1,
+ -10 >> 1,
+
+ // bitwise operators
+
+ 1 & -1,
+ 1 ^ -1,
+ 1 | -1,
+};
Index: src/tests/usr.bin/xlint/lint1/msg_305.c
diff -u src/tests/usr.bin/xlint/lint1/msg_305.c:1.5 src/tests/usr.bin/xlint/lint1/msg_305.c:1.6
--- src/tests/usr.bin/xlint/lint1/msg_305.c:1.5 Sat Aug 26 10:43:53 2023
+++ src/tests/usr.bin/xlint/lint1/msg_305.c Sun Jan 7 21:19:42 2024
@@ -1,20 +1,27 @@
-/* $NetBSD: msg_305.c,v 1.5 2023/08/26 10:43:53 rillig Exp $ */
+/* $NetBSD: msg_305.c,v 1.6 2024/01/07 21:19:42 rillig Exp $ */
# 3 "msg_305.c"
/* Test for message: conversion of %s to %s requires a cast, op %s [305] */
/* lint1-flags: -sw -X 351 */
-void take_void_pointer(void *);
-
-typedef void (*function)(void);
+void *void_pointer;
+void (*void_function)(void);
+int (*int_function)(int);
void
-caller(void **void_pointer, function *function_pointer)
+example(int cond)
{
/* expect+1: warning: conversion of function pointer to 'void *' requires a cast, op = [305] */
- *void_pointer = *function_pointer;
+ void_pointer = void_function;
/* expect+1: warning: conversion of 'void *' to function pointer requires a cast, op = [305] */
- *function_pointer = *void_pointer;
+ void_function = void_pointer;
+
+ /* expect+1: warning: conversion of function pointer to 'void *' requires a cast, op = [305] */
+ void_pointer = cond ? void_function : int_function;
+ /* expect+1: warning: conversion of function pointer to 'void *' requires a cast, op : [305] */
+ void_pointer = cond ? void_pointer : int_function;
+ /* expect+1: warning: conversion of function pointer to 'void *' requires a cast, op : [305] */
+ void_pointer = cond ? void_function : void_pointer;
}