Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/coreutils-cksum-pr108664.c

raw
1 /* { dg-require-effective-target int32plus } */
2 /* { dg-require-effective-target size24plus } */
3
4 /* Reduced from coreutils's cksum.c: cksum_slice8 */
5
6 typedef long unsigned int size_t;
7 typedef unsigned int __uint32_t;
8 typedef unsigned long int __uintmax_t;
9 typedef struct _IO_FILE FILE;
10
11 extern size_t
12 fread_unlocked(void* __restrict __ptr,
13 size_t __size,
14 size_t __n,
15 FILE* __restrict __stream);
16 extern int
17 feof_unlocked(FILE* __stream) __attribute__((__nothrow__, __leaf__));
18 extern int
19 ferror_unlocked(FILE* __stream) __attribute__((__nothrow__, __leaf__));
20 static __inline __uint32_t
21 __bswap_32(__uint32_t __bsx)
22 {
23
24 return __builtin_bswap32(__bsx);
25 }
26 typedef __uint32_t uint32_t;
27 typedef unsigned long int uint_fast32_t;
28 typedef __uintmax_t uintmax_t;
29 extern int*
30 __errno_location(void) __attribute__((__nothrow__, __leaf__))
31 __attribute__((__const__));
32 extern uint_fast32_t const crctab[8][256];
33
34 static bool
35 cksum_slice8(FILE* fp, uint_fast32_t* crc_out, uintmax_t* length_out)
36 {
37 uint32_t buf[(1 << 16) / sizeof(uint32_t)];
38 uint_fast32_t crc = 0;
39 uintmax_t length = 0;
40 size_t bytes_read;
41
42 if (!fp || !crc_out || !length_out)
43 return 0;
44
45 while ((bytes_read = fread_unlocked(buf, 1, (1 << 16), fp)) > 0) {
46 uint32_t* datap;
47
48 if (length + bytes_read < length) {
49
50 (*__errno_location()) = 75;
51 return 0;
52 }
53 length += bytes_read;
54
55 if (bytes_read == 0) {
56 if (ferror_unlocked(fp))
57 return 0;
58 }
59
60 datap = (uint32_t*)buf;
61 while (bytes_read >= 8) {
62 uint32_t first = *datap++, second = *datap++; /* { dg-bogus "use of uninitialized value" "PR analyzer/108664" } */
63 crc ^= __bswap_32(first);
64 second = __bswap_32(second);
65 crc =
66 (crctab[7][(crc >> 24) & 0xFF] ^ crctab[6][(crc >> 16) & 0xFF] ^
67 crctab[5][(crc >> 8) & 0xFF] ^ crctab[4][(crc)&0xFF] ^
68 crctab[3][(second >> 24) & 0xFF] ^ crctab[2][(second >> 16) & 0xFF] ^
69 crctab[1][(second >> 8) & 0xFF] ^ crctab[0][(second)&0xFF]);
70 bytes_read -= 8;
71 }
72
73 unsigned char* cp = (unsigned char*)datap;
74 while (bytes_read--)
75 crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ *cp++) & 0xFF];
76 if (feof_unlocked(fp))
77 break;
78 }
79
80 *crc_out = crc;
81 *length_out = length;
82
83 return 1;
84 }