Fix warnings

This change fixes Cosmopolitan so it has fewer opinions about compiler
warnings. The whole repository had to be cleaned up to be buildable in
-Werror -Wall mode. This lets us benefit from things like strict const
checking. Some actual bugs might have been caught too.
This commit is contained in:
Justine Tunney 2023-09-01 20:49:13 -07:00
parent e2b3c3618e
commit 0d748ad58e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
571 changed files with 1306 additions and 1888 deletions

View file

@ -33,8 +33,8 @@ static int bcmp_sse(const char *p, const char *q, size_t n) {
q += 16;
n -= 16;
}
a = *(const xmm_t *)p ^ *(const xmm_t *)q |
*(const xmm_t *)(p + n - 16) ^ *(const xmm_t *)(q + n - 16);
a = (*(const xmm_t *)p ^ *(const xmm_t *)q) |
(*(const xmm_t *)(p + n - 16) ^ *(const xmm_t *)(q + n - 16));
return !!(a[0] | a[1]);
}
#endif
@ -65,8 +65,8 @@ _Microarchitecture("avx") static int bcmp_avx(const char *p, const char *q,
n -= 16;
}
}
a = *(const xmm_t *)p ^ *(const xmm_t *)q |
*(const xmm_t *)(p + n - 16) ^ *(const xmm_t *)(q + n - 16);
a = (*(const xmm_t *)p ^ *(const xmm_t *)q) |
(*(const xmm_t *)(p + n - 16) ^ *(const xmm_t *)(q + n - 16));
return !!(a[0] | a[1]);
}
#endif
@ -103,7 +103,6 @@ _Microarchitecture("avx") static int bcmp_avx(const char *p, const char *q,
*/
int bcmp(const void *a, const void *b, size_t n) {
int c;
unsigned u;
uint32_t i, j;
uint64_t x, y;
const char *p, *q;

View file

@ -46,7 +46,7 @@ size_t c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps) {
return 1;
}
if (!*x && c16 - 0xd800u < 0x400) {
*x = c16 - 0xd7c0 << 10;
*x = (c16 - 0xd7c0) << 10;
return 0;
}
if (*x) {

View file

@ -27,7 +27,7 @@
* @see PKZIP, FAT
*/
int64_t DosDateTimeToUnix(unsigned date, unsigned time) {
unsigned weekday, year, month, day, hour, minute, second, yday, leap;
unsigned year, month, day, hour, minute, second, yday, leap;
year = ((date & 0xfffffe00) >> 9) + 1980 - 1900;
month = MAX(1, MIN(12, (date & 0x01e0) >> 5));
day = (date & 0x001f) ? (date & 0x001f) - 1 : 0;

View file

@ -21,7 +21,7 @@
/**
* Returns comment of zip central directory.
*/
void *GetZipCdirComment(const uint8_t *eocd) {
const void *GetZipCdirComment(const uint8_t *eocd) {
if (READ32LE(eocd) == kZipCdir64HdrMagic && ZIP_CDIR64_COMMENTSIZE(eocd)) {
return ZIP_CDIR64_COMMENT(eocd);
} else {

View file

@ -38,7 +38,6 @@ typedef long long v2di __attribute__((__vector_size__(16), __aligned__(1)));
* @return pointer to EOCD64 or EOCD, otherwise null
*/
void *GetZipEocd(const void *f, size_t n, int *e) {
v2di x;
int err;
size_t i, j;
uint32_t magic;
@ -53,7 +52,7 @@ void *GetZipEocd(const void *f, size_t n, int *e) {
READ16LE("PK"), READ16LE("PK"), READ16LE("PK"), READ16LE("PK")};
asm("" : "+x"(pk));
if (i >= 14) {
x = *(const v2di *)(p + i - 14);
v2di x = *(const v2di *)(p + i - 14);
if (!(__builtin_ia32_pmovmskb128(
(v16qi)__builtin_ia32_pcmpeqw128((v8hi)x, pk)) |
__builtin_ia32_pmovmskb128((v16qi)__builtin_ia32_pcmpeqw128(
@ -70,7 +69,7 @@ void *GetZipEocd(const void *f, size_t n, int *e) {
}
if (magic == kZipCdir64LocatorMagic && i + kZipCdir64LocatorSize <= n &&
(err = IsZipEocd64(p, n, ZIP_LOCATE64_OFFSET(p + i))) == kZipOk) {
return p + ZIP_LOCATE64_OFFSET(p + i);
return (void *)(p + ZIP_LOCATE64_OFFSET(p + i));
} else if (magic == kZipCdirHdrMagic &&
(err = IsZipEocd32(p, n, i)) == kZipOk) {
j = i;
@ -78,10 +77,10 @@ void *GetZipEocd(const void *f, size_t n, int *e) {
if (READ32LE(p + j) == kZipCdir64LocatorMagic &&
j + kZipCdir64LocatorSize <= n &&
IsZipEocd64(p, n, ZIP_LOCATE64_OFFSET(p + j)) == kZipOk) {
return p + ZIP_LOCATE64_OFFSET(p + j);
return (void *)(p + ZIP_LOCATE64_OFFSET(p + j));
}
} while (j-- && i - j < 128);
return p + i;
return (void *)(p + i);
}
} while (i > 0 && i-- + 0x10000 + 0x1000 >= n);
if (e) *e = err;

View file

@ -51,7 +51,6 @@ static const char kUtf8Dispatch[] = {
*/
dontasan bool isutf8(const void *data, size_t size) {
long c;
unsigned m;
const char *p, *e;
if (size == -1) size = data ? strlen(data) : 0;
if (IsAsan()) __asan_verify(data, size);
@ -62,6 +61,7 @@ dontasan bool isutf8(const void *data, size_t size) {
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
if (!((intptr_t)p & 15)) {
for (;;) {
unsigned m;
if ((m = __builtin_ia32_pmovmskb128(*(xmm_t *)p >= (xmm_t){0}) ^
0xffff)) {
m = __builtin_ctzll(m);

View file

@ -22,7 +22,6 @@
* Returns nonzero if c is lowercase letter.
*/
int iswlower(wint_t c) {
int r;
if (c < 0200) {
return 'a' <= c && c <= 'z';
} else {

View file

@ -22,7 +22,6 @@
* Returns nonzero if c is uppercase letter.
*/
int iswupper(wint_t c) {
int r;
if (c < 0200) {
return 'A' <= c && c <= 'Z';
} else {

View file

@ -32,7 +32,8 @@
* @see rldecode() for a 16-byte decompressor
*/
textstartup void *lz4cpy(void *dest, const void *blockdata, size_t blocksize) {
unsigned char *op, *ip, *ipe, *match;
unsigned char *op, *match;
const unsigned char *ip, *ipe;
unsigned token, length, fifteen, offset, matchlen;
for (op = dest, ip = blockdata, ipe = ip + blocksize;;) {
token = *ip++;

View file

@ -28,8 +28,8 @@
* @see lz4cpy()
*/
size_t lz4len(const void *blockdata, size_t blocksize) {
unsigned char *ip, *ipe;
unsigned token, length, fifteen, offset, matchlen;
const unsigned char *ip, *ipe;
unsigned token, length, fifteen, matchlen;
size_t unpacklen = 0;
for (ip = blockdata, ipe = ip + blocksize;;) {
token = *ip++;
@ -43,7 +43,6 @@ size_t lz4len(const void *blockdata, size_t blocksize) {
ip += length;
unpacklen += length;
if (ip >= ipe) break;
offset = READ16LE(ip);
matchlen = token & fifteen;
ip += 2;
if (matchlen == fifteen) {

View file

@ -64,7 +64,7 @@ size_t mbrtowc(wchar_t *wc, const char *src, size_t n, mbstate_t *st) {
if (n) {
if (OOB(c, *s)) goto ilseq;
loop:
c = c << 6 | *s++ - 0x80;
c = c << 6 | (*s++ - 0x80);
n--;
if (!(c & (1U << 31))) {
*(unsigned *)st = 0;

View file

@ -114,19 +114,19 @@ size_t mbsrtowcs(wchar_t *ws, const char **src, size_t wn, mbstate_t *st) {
s--;
break;
}
c = (c << 6) | *s++ - 0x80;
c = (c << 6) | (*s++ - 0x80);
if (c & (1U << 31)) {
if (*s - 0x80u >= 0x40) {
s -= 2;
break;
}
c = (c << 6) | *s++ - 0x80;
c = (c << 6) | (*s++ - 0x80);
if (c & (1U << 31)) {
if (*s - 0x80u >= 0x40) {
s -= 3;
break;
}
c = (c << 6) | *s++ - 0x80;
c = (c << 6) | (*s++ - 0x80);
}
}
*ws++ = c;

View file

@ -51,19 +51,19 @@ int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n) {
* insufficient to read a character */
if (n < 4 && ((c << (6 * n - 6)) & (1U << 31))) goto ilseq;
if (OOB(c, *s)) goto ilseq;
c = c << 6 | *s++ - 0x80;
c = c << 6 | (*s++ - 0x80);
if (!(c & (1U << 31))) {
*wc = c;
return 2;
}
if (*s - 0x80u >= 0x40) goto ilseq;
c = c << 6 | *s++ - 0x80;
c = c << 6 | (*s++ - 0x80);
if (!(c & (1U << 31))) {
*wc = c;
return 3;
}
if (*s - 0x80u >= 0x40) goto ilseq;
*wc = c << 6 | *s++ - 0x80;
*wc = c << 6 | (*s++ - 0x80);
return 4;
ilseq:
errno = EILSEQ;

View file

@ -36,12 +36,13 @@ dontasan void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen) {
#if defined(__x86_64__) && !defined(__chibicc__)
char c;
xmm_t n, *v;
xmm_t n;
const xmm_t *v;
unsigned i, k, m;
const char *p, *q, *e;
if (IsAsan()) __asan_verify(needle, needlelen);
if (IsAsan()) __asan_verify(haystack, haystacklen);
if (!needlelen) return haystack;
if (!needlelen) return (void *)haystack;
if (UNLIKELY(needlelen > haystacklen)) return 0;
q = needle;
c = *q;
@ -72,7 +73,7 @@ dontasan void *memmem(const void *haystack, size_t haystacklen,
}
#else
size_t i, j;
if (!needlelen) return haystack;
if (!needlelen) return (void *)haystack;
if (needlelen > haystacklen) return 0;
for (i = 0; i < haystacklen; ++i) {
for (j = 0;; ++j) {

View file

@ -38,7 +38,7 @@ static inline const char16_t *memrchr16_pure(const char16_t *s, char16_t c,
dontasan static inline const char16_t *memrchr16_sse(const char16_t *s,
char16_t c, size_t n) {
size_t i;
unsigned k, m;
unsigned m;
xmm_t v, t = {c, c, c, c, c, c, c, c};
for (i = n; i >= 8;) {
v = *(const xmm_t *)(s + (i -= 8));
@ -77,6 +77,6 @@ void *memrchr16(const void *s, int c, size_t n) {
}
return (void *)r;
#else
return memrchr16_pure(s, c, n);
return (void *)memrchr16_pure(s, c, n);
#endif
}

View file

@ -37,8 +37,8 @@ dontasan static inline const char *rawmemchr_sse(const char *s,
unsigned char c) {
unsigned k;
unsigned m;
xmm_t v, *p;
xmm_t n = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
const xmm_t *p;
xmm_t v, n = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
k = (uintptr_t)s & 15;
p = (const xmm_t *)((uintptr_t)s & -16);
v = *p;
@ -54,7 +54,7 @@ dontasan static inline const char *rawmemchr_sse(const char *s,
}
#endif
static inline dontasan uint64_t UncheckedAlignedRead64(unsigned char *p) {
static inline dontasan uint64_t UncheckedAlignedRead64(const unsigned char *p) {
return (uint64_t)p[7] << 070 | (uint64_t)p[6] << 060 | (uint64_t)p[5] << 050 |
(uint64_t)p[4] << 040 | (uint64_t)p[3] << 030 | (uint64_t)p[2] << 020 |
(uint64_t)p[1] << 010 | (uint64_t)p[0] << 000;
@ -84,7 +84,7 @@ void *rawmemchr(const void *s, int c) {
c &= 255;
v = 0x0101010101010101ul * c;
for (; (uintptr_t)p & 7; ++p) {
if (*p == c) return p;
if (*p == c) return (void *)p;
}
for (;; p += 8) {
w = UncheckedAlignedRead64(p);
@ -94,6 +94,6 @@ void *rawmemchr(const void *s, int c) {
}
}
assert(*p == c);
return p;
return (void *)p;
#endif
}

View file

@ -24,9 +24,7 @@
/**
* Sets program locale.
*
* Cosmopolitan only supports the C or POSIX locale.
*
* "You can have any locale you want as long as it's C." -- Henry Ford
* Cosmopolitan only supports the C or POSIX locale with UTF-8.
*/
char *setlocale(int category, const char *locale) {
char *res;
@ -36,7 +34,7 @@ char *setlocale(int category, const char *locale) {
!strcmp(locale, "POSIX") || //
!strcmp(locale, "C.UTF-8") || //
!strcmp(locale, "en_US.UTF-8")) {
res = locale;
res = (char *)locale;
} else {
res = NULL;
}

View file

@ -23,38 +23,38 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int isascii(int) pureconst;
int isspace(int) pureconst;
int isalpha(int) pureconst;
int isdigit(int) pureconst;
int isalnum(int) pureconst;
int isxdigit(int) pureconst;
int isprint(int) pureconst;
int islower(int) pureconst;
int isupper(int) pureconst;
int isblank(int) pureconst;
int iscntrl(int) pureconst;
int isgraph(int) pureconst;
int tolower(int) pureconst;
int ispunct(int) pureconst;
int toupper(int) pureconst;
int toascii(int) pureconst;
int isascii(int);
int isspace(int);
int isalpha(int);
int isdigit(int);
int isalnum(int);
int isxdigit(int);
int isprint(int);
int islower(int);
int isupper(int);
int isblank(int);
int iscntrl(int);
int isgraph(int);
int tolower(int);
int ispunct(int);
int toupper(int);
int toascii(int);
int iswalnum(wint_t) pureconst;
int iswalpha(wint_t) pureconst;
int iswblank(wint_t) pureconst;
int iswcntrl(wint_t) pureconst;
int iswdigit(wint_t) pureconst;
int iswgraph(wint_t) pureconst;
int iswlower(wint_t) pureconst;
int iswspace(wint_t) pureconst;
int iswupper(wint_t) pureconst;
int iswxdigit(wint_t) pureconst;
int iswpunct(wint_t) pureconst;
int iswprint(wint_t) pureconst;
int iswseparator(wint_t) pureconst;
wint_t towlower(wint_t) pureconst;
wint_t towupper(wint_t) pureconst;
int iswalnum(wint_t);
int iswalpha(wint_t);
int iswblank(wint_t);
int iswcntrl(wint_t);
int iswdigit(wint_t);
int iswgraph(wint_t);
int iswlower(wint_t);
int iswspace(wint_t);
int iswupper(wint_t);
int iswxdigit(wint_t);
int iswpunct(wint_t);
int iswprint(wint_t);
int iswseparator(wint_t);
wint_t towlower(wint_t);
wint_t towupper(wint_t);
void bzero(void *, size_t) memcpyesque;
void *memset(void *, int, size_t) memcpyesque;
@ -191,7 +191,7 @@ bool startswithi(const char *, const char *) strlenesque;
bool endswith(const char *, const char *) strlenesque;
bool istext(const void *, size_t) libcesque;
bool isutf8(const void *, size_t) libcesque;
char *strsignal_r(int, char[15]) returnsnonnull libcesque dontdiscard;
char *strsignal_r(int, char[21]) returnsnonnull libcesque dontdiscard;
int strerror_wr(int, uint32_t, char *, size_t)
dontthrow nocallback;
char16_t *chomp16(char16_t *) libcesque;

View file

@ -32,7 +32,7 @@
dontasan int strcasecmp(const char *a, const char *b) {
int x, y;
size_t i = 0;
uint64_t v, w, d;
uint64_t v, w;
if (a == b) return 0;
if (IsAsan()) __asan_verify_str(a);
if (IsAsan()) __asan_verify_str(b);

View file

@ -38,13 +38,13 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
dontasan char *strcasestr(const char *haystack, const char *needle) {
#if defined(__x86_64__) && !defined(__chibicc__)
char c;
xmm_t *p;
size_t i;
unsigned k, m;
const xmm_t *p;
xmm_t v, n1, n2, z = {0};
if (IsAsan()) __asan_verify(needle, 1);
if (IsAsan()) __asan_verify(haystack, 1);
if (haystack == needle || !*needle) return haystack;
if (haystack == needle || !*needle) return (char *)haystack;
c = *needle;
n1 = (xmm_t){c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
c = kToLower[c & 255];
@ -71,8 +71,7 @@ dontasan char *strcasestr(const char *haystack, const char *needle) {
return 0;
#else
size_t i;
unsigned k, m;
if (haystack == needle || !*needle) return haystack;
if (haystack == needle || !*needle) return (void *)haystack;
for (;;) {
for (i = 0;; ++i) {
if (!needle[i]) return (/*unconst*/ char *)haystack;

View file

@ -37,13 +37,13 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
*/
dontasan char *strstr(const char *haystack, const char *needle) {
#if defined(__x86_64__) && !defined(__chibicc__)
xmm_t *p;
size_t i;
unsigned k, m;
const xmm_t *p;
xmm_t v, n, z = {0};
if (IsAsan()) __asan_verify(needle, 1);
if (IsAsan()) __asan_verify(haystack, 1);
if (haystack == needle || !*needle) return haystack;
if (haystack == needle || !*needle) return (char *)haystack;
n = (xmm_t){*needle, *needle, *needle, *needle, *needle, *needle,
*needle, *needle, *needle, *needle, *needle, *needle,
*needle, *needle, *needle, *needle};
@ -69,8 +69,7 @@ dontasan char *strstr(const char *haystack, const char *needle) {
return 0;
#else
size_t i;
unsigned k, m;
if (haystack == needle || !*needle) return haystack;
if (haystack == needle || !*needle) return (void *)haystack;
for (;;) {
for (i = 0;; ++i) {
if (!needle[i]) return (/*unconst*/ char *)haystack;

View file

@ -6,6 +6,6 @@
#define ThomPikeByte(x) ((x) & (((1 << ThomPikeMsb(x)) - 1) | 3))
#define ThomPikeLen(x) (7 - ThomPikeMsb(x))
#define ThomPikeMsb(x) ((255 & (x)) < 252 ? _bsr(255 & ~(x)) : 1)
#define ThomPikeMerge(x, y) ((x) << 6 | 077 & (y))
#define ThomPikeMerge(x, y) ((x) << 6 | (077 & (y)))
#endif /* COSMOPOLITAN_LIBC_STR_THOMPIKE_H_ */

View file

@ -58,7 +58,7 @@ static inline dontasan axdx_t tprecode8to16_sse2(char16_t *dst, size_t dstsize,
axdx_t tprecode8to16(char16_t *dst, size_t dstsize, const char *src) {
axdx_t r;
unsigned w;
int x, y, a, b, i, n;
int x, a, b, i, n;
r.ax = 0;
r.dx = 0;
for (;;) {

View file

@ -23,7 +23,7 @@
*/
wchar_t *wcschr(const wchar_t *s, wchar_t c) {
for (;; ++s) {
if (*s == c) return s;
if (*s == c) return (wchar_t *)s;
if (!*s) return 0;
}
}

View file

@ -20,7 +20,7 @@
wchar_t *wcschrnul(const wchar_t *s, wchar_t c) {
for (;; ++s) {
if (*s == c) return s;
if (!*s) return s;
if (*s == c) return (wchar_t *)s;
if (!*s) return (wchar_t *)s;
}
}

View file

@ -39,7 +39,7 @@ static inline const wchar_t *wmemrchr_pure(const wchar_t *s, wchar_t c,
dontasan static inline const wchar_t *wmemrchr_sse(const wchar_t *s, wchar_t c,
size_t n) {
size_t i;
unsigned k, m;
unsigned m;
xmm_t v, t = {c, c, c, c};
for (i = n; i >= 4;) {
v = *(const xmm_t *)(s + (i -= 4));
@ -78,6 +78,6 @@ void *wmemrchr(const wchar_t *s, wchar_t c, size_t n) {
r = wmemrchr_sse(s, c, n);
return (void *)r;
#else
return wmemrchr_pure(s, c, n);
return (void *)wmemrchr_pure(s, c, n);
#endif
}