Home | History | Annotate | Line # | Download | only in lint1
msg_259.c revision 1.24
      1  1.24  rillig /*	$NetBSD: msg_259.c,v 1.24 2024/06/08 13:50:47 rillig Exp $	*/
      2   1.1  rillig # 3 "msg_259.c"
      3   1.1  rillig 
      4  1.23  rillig // Test for message: argument %d is converted from '%s' to '%s' due to prototype [259]
      5   1.1  rillig 
      6  1.16  rillig /*
      7  1.19  rillig  * This warning detects function calls that are translated in very different
      8  1.19  rillig  * translation environments, one where prototypes are omitted, and one where
      9  1.19  rillig  * prototypes are active.  It is possible to make such code interoperable,
     10  1.19  rillig  * but that requires that each argument is converted to its proper type by
     11  1.19  rillig  * the caller of the function.
     12  1.19  rillig  *
     13  1.19  rillig  * When lint is run with the '-s' flag, it no longer warns about code with
     14  1.19  rillig  * incompatibilities between traditional C and C90, therefore this test omits
     15  1.19  rillig  * all of the options '-t', '-s', '-S' and '-Ac11'.
     16  1.19  rillig  *
     17  1.19  rillig  * See also msg_297, which is about lossy integer conversions, but that
     18  1.19  rillig  * requires the flags -a -p -P, which are not enabled in the default NetBSD
     19  1.19  rillig  * build.
     20  1.16  rillig  */
     21  1.16  rillig 
     22  1.11  rillig /* lint1-only-if: lp64 */
     23  1.22  rillig /* lint1-flags: -g -h -w -X 351 */
     24   1.2  rillig 
     25  1.14  rillig void plain_char(char);
     26  1.17  rillig void signed_char(signed char);
     27  1.17  rillig void unsigned_char(unsigned char);
     28  1.17  rillig void signed_short(signed short);
     29  1.17  rillig void unsigned_short(unsigned short);
     30  1.14  rillig void signed_int(int);
     31  1.14  rillig void unsigned_int(unsigned int);
     32  1.14  rillig void signed_long(long);
     33  1.14  rillig void unsigned_long(unsigned long);
     34  1.14  rillig void signed_long_long(long long);
     35  1.14  rillig void unsigned_long_long(unsigned long long);
     36  1.24  rillig void take_float(float);
     37   1.2  rillig 
     38   1.2  rillig void
     39  1.14  rillig change_in_type_width(char c, int i, long l)
     40   1.2  rillig {
     41  1.14  rillig 	plain_char(c);
     42  1.14  rillig 	signed_int(c);
     43   1.9  rillig 	/* No warning 259 on LP64, only on ILP32 */
     44  1.14  rillig 	signed_long(c);
     45   1.9  rillig 
     46  1.14  rillig 	plain_char(i);		/* XXX: why no warning? */
     47  1.14  rillig 	signed_int(i);
     48   1.9  rillig 	/* No warning 259 on LP64, only on ILP32 */
     49  1.14  rillig 	signed_long(i);
     50   1.9  rillig 
     51  1.14  rillig 	plain_char(l);		/* XXX: why no warning? */
     52  1.21  rillig 	/* expect+1: ... from 'long' to 'int' due to prototype [259] */
     53  1.14  rillig 	signed_int(l);
     54  1.14  rillig 	signed_long(l);
     55   1.2  rillig }
     56  1.10  rillig 
     57  1.10  rillig /*
     58  1.19  rillig  * Converting a signed integer type to its corresponding unsigned integer
     59  1.19  rillig  * type (C99 6.2.5p6) is usually not a problem since the actual values of the
     60  1.19  rillig  * expressions are usually not anywhere near the maximum signed value.  From
     61  1.19  rillig  * a technical standpoint, it is correct to warn here since even small
     62  1.19  rillig  * negative numbers may result in very large positive numbers.
     63  1.19  rillig  *
     64  1.19  rillig  * A common case where it occurs is when the difference of two pointers is
     65  1.19  rillig  * converted to size_t.  The type ptrdiff_t is defined to be signed, but in
     66  1.19  rillig  * many practical cases, the expression is '(end - start)', which makes the
     67  1.19  rillig  * resulting value necessarily positive.
     68  1.17  rillig  */
     69  1.17  rillig void
     70  1.17  rillig small_integer_types(char c, signed char sc, unsigned char uc,
     71  1.17  rillig 		    signed short ss, unsigned short us,
     72  1.17  rillig 		    signed int si, unsigned int ui,
     73  1.17  rillig 		    signed long long sll, unsigned long long ull)
     74  1.17  rillig {
     75  1.17  rillig 	plain_char(c);
     76  1.17  rillig 	plain_char(sc);
     77  1.17  rillig 	plain_char(uc);
     78  1.17  rillig 	plain_char(ss);
     79  1.17  rillig 	plain_char(us);
     80  1.17  rillig 	plain_char(si);
     81  1.17  rillig 	plain_char(ui);
     82  1.17  rillig 	plain_char(sll);
     83  1.17  rillig 	plain_char(ull);
     84  1.17  rillig 
     85  1.17  rillig 	signed_char(c);
     86  1.17  rillig 	signed_char(sc);
     87  1.17  rillig 	signed_char(uc);
     88  1.17  rillig 	signed_char(ss);
     89  1.17  rillig 	signed_char(us);
     90  1.17  rillig 	signed_char(si);
     91  1.17  rillig 	signed_char(ui);
     92  1.17  rillig 	signed_char(sll);
     93  1.17  rillig 	signed_char(ull);
     94  1.17  rillig 
     95  1.17  rillig 	unsigned_char(c);
     96  1.17  rillig 	unsigned_char(sc);
     97  1.17  rillig 	unsigned_char(uc);
     98  1.17  rillig 	unsigned_char(ss);
     99  1.17  rillig 	unsigned_char(us);
    100  1.17  rillig 	unsigned_char(si);
    101  1.17  rillig 	unsigned_char(ui);
    102  1.17  rillig 	unsigned_char(sll);
    103  1.17  rillig 	unsigned_char(ull);
    104  1.17  rillig 
    105  1.17  rillig 	signed_short(c);
    106  1.17  rillig 	signed_short(sc);
    107  1.17  rillig 	signed_short(uc);
    108  1.17  rillig 	signed_short(ss);
    109  1.17  rillig 	signed_short(us);
    110  1.17  rillig 	signed_short(si);
    111  1.17  rillig 	signed_short(ui);
    112  1.17  rillig 	signed_short(sll);
    113  1.17  rillig 	signed_short(ull);
    114  1.17  rillig 
    115  1.17  rillig 	unsigned_short(c);
    116  1.17  rillig 	unsigned_short(sc);
    117  1.17  rillig 	unsigned_short(uc);
    118  1.17  rillig 	unsigned_short(ss);
    119  1.17  rillig 	unsigned_short(us);
    120  1.17  rillig 	unsigned_short(si);
    121  1.17  rillig 	unsigned_short(ui);
    122  1.17  rillig 	unsigned_short(sll);
    123  1.17  rillig 	unsigned_short(ull);
    124  1.17  rillig }
    125  1.17  rillig 
    126  1.17  rillig /*
    127  1.19  rillig  * This function tests, among others, the conversion from a signed integer
    128  1.19  rillig  * type to its corresponding unsigned integer type.  Warning 259 is not
    129  1.19  rillig  * about lossy integer conversions but about ABI calling conventions.
    130  1.14  rillig  *
    131  1.19  rillig  * A common case where a conversion from a signed integer type to its
    132  1.19  rillig  * corresponding unsigned integer type occurs is when the difference of two
    133  1.19  rillig  * pointers is converted to size_t.  The type ptrdiff_t is defined to be
    134  1.19  rillig  * signed, but in many practical cases, the expression is '(end - start)',
    135  1.19  rillig  * which makes the resulting value necessarily positive.
    136  1.10  rillig  */
    137  1.10  rillig void
    138  1.14  rillig signed_to_unsigned(int si, long sl, long long sll)
    139  1.10  rillig {
    140  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'int' to 'unsigned int' due to prototype [259] */
    141  1.14  rillig 	unsigned_int(si);
    142  1.10  rillig 
    143  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long' to 'unsigned int' due to prototype [259] */
    144  1.14  rillig 	unsigned_int(sl);
    145  1.10  rillig 
    146  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long long' to 'unsigned int' due to prototype [259] */
    147  1.14  rillig 	unsigned_int(sll);
    148  1.12  rillig 
    149  1.12  rillig 	/*
    150  1.19  rillig 	 * No warning here.  Even though 'unsigned long' is 64 bits wide, it
    151  1.19  rillig 	 * cannot represent negative 32-bit values.  This lossy conversion is
    152  1.19  rillig 	 * covered by message 297 instead, which requires nonstandard flags.
    153  1.12  rillig 	 */
    154  1.14  rillig 	unsigned_long(si);
    155  1.14  rillig 
    156  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long' to 'unsigned long' due to prototype [259] */
    157  1.14  rillig 	unsigned_long(sl);
    158  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long long' to 'unsigned long' due to prototype [259] */
    159  1.15  rillig 	unsigned_long(sll);
    160  1.12  rillig 
    161  1.12  rillig 	/*
    162  1.19  rillig 	 * No warning here.  Even though 'unsigned long long' is 64 bits
    163  1.19  rillig 	 * wide, it cannot represent negative 32-bit values.  This lossy
    164  1.19  rillig 	 * conversion is covered by message 297 instead, which requires
    165  1.19  rillig 	 * nonstandard flags.
    166  1.12  rillig 	 */
    167  1.14  rillig 	unsigned_long_long(si);
    168  1.12  rillig 
    169  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long' to 'unsigned long long' due to prototype [259] */
    170  1.14  rillig 	unsigned_long_long(sl);
    171  1.14  rillig 
    172  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long long' to 'unsigned long long' due to prototype [259] */
    173  1.14  rillig 	unsigned_long_long(sll);
    174  1.14  rillig }
    175  1.14  rillig 
    176  1.14  rillig void
    177  1.14  rillig unsigned_to_signed(unsigned int ui, unsigned long ul, unsigned long long ull)
    178  1.14  rillig {
    179  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned int' to 'int' due to prototype [259] */
    180  1.14  rillig 	signed_int(ui);
    181  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long' to 'int' due to prototype [259] */
    182  1.14  rillig 	signed_int(ul);
    183  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long long' to 'int' due to prototype [259] */
    184  1.14  rillig 	signed_int(ull);
    185  1.14  rillig 	signed_long(ui);
    186  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long' to 'long' due to prototype [259] */
    187  1.14  rillig 	signed_long(ul);
    188  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long long' to 'long' due to prototype [259] */
    189  1.15  rillig 	signed_long(ull);
    190  1.14  rillig 	signed_long_long(ui);
    191  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long' to 'long long' due to prototype [259] */
    192  1.14  rillig 	signed_long_long(ul);
    193  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long long' to 'long long' due to prototype [259] */
    194  1.14  rillig 	signed_long_long(ull);
    195  1.14  rillig }
    196  1.14  rillig 
    197  1.14  rillig void
    198  1.14  rillig signed_to_signed(signed int si, signed long sl, signed long long sll)
    199  1.14  rillig {
    200  1.14  rillig 	signed_int(si);
    201  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long' to 'int' due to prototype [259] */
    202  1.14  rillig 	signed_int(sl);
    203  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long long' to 'int' due to prototype [259] */
    204  1.14  rillig 	signed_int(sll);
    205  1.14  rillig 	signed_long(si);
    206  1.14  rillig 	signed_long(sl);
    207  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long long' to 'long' due to prototype [259] */
    208  1.15  rillig 	signed_long(sll);
    209  1.14  rillig 	signed_long_long(si);
    210  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'long' to 'long long' due to prototype [259] */
    211  1.14  rillig 	signed_long_long(sl);
    212  1.14  rillig 	signed_long_long(sll);
    213  1.14  rillig }
    214  1.14  rillig 
    215  1.14  rillig void
    216  1.14  rillig unsigned_to_unsigned(unsigned int ui, unsigned long ul, unsigned long long ull)
    217  1.14  rillig {
    218  1.14  rillig 	unsigned_int(ui);
    219  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
    220  1.14  rillig 	unsigned_int(ul);
    221  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long long' to 'unsigned int' due to prototype [259] */
    222  1.14  rillig 	unsigned_int(ull);
    223  1.14  rillig 	unsigned_long(ui);
    224  1.14  rillig 	unsigned_long(ul);
    225  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long long' to 'unsigned long' due to prototype [259] */
    226  1.15  rillig 	unsigned_long(ull);
    227  1.14  rillig 	unsigned_long_long(ui);
    228  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long' to 'unsigned long long' due to prototype [259] */
    229  1.14  rillig 	unsigned_long_long(ul);
    230  1.14  rillig 	unsigned_long_long(ull);
    231  1.10  rillig }
    232  1.13  rillig 
    233  1.13  rillig void
    234  1.24  rillig constants(void)
    235  1.24  rillig {
    236  1.24  rillig 	/* expect+2: warning: argument 1 is converted from 'long long' to 'unsigned int' due to prototype [259] */
    237  1.24  rillig 	/* expect+1: warning: conversion of 'long long' to 'unsigned int' is out of range, arg #1 [295] */
    238  1.24  rillig 	unsigned_int(0x7fffffffffffffffLL);
    239  1.24  rillig 	/* expect+1: warning: argument 1 is converted from 'double' to 'unsigned int' due to prototype [259] */
    240  1.24  rillig 	unsigned_int(2.1);
    241  1.24  rillig }
    242  1.24  rillig 
    243  1.24  rillig void
    244  1.24  rillig to_float(double dbl)
    245  1.24  rillig {
    246  1.24  rillig 	/* expect+1: warning: argument 1 is converted from 'double' to 'float' due to prototype [259] */
    247  1.24  rillig 	take_float(dbl);
    248  1.24  rillig }
    249  1.24  rillig 
    250  1.24  rillig void
    251  1.13  rillig pass_sizeof_as_smaller_type(void)
    252  1.13  rillig {
    253  1.13  rillig 	/*
    254  1.19  rillig 	 * Even though the expression has type size_t, it has a constant
    255  1.13  rillig 	 * value that fits effortless into an 'unsigned int', it's so small
    256  1.19  rillig 	 * that it would even fit into a 3-bit bit-field, so lint's warning
    257  1.19  rillig 	 * may seem wrong here.
    258  1.19  rillig 	 *
    259  1.19  rillig 	 * This warning 259 is not about lossy integer conversion though but
    260  1.19  rillig 	 * instead covers calling conventions that may differ between integer
    261  1.19  rillig 	 * types of different sizes, and from that point of view, the
    262  1.19  rillig 	 * constant, even though its value would fit in an unsigned int, is
    263  1.19  rillig 	 * still passed as size_t.
    264  1.13  rillig 	 */
    265  1.23  rillig 	/* expect+1: warning: argument 1 is converted from 'unsigned long' to 'unsigned int' due to prototype [259] */
    266  1.14  rillig 	unsigned_int(sizeof(int));
    267  1.13  rillig }
    268