xref: /netbsd-src/tests/usr.bin/xlint/lint1/platform_ilp32_long.c (revision c6466ed0f7b5b26a3f7774bb65f229ef46abdf62)
1 /*	$NetBSD: platform_ilp32_long.c,v 1.9 2024/03/30 16:47:45 rillig Exp $	*/
2 # 3 "platform_ilp32_long.c"
3 
4 /*
5  * Test features that only apply to platforms that have 32-bit int, long and
6  * pointer types and where size_t is unsigned long, not unsigned int.
7  *
8  * On these platforms, in portable mode (-p), the type 'int' is in some cases
9  * assumed to be only 24 bits wide, in order to detect conversions from
10  * 'long' (or more probably 'size_t') to 'int', which can lose accuracy.
11  */
12 
13 /* lint1-only-if: ilp32 long */
14 /* lint1-extra-flags: -c -h -a -p -b -r -z -X 351 */
15 
16 int s32;
17 unsigned int u32;
18 long sl32;
19 unsigned long ul32;
20 
21 void
convert_between_int_and_long(void)22 convert_between_int_and_long(void)
23 {
24 	/*
25 	 * The '-p' option enables checks that apply independently of the
26 	 * current platform, assuming that 'long' is always wider than 'int'.
27 	 * This assumption, when applied on its own, leads to wrong warnings
28 	 * that a 32-bit 'long' may lose accuracy when converted to a 32-bit
29 	 * 'int'.
30 	 *
31 	 * To avoid these, take a look at the actually possible values of the
32 	 * right-hand side, and if they fit in the left-hand side, don't warn.
33 	 */
34 	s32 = sl32;
35 	sl32 = s32;
36 	u32 = ul32;
37 	ul32 = u32;
38 }
39 
40 unsigned char u8;
41 unsigned long long u64;
42 unsigned char u8_buf[20];
43 unsigned long long u64_buf[20];
44 
45 void
array_index(void)46 array_index(void)
47 {
48 
49 	/* expect+1: warning: array subscript 16777215 cannot be > 19 [168] */
50 	u8 += u8_buf[0x00ffffff];
51 	/* expect+1: warning: array subscript 2147483647 cannot be > 19 [168] */
52 	u8 += u8_buf[0x7fffffff];
53 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
54 	/* expect+1: warning: array subscript -2147483648 cannot be negative [167] */
55 	u8 += u8_buf[2147483648];
56 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
57 	/* expect+1: warning: array subscript -2147483648 cannot be negative [167] */
58 	u8 += u8_buf[0x80000000];
59 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
60 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
61 	u8 += u8_buf[0xffffffff];
62 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
63 	/* expect+1: warning: array subscript -2147483648 cannot be negative [167] */
64 	u8 += u8_buf[0x80000000];
65 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
66 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
67 	u8 += u8_buf[0xffffffff];
68 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
69 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
70 	u8 += u8_buf[0x00ffffffffffffff];
71 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
72 	u8 += u8_buf[0xffffffffffffffff];
73 
74 	/* expect+1: warning: array subscript 16777215 cannot be > 19 [168] */
75 	u64 += u64_buf[0x00ffffff];
76 	/* expect+2: warning: '2147483647 * 8' overflows 'long' [141] */
77 	/* expect+1: warning: array subscript 268435455 cannot be > 19 [168] */
78 	u64 += u64_buf[0x7fffffff];
79 	/* expect+3: warning: conversion of 'long long' to 'long' is out of range [119] */
80 	/* expect+2: warning: '-2147483648 * 8' overflows 'long' [141] */
81 	/* expect+1: warning: array subscript -268435456 cannot be negative [167] */
82 	u64 += u64_buf[2147483648];
83 	/* expect+3: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
84 	/* expect+2: warning: '-2147483648 * 8' overflows 'long' [141] */
85 	/* expect+1: warning: array subscript -268435456 cannot be negative [167] */
86 	u64 += u64_buf[0x80000000];
87 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
88 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
89 	u64 += u64_buf[0xffffffff];
90 	/* expect+3: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
91 	/* expect+2: warning: '-2147483648 * 8' overflows 'long' [141] */
92 	/* expect+1: warning: array subscript -268435456 cannot be negative [167] */
93 	u64 += u64_buf[0x80000000];
94 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
95 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
96 	u64 += u64_buf[0xffffffff];
97 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
98 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
99 	u64 += u64_buf[0x00ffffffffffffff];
100 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
101 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
102 	u64 += u64_buf[0x0fffffffffffffff];
103 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
104 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
105 	u64 += u64_buf[0x1fffffffffffffff];
106 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
107 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
108 	u64 += u64_buf[0x3fffffffffffffff];
109 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
110 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
111 	u64 += u64_buf[0x7fffffffffffffff];
112 	/* expect+1: warning: array subscript -1 cannot be negative [167] */
113 	u64 += u64_buf[0xffffffffffffffff];
114 }
115