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

@ -129,13 +129,13 @@ struct AsanExtra {
};
struct AsanSourceLocation {
const char *filename;
char *filename;
int line;
int column;
};
struct AsanAccessInfo {
const char *addr;
char *addr;
const uintptr_t first_bad_addr;
size_t size;
bool iswrite;
@ -143,7 +143,7 @@ struct AsanAccessInfo {
};
struct AsanGlobal {
const char *addr;
char *addr;
size_t size;
size_t size_with_redzone;
const void *name;
@ -517,7 +517,6 @@ struct AsanFault __asan_check(const void *p, long n) {
*/
struct AsanFault __asan_check_str(const char *p) {
uint64_t w;
struct AsanFault f;
signed char c, k, *s;
s = SHADOW(p);
if (OverlapsShadowSpace(p, 1)) {
@ -1059,7 +1058,8 @@ static struct AsanExtra *__asan_get_extra(const void *p, size_t *c) {
long x, n;
struct AsanExtra *e;
f = (intptr_t)p >> 16;
if (!kisdangerous(p) && (n = _weaken(dlmalloc_usable_size)(p)) > sizeof(*e) &&
if (!kisdangerous(p) &&
(n = _weaken(dlmalloc_usable_size)((void *)p)) > sizeof(*e) &&
!ckd_add(&x, (intptr_t)p, n) && x <= 0x800000000000 &&
(LIKELY(f == (int)((x - 1) >> 16)) || !kisdangerous((void *)(x - 1))) &&
(LIKELY(f == (int)((x = x - sizeof(*e)) >> 16)) ||
@ -1227,7 +1227,6 @@ void *__asan_memalign(size_t align, size_t size) {
}
void *__asan_calloc(size_t n, size_t m) {
char *p;
struct AsanTrace bt;
__asan_trace(&bt, RBP);
if (ckd_mul(&n, n, m)) n = -1;
@ -1498,7 +1497,7 @@ void __asan_init(int argc, char **argv, char **envp, intptr_t *auxv) {
__asan_shadow_existing_mappings();
__asan_map_shadow((uintptr_t)__executable_start, _end - __executable_start);
__asan_map_shadow(0, 4096);
__asan_poison(0, getauxval(AT_PAGESZ), kAsanNullPage);
__asan_poison((void *)__veil("r", 0L), getauxval(AT_PAGESZ), kAsanNullPage);
if (!IsWindows()) {
sys_mprotect((void *)0x7fff8000, 0x10000, PROT_READ);
}

View file

@ -29,18 +29,17 @@
* @note 30gbps on Nehalem (Intel 2008+) otherwise 3gbps
*/
size_t _countbits(const void *a, size_t n) {
int i;
size_t t;
unsigned b;
uint64_t x;
long Ai, Bi, Ci, Di;
long Ao, Bo, Co, Do;
const char *p, *e;
t = 0;
p = a;
e = p + n;
if (!IsTiny()) {
#ifdef __x86_64__
long Ai, Bi, Ci, Di;
long Ao, Bo, Co, Do;
if (X86_HAVE(POPCNT)) {
while (p + sizeof(long) * 4 <= e) {
__builtin_memcpy(&Ai, p + 000, sizeof(long));

View file

@ -28,7 +28,7 @@ int begin_cancellation_point(void) {
int state = 0;
struct CosmoTib *tib;
struct PosixThread *pt;
if (__enable_tls) {
if (__tls_enabled) {
tib = __get_tls();
if ((pt = (struct PosixThread *)tib->tib_pthread)) {
state = pt->flags & PT_INCANCEL;
@ -41,7 +41,7 @@ int begin_cancellation_point(void) {
void end_cancellation_point(int state) {
struct CosmoTib *tib;
struct PosixThread *pt;
if (__enable_tls) {
if (__tls_enabled) {
tib = __get_tls();
if ((pt = (struct PosixThread *)tib->tib_pthread)) {
pt->flags &= ~PT_INCANCEL;

View file

@ -38,13 +38,14 @@ __msabi extern typeof(Sleep) *const __imp_Sleep;
* @return handle, or -1 on failure w/ `errno` set appropriately
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
*/
textwindows int64_t CreateFile(const char16_t *lpFileName, //
uint32_t dwDesiredAccess, //
uint32_t dwShareMode, //
struct NtSecurityAttributes *opt_lpSecurity, //
int dwCreationDisposition, //
uint32_t dwFlagsAndAttributes, //
int64_t opt_hTemplateFile) {
textwindows int64_t
CreateFile(const char16_t *lpFileName, //
uint32_t dwDesiredAccess, //
uint32_t dwShareMode, //
const struct NtSecurityAttributes *opt_lpSecurity, //
int dwCreationDisposition, //
uint32_t dwFlagsAndAttributes, //
int64_t opt_hTemplateFile) {
int64_t hHandle;
uint32_t micros = 1;
TryAgain:

View file

@ -19,12 +19,12 @@
#include "libc/intrin/describeflags.internal.h"
// TODO(jart): Fork this function into ASAN and non-ASAN versions.
const char *DescribeFlags(char *p, size_t n, struct DescribeFlags *d, size_t m,
const char *prefix, unsigned x) {
const char *DescribeFlags(char *p, size_t n, const struct DescribeFlags *d,
size_t m, const char *prefix, unsigned x) {
bool t;
char b[21];
size_t i, j, k;
for (t = i = j = 0; j < m; ++j) {
for (t = false, i = j = 0; j < m; ++j) {
if (d[j].flag && d[j].flag != -1 && (x & d[j].flag) == d[j].flag) {
x &= ~d[j].flag;
if (t) {

View file

@ -9,7 +9,7 @@ struct thatispacked DescribeFlags {
const char *name;
};
const char *DescribeFlags(char *, size_t, struct DescribeFlags *, size_t,
const char *DescribeFlags(char *, size_t, const struct DescribeFlags *, size_t,
const char *, unsigned);
const char *DescribeArchPrctlCode(char[12], int);
@ -54,7 +54,7 @@ const char *DescribeRemapFlags(char[48], int);
const char *DescribeRlimitName(char[20], int);
const char *DescribeSchedPolicy(char[48], int);
const char *DescribeSeccompOperation(int);
const char *DescribeSiCode(char[17], int, int);
const char *DescribeSiCode(char[20], int, int);
const char *DescribeSleepFlags(char[16], int);
const char *DescribeSockLevel(char[12], int);
const char *DescribeSockOptname(char[32], int, int);
@ -105,7 +105,7 @@ const char *DescribeWhichPrio(char[12], int);
#define DescribeRemapFlags(x) DescribeRemapFlags(alloca(48), x)
#define DescribeRlimitName(rl) DescribeRlimitName(alloca(20), rl)
#define DescribeSchedPolicy(x) DescribeSchedPolicy(alloca(48), x)
#define DescribeSiCode(x, y) DescribeSiCode(alloca(17), x, y)
#define DescribeSiCode(x, y) DescribeSiCode(alloca(20), x, y)
#define DescribeSleepFlags(x) DescribeSleepFlags(alloca(16), x)
#define DescribeSockLevel(x) DescribeSockLevel(alloca(12), x)
#define DescribeSockOptname(x, y) DescribeSockOptname(alloca(32), x, y)

View file

@ -61,7 +61,6 @@ static const char *GetFrameName(int x) {
}
const char *(DescribeFrame)(char buf[32], int x) {
char *p;
if (IsShadowFrame(x)) {
ksnprintf(buf, 32, "%s %s %.8x", GetFrameName(x),
GetFrameName(FRAME(UNSHADOW(ADDR_32_TO_48(x)))),

View file

@ -20,8 +20,9 @@
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/str/str.h"
char *DescribeMagnum(char *b, const struct MagnumStr *m, const char *p, int x) {
char *s;
const char *DescribeMagnum(char *b, const struct MagnumStr *m, const char *p,
int x) {
const char *s;
if (x == 127) return "CLOCK_INVALID";
if ((s = GetMagnumStr(m, x))) {
stpcpy(stpcpy(b, p), s);

View file

@ -32,8 +32,7 @@
const char *(DescribePollFds)(char buf[N], ssize_t rc, struct pollfd *fds,
size_t nfds) {
char b64[64];
const char *d;
int i, j, o = 0;
int i, o = 0;
if (!fds) return "NULL";
if ((!IsAsan() && kisdangerous(fds)) ||

View file

@ -30,7 +30,7 @@ static bool IsSiUser(int si_code) {
}
}
static void NameIt(char p[17], const char *s, int si_code) {
static void NameIt(char p[20], const char *s, int si_code) {
p = stpcpy(p, s);
FormatInt32(p, si_code);
}
@ -38,7 +38,7 @@ static void NameIt(char p[17], const char *s, int si_code) {
/**
* Returns symbolic name for siginfo::si_code value.
*/
const char *(DescribeSiCode)(char b[17], int sig, int si_code) {
const char *(DescribeSiCode)(char b[20], int sig, int si_code) {
NameIt(b, "SI_", si_code);
if (si_code == SI_QUEUE) {
strcpy(b + 3, "QUEUE"); /* sent by sigqueue(2) */

View file

@ -26,8 +26,8 @@
* Describes setsockopt() optname arguments.
*/
const char *(DescribeSockOptname)(char buf[32], int l, int x) {
int i;
char *s, *p;
char *p;
const char *s;
const struct MagnumStr *ms;
p = buf;
if (x) {

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/termios.h"
#include "libc/calls/struct/termios.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
@ -28,7 +29,8 @@
#define append(...) o += ksnprintf(buf + o, N - o, __VA_ARGS__)
const char *(DescribeTermios)(char buf[N], ssize_t rc, struct termios *tio) {
const char *(DescribeTermios)(char buf[N], ssize_t rc,
const struct termios *tio) {
int o = 0;
char b128[128];

View file

@ -29,11 +29,8 @@
#define append(...) o += ksnprintf(buf + o, N - o, __VA_ARGS__)
const char *(DescribeWinsize)(char buf[N], int rc, struct winsize *ws) {
char b64[64];
const char *d;
int i, j, o = 0;
const char *(DescribeWinsize)(char buf[N], int rc, const struct winsize *ws) {
int o = 0;
if (!ws) return "NULL";
if (rc == -1) return "n/a";
if ((!IsAsan() && kisdangerous(ws)) ||

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/nexgen32e/ffs.h"
/**
* Finds lowest set bit in word.
@ -27,7 +28,7 @@ int ffs(int x) {
/**
* Finds lowest set bit in word.
*/
long ffsl(long x) {
int ffsl(long x) {
return __builtin_ffsl(x);
}

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/fd.internal.h"
#include "libc/calls/ttydefaults.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/extend.internal.h"
@ -26,12 +26,10 @@
#include "libc/intrin/nomultics.internal.h"
#include "libc/intrin/pushpop.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/thread/thread.h"

View file

@ -74,7 +74,7 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/thread/tls.h"
#include "libc/thread/tls2.h"
#include "libc/thread/tls2.internal.h"
#include "libc/vga/vga.internal.h"
#define KGETINT(x, va, t, s) \
@ -134,14 +134,13 @@ __msabi extern typeof(WriteFile) *const __imp_WriteFile;
long __klog_handle;
extern struct SymbolTable *__symtab;
privileged static inline char *kadvance(char *p, char *e, long n) {
__funline char *kadvance(char *p, char *e, long n) {
intptr_t t = (intptr_t)p;
if (ckd_add(&t, t, n)) t = (intptr_t)e;
return (char *)t;
}
privileged static char *kemitquote(char *p, char *e, signed char t,
unsigned c) {
__funline char *kemitquote(char *p, char *e, signed char t, unsigned c) {
if (t) {
if (p < e) {
*p = t < 0 ? 'u' : 'L';
@ -155,27 +154,27 @@ privileged static char *kemitquote(char *p, char *e, signed char t,
return p;
}
privileged static inline bool kiskernelpointer(const void *p) {
__funline bool kiskernelpointer(const void *p) {
return 0x7f0000000000 <= (intptr_t)p && (intptr_t)p < 0x800000000000;
}
privileged static inline bool kistextpointer(const void *p) {
__funline bool kistextpointer(const void *p) {
return __executable_start <= (const unsigned char *)p &&
(const unsigned char *)p < _etext;
}
privileged static inline bool kisimagepointer(const void *p) {
__funline bool kisimagepointer(const void *p) {
return __executable_start <= (const unsigned char *)p &&
(const unsigned char *)p < _end;
}
privileged static inline bool kischarmisaligned(const char *p, signed char t) {
__funline bool kischarmisaligned(const char *p, signed char t) {
if (t == -1) return (intptr_t)p & 1;
if (t >= 1) return !!((intptr_t)p & 3);
return false;
}
privileged static inline bool kismemtrackhosed(void) {
__funline bool kismemtrackhosed(void) {
return !((_weaken(_mmi)->i <= _weaken(_mmi)->n) &&
(_weaken(_mmi)->p == _weaken(_mmi)->s ||
_weaken(_mmi)->p == (struct MemoryInterval *)kMemtrackStart));
@ -319,7 +318,7 @@ privileged long kloghandle(void) {
hand = STDERR_FILENO;
} else if (IsWindows()) {
uint32_t e, n;
const char16_t path[512];
char16_t path[512];
e = __imp_GetLastError();
n = __imp_GetEnvironmentVariableW(u"KPRINTF_LOG", path, 512);
if (!n && __imp_GetLastError() == kNtErrorEnvvarNotFound) {
@ -389,7 +388,6 @@ privileged void klog(const char *b, size_t n) {
#ifdef __x86_64__
int e;
long h;
bool cf;
size_t i;
uint16_t dx;
uint32_t wrote;
@ -442,7 +440,7 @@ privileged void klog(const char *b, size_t n) {
privileged static size_t kformat(char *b, size_t n, const char *fmt,
va_list va) {
int si, y;
int si;
wint_t t, u;
const char *abet;
signed char type;

View file

@ -40,10 +40,8 @@ dontasan static inline const unsigned char *memchr_sse(const unsigned char *s,
unsigned char c,
size_t n) {
size_t i;
unsigned k;
unsigned m;
xmm_t v, *p;
xmm_t t = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
xmm_t v, t = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
for (; n >= 16; n -= 16, s += 16) {
v = *(const xmm_t *)s;
m = __builtin_ia32_pmovmskb128(v == t);

View file

@ -91,7 +91,7 @@ void *memmove(void *dst, const void *src, size_t n) {
size_t i;
const char *s;
uint64_t a, b;
xmm_t v, w, x, y, V, W, X, Y, wut;
xmm_t v, w, x, y, V, W, X, Y;
d = dst;
s = src;

View file

@ -40,7 +40,7 @@ dontasan static inline const unsigned char *memrchr_sse(const unsigned char *s,
unsigned char c,
size_t n) {
size_t i;
unsigned k, m;
unsigned m;
xmm_t v, t = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
for (i = n; i >= 16;) {
v = *(const xmm_t *)(s + (i -= 16));

View file

@ -210,7 +210,7 @@ dontasan void __unref_page(struct mman *mm, uint64_t *pml4t, uint64_t p) {
*/
static dontasan textreal void __invert_memory(struct mman *mm,
uint64_t *pml4t) {
uint64_t i, j, *m, p, pe;
uint64_t i;
for (i = 0; i < mm->e820n; ++i) {
uint64_t ps = mm->e820[i].addr, size = mm->e820[i].size;
/* ape/ape.S has already mapped the first 2 MiB of physical memory. */
@ -261,8 +261,8 @@ dontasan textreal void __setup_mman(struct mman *mm, uint64_t *pml4t,
*/
dontasan textreal void __map_phdrs(struct mman *mm, uint64_t *pml4t, uint64_t b,
uint64_t top) {
uint64_t i, f, v, m;
struct Elf64_Phdr *p;
uint64_t i, f, v, m, *e;
extern char ape_phdrs[] __attribute__((__weak__));
extern char ape_phdrs_end[] __attribute__((__weak__));
__setup_mman(mm, pml4t, top);

View file

@ -32,8 +32,8 @@ static bool IsNoteworthyHole(unsigned i, const struct MemoryIntervals *mm) {
}
void PrintMemoryIntervals(int fd, const struct MemoryIntervals *mm) {
char *p, mappingbuf[8], framebuf[64], sb[16];
long i, w, frames, maptally = 0;
char mappingbuf[8], framebuf[64], sb[16];
for (w = i = 0; i < mm->i; ++i) {
w = MAX(w, LengthInt64Thousands(mm->p[i].y + 1 - mm->p[i].x));
}

View file

@ -34,7 +34,6 @@
* @return 0 on success, or errno on error
*/
int pthread_key_delete(pthread_key_t k) {
uint64_t mask;
unassert(0 <= k && k < PTHREAD_KEYS_MAX);
unassert(atomic_load_explicit(_pthread_key_dtor + k, memory_order_acquire));
atomic_store_explicit(_pthread_key_dtor + k, 0, memory_order_release);

View file

@ -65,7 +65,7 @@
* @vforksafe
*/
int pthread_mutex_lock(pthread_mutex_t *mutex) {
int c, d, t;
int t;
if (__vforked) return 0;

View file

@ -35,7 +35,7 @@
* @raise ENOTRECOVERABLE if `mutex` is corrupted
*/
errno_t pthread_mutex_trylock(pthread_mutex_t *mutex) {
int c, d, t;
int t;
if (__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //

View file

@ -37,7 +37,7 @@
* @vforksafe
*/
int pthread_mutex_unlock(pthread_mutex_t *mutex) {
int c, t;
int t;
if (__vforked) return 0;

View file

@ -37,6 +37,6 @@ int pthread_setspecific(pthread_key_t k, const void *val) {
// ──Quoth POSIX.1-2017
unassert(0 <= k && k < PTHREAD_KEYS_MAX);
unassert(atomic_load_explicit(_pthread_key_dtor + k, memory_order_acquire));
__get_tls()->tib_keys[k] = val;
__get_tls()->tib_keys[k] = (void *)val;
return 0;
}

View file

@ -28,7 +28,7 @@
* @asyncsignalsafe
*/
int sigcountset(const sigset_t *set) {
int r, i, x, y;
int x, y;
switch (_NSIG) {
case 32:
x = (uint32_t)set->__bits[0];

View file

@ -20,7 +20,7 @@
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/tls.h"
#include "libc/thread/tls2.h"
#include "libc/thread/tls2.internal.h"
privileged void __stracef(const char *fmt, ...) {
va_list v;

View file

@ -35,7 +35,8 @@ typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
dontasan static inline const char *strchr_sse(const char *s, unsigned char c) {
unsigned k;
unsigned m;
xmm_t v, *p;
const xmm_t *p;
xmm_t v;
xmm_t z = {0};
xmm_t n = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
k = (uintptr_t)s & 15;

View file

@ -36,7 +36,8 @@ dontasan static inline const char *strchrnul_sse(const char *s,
unsigned char c) {
unsigned k;
unsigned m;
xmm_t v, *p;
const xmm_t *p;
xmm_t v;
xmm_t z = {0};
xmm_t n = {c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
k = (uintptr_t)s & 15;

View file

@ -32,7 +32,7 @@
dontasan int strcmp(const char *a, const char *b) {
int c;
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

@ -28,7 +28,7 @@
*/
char *strerror(int err) {
if (IsTiny()) {
return firstnonnull(_strerrno(err), "EUNKNOWN");
return (char *)firstnonnull(_strerrno(err), "EUNKNOWN");
} else {
_Alignas(1) static char buf[512];
strerror_r(err, buf, sizeof(buf));

View file

@ -42,17 +42,16 @@ dontasan dontubsan privileged
int strerror_wr(int err, uint32_t winerr, char *buf, size_t size) {
/* kprintf() weakly depends on this function */
int c, n;
bool wanting;
char16_t winmsg[256];
const char *sym, *msg;
wanting = false;
/* wanting = false; */
if (!(sym = _strerrno(err))) {
sym = "EUNKNOWN";
wanting = true;
/* wanting = true; */
}
if (!(msg = _strerdoc(err))) {
msg = "No error information";
wanting = true;
/* wanting = true; */
}
if (IsTiny()) {
if (!sym) sym = "EUNKNOWN";

View file

@ -18,7 +18,7 @@
*/
#include "libc/str/str.h"
static char g_strsignal[15];
static char g_strsignal[21];
/**
* Returns string describing signal code.

View file

@ -36,15 +36,13 @@
* @asyncsignalsafe
* @threadsafe
*/
privileged dontdiscard char *strsignal_r(int sig, char buf[15]) {
int i;
char *p;
privileged dontdiscard char *strsignal_r(int sig, char buf[21]) {
const char *s;
if (!sig) {
return "0";
}
if ((s = GetMagnumStr(kSignalNames, sig))) {
return s;
return (char *)s;
}
if (SIGRTMIN <= sig && sig <= SIGRTMAX) {
sig -= SIGRTMIN;

View file

@ -29,8 +29,9 @@
* @raise EINVAL if `s` is an empty string or has a `'='` character
*/
int unsetenv(const char *s) {
char **p, *t;
char **p;
struct Env e;
const char *t;
if (!s || !*s) return einval();
for (t = s; *t; ++t) {
if (*t == '=') return einval();

View file

@ -32,7 +32,6 @@ textwindows bool32 VirtualProtect(void *lpAddress, uint64_t dwSize,
uint32_t flNewProtect,
uint32_t *lpflOldProtect) {
bool32 bOk;
char oldbuf[64];
bOk = __imp_VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect);
if (!bOk) __winerr();
NTTRACE("VirtualProtect(%p, %'zu, %s, [%s]) → %hhhd% m", lpAddress, dwSize,

View file

@ -24,7 +24,7 @@
#include "libc/nt/runtime.h"
#include "libc/sock/internal.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/tls2.h"
#include "libc/thread/tls2.internal.h"
/**
* Return path for failed Win32 API calls.