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

@ -52,7 +52,7 @@
#include "libc/thread/openbsd.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#include "libc/thread/tls2.h"
#include "libc/thread/tls2.internal.h"
#include "libc/thread/xnu.internal.h"
#define kMaxThreadIds 32768
@ -215,8 +215,6 @@ XnuThreadMain(void *pthread, // rdi
static errno_t CloneXnu(int (*fn)(void *), char *stk, size_t stksz, int flags,
void *arg, void *tls, int *ptid, int *ctid) {
int rc;
bool failed;
static bool once;
struct CloneArgs *wt;
if (!once) {

View file

@ -95,7 +95,6 @@ static wontreturn void UnsupportedSyntax(unsigned char c) {
}
static void Open(const char *path, int fd, int flags) {
const char *err;
close(fd);
if (open(path, flags, 0644) == -1) {
perror(path);
@ -118,7 +117,7 @@ static int GetSignalByName(const char *s) {
return 0;
}
static void PutEnv(char **p, const char *kv) {
static void PutEnv(char **p, char *kv) {
struct Env e;
e = __getenv(p, kv);
p[e.i] = kv;
@ -182,7 +181,6 @@ static int Waiter(int pid) {
}
static int Wait(void) {
char ibuf[12];
int e, ws, pid;
if (n > 1) {
if ((pid = atoi(args[1])) <= 0) {
@ -719,7 +717,7 @@ static void Pipe(void) {
}
static int ShellSpawn(void) {
int rc, ws, pid;
int rc, pid;
if ((rc = TryBuiltin()) == -1) {
switch ((pid = fork())) {
case 0:
@ -804,8 +802,7 @@ static bool CopyVar(void) {
}
static char *Tokenize(void) {
const char *s;
int c, t, j, k, rc;
int c, t, rc;
for (t = STATE_WHITESPACE;; ++p) {
switch (t) {
@ -954,8 +951,8 @@ static const char *GetRedirectArg(const char *prog, const char *arg, int n) {
}
int _cocmd(int argc, char **argv, char **envp) {
size_t i;
char *arg;
size_t i, j;
size_t globCount = 0;
int globFlags = 0;
glob_t globTheBuilder;

View file

@ -101,7 +101,7 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1) {
__oldstack = (intptr_t)sp;
// detect apple m1 environment
char *magnums;
const char *magnums;
if (SupportsXnu() && (__syslib = m1)) {
hostos = _HOSTXNU;
magnums = syscon_xnu;
@ -112,15 +112,6 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1) {
notpossible;
}
// get page size
unsigned long pagesz = 4096;
for (int i = 0; auxv[i]; i += 2) {
if (auxv[i] == AT_PAGESZ) {
pagesz = auxv[i + 1];
break;
}
}
// setup system magic numbers
for (long *mp = syscon_start; mp < syscon_end; ++mp) {
magnums = DecodeMagnum(magnums, mp);

View file

@ -41,8 +41,8 @@ struct EfiArgs {
char ArgBlock[0xC00];
};
static const EFI_GUID kEfiLoadedImageProtocol = LOADED_IMAGE_PROTOCOL;
static const EFI_GUID kEfiGraphicsOutputProtocol = GRAPHICS_OUTPUT_PROTOCOL;
static EFI_GUID kEfiLoadedImageProtocol = LOADED_IMAGE_PROTOCOL;
static EFI_GUID kEfiGraphicsOutputProtocol = GRAPHICS_OUTPUT_PROTOCOL;
extern const char vga_console[];
extern void _EfiPostboot(struct mman *, uint64_t *, uintptr_t, char **);
@ -143,7 +143,6 @@ static void EfiInitVga(struct mman *mm, EFI_SYSTEM_TABLE *SystemTable) {
*/
__msabi dontasan EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
EFI_SYSTEM_TABLE *SystemTable) {
int type, x87cw = 0x037f;
struct mman *mm;
uint32_t DescVersion;
uintptr_t i, j, MapSize;
@ -152,7 +151,7 @@ __msabi dontasan EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
EFI_MEMORY_DESCRIPTOR *Map, *Desc;
uint64_t Address;
uintptr_t Args, MapKey, DescSize;
uint64_t p, pe, cr4, *m, *pd, *sp, *pml4t, *pdt1, *pdt2, *pdpt1, *pdpt2;
uint64_t *pd, *pml4t, *pdt1, *pdt2, *pdpt1, *pdpt2;
extern char os asm("__hostos");
os = _HOSTMETAL;

View file

@ -97,7 +97,7 @@ _Alignas(TLS_ALIGNMENT) static char __static_tls[6016];
*/
textstartup void __enable_tls(void) {
int tid;
size_t hiz, siz;
size_t siz;
char *mem, *tls;
struct CosmoTib *tib;
@ -150,7 +150,7 @@ textstartup void __enable_tls(void) {
#elif defined(__aarch64__)
hiz = ROUNDUP(sizeof(*tib) + 2 * sizeof(void *), I(_tls_align));
size_t hiz = ROUNDUP(sizeof(*tib) + 2 * sizeof(void *), I(_tls_align));
siz = hiz + I(_tls_size);
if (siz <= sizeof(__static_tls)) {
mem = __static_tls;

View file

@ -192,13 +192,11 @@ static textwindows int OnForkCrash(struct NtExceptionPointers *ep) {
}
textwindows void WinMainForked(void) {
bool ok;
jmp_buf jb;
int64_t reader;
int64_t savetsc;
char *addr, *shad;
struct DirectMap dm;
uint64_t size, upsize;
int64_t oncrash, savetsc;
struct MemoryInterval *maps;
char16_t fvar[21 + 1 + 21 + 1];
uint32_t i, varlen, oldprot, savepid;
@ -215,7 +213,7 @@ textwindows void WinMainForked(void) {
NTTRACE("WinMainForked()");
SetEnvironmentVariable(u"_FORK", NULL);
#ifdef SYSDEBUG
oncrash = AddVectoredExceptionHandler(1, NT2SYSV(OnForkCrash));
int64_t oncrash = AddVectoredExceptionHandler(1, NT2SYSV(OnForkCrash));
#endif
ParseInt(fvar, &reader);
@ -321,14 +319,13 @@ textwindows void WinMainForked(void) {
textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
jmp_buf jb;
uint32_t op;
uint32_t oldprot;
char **args;
char ok, threaded;
char **args, **args2;
struct CosmoTib *tib;
char16_t pipename[64];
int64_t reader, writer;
struct NtStartupInfo startinfo;
int i, n, pid, untrackpid, rc = -1;
int i, pid, untrackpid, rc = -1;
char *p, forkvar[6 + 21 + 1 + 21 + 1];
struct NtProcessInformation procinfo;
threaded = __threaded;
@ -353,6 +350,8 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
// If --strace was passed to this program, then propagate it the
// forked process since the flag was removed by __intercept_flag
if (strace_enabled(0) > 0) {
int n;
char **args2;
for (n = 0; args[n];) ++n;
args2 = alloca((n + 2) * sizeof(char *));
for (i = 0; i < n; ++i) args2[i] = args[i];

View file

@ -37,6 +37,7 @@ int _fork(uint32_t dwCreationFlags) {
struct CosmoTib *tib;
struct PosixThread *pt;
int ax, dx, tid, parent;
(void)parent;
BLOCK_SIGNALS;
if (__threaded && _weaken(_pthread_onfork_prepare)) {
_weaken(_pthread_onfork_prepare)();

View file

@ -28,7 +28,7 @@
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/thread/tls.h"
#include "libc/thread/tls2.h"
#include "libc/thread/tls2.internal.h"
/**
* @fileoverview Plain-text function call logging.
@ -48,7 +48,7 @@
static struct CosmoFtrace g_ftrace;
static privileged inline int GetNestingLevelImpl(struct StackFrame *frame) {
__funline int GetNestingLevelImpl(struct StackFrame *frame) {
int nesting = -2;
while (frame) {
++nesting;
@ -57,8 +57,7 @@ static privileged inline int GetNestingLevelImpl(struct StackFrame *frame) {
return MAX(0, nesting);
}
static privileged inline int GetNestingLevel(struct CosmoFtrace *ft,
struct StackFrame *sf) {
__funline int GetNestingLevel(struct CosmoFtrace *ft, struct StackFrame *sf) {
int nesting;
nesting = GetNestingLevelImpl(sf);
if (nesting < ft->ft_skew) ft->ft_skew = nesting;

View file

@ -30,7 +30,7 @@ forceinline int IsAlpha(int c) {
forceinline char *MemChr(const char *s, unsigned char c, unsigned long n) {
for (; n; --n, ++s) {
if ((*s & 255) == c) {
return s;
return (char *)s;
}
}
return 0;
@ -42,7 +42,7 @@ Recode16to8(char *dst, size_t dstsize, const char16_t *src) {
axdx_t r;
uint64_t w;
wint_t x, y;
for (v = r.ax = 0, r.dx = 0;;) {
for (v = false, r.ax = 0, r.dx = 0;;) {
if (!(x = src[r.dx++])) break;
if (!IsUcs2(x)) {
y = src[r.dx++];
@ -72,7 +72,6 @@ Recode16to8(char *dst, size_t dstsize, const char16_t *src) {
textwindows dontinstrument dontasan void FixPath(char *path) {
char *p;
size_t i;
// turn backslash into slash
for (p = path; *p; ++p) {

View file

@ -45,10 +45,8 @@
*/
char *GetInterpreterExecutableName(char *p, size_t n) {
int e;
size_t m;
int cmd[4];
ssize_t rc;
char *r, *t;
e = errno;
if (n < 2) {
errno = ENAMETOOLONG;

View file

@ -57,5 +57,5 @@ char *getlogin(void) {
res = 0;
}
STRACE("getlogin() → %#s% m", res);
return res;
return (char *)res;
}

View file

@ -25,11 +25,11 @@
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/str/str.h"
#include "libc/thread/thread.h"
#include "libc/x/x.h"
#include "libc/zip.internal.h"
#include "libc/runtime/zipos.internal.h"
#include "third_party/puff/puff.h"
__static_yoink("__get_symbol");
@ -56,8 +56,8 @@ static ssize_t GetZipFile(struct Zipos *zipos, const char *name) {
* @note This code can't depend on dlmalloc()
*/
static struct SymbolTable *GetSymbolTableFromZip(struct Zipos *zipos) {
ssize_t cf, lf;
size_t size, size2;
ssize_t rc, cf, lf;
struct SymbolTable *res = 0;
if ((cf = GetZipFile(zipos, ".symtab." _ARCH_NAME)) != -1 ||
(cf = GetZipFile(zipos, ".symtab")) != -1) {
@ -93,7 +93,6 @@ static struct SymbolTable *GetSymbolTableFromZip(struct Zipos *zipos) {
* @note This code can't depend on dlmalloc()
*/
static struct SymbolTable *GetSymbolTableFromElf(void) {
int e;
const char *s;
if (PLEDGED(RPATH) && (s = FindDebugBinary())) {
return OpenSymbolTable(s);

View file

@ -37,7 +37,7 @@ textstartup bool __intercept_flag(int *argc, char *argv[], const char *flag) {
* todo: update this to allow ------strace etc.
*/
if (a && a[0] == '-' && a[1] == '-' && !__strcmp(a + 2, flag)) {
a = flag;
a = (char *)flag;
}
argv[i++] = a;
}

View file

@ -40,7 +40,6 @@ void *__cxa_finalize(void *);
void __restore_console_win32(void);
void __stack_chk_fail(void) wontreturn relegated;
void __stack_chk_fail_local(void) wontreturn relegated;
void __asan_init(int, char **, char **, intptr_t *);
void _jmpstack(void *, void *, ...) wontreturn;
long _setstack(void *, void *, ...);
int GetDosArgv(const char16_t *, char *, size_t, char **, size_t);

View file

@ -240,10 +240,10 @@ static textwindows dontinline dontasan void *__map_memories(
dontasan inline void *__mmap_unlocked(void *addr, size_t size, int prot,
int flags, int fd, int64_t off) {
int a, f, n, x;
char *p = addr;
struct DirectMap dm;
size_t requested_size;
int a, b, i, f, m, n, x;
bool needguard, clashes;
unsigned long page_size;
size_t virtualused, virtualneed;
@ -465,12 +465,14 @@ dontasan inline void *__mmap_unlocked(void *addr, size_t size, int prot,
*/
void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) {
void *res;
#ifdef SYSDEBUG
size_t toto = 0;
#if defined(SYSDEBUG) && (_KERNTRACE || _NTTRACE)
#if _KERNTRACE || _NTTRACE
if (IsWindows()) {
STRACE("mmap(%p, %'zu, %s, %s, %d, %'ld) → ...", addr, size,
DescribeProtFlags(prot), DescribeMapFlags(flags), fd, off);
}
#endif
#endif
__mmi_lock();
if (!__isfdkind(fd, kFdZip)) {

View file

@ -70,7 +70,7 @@ static dontasan void __munmap_impl(char *p, size_t n) {
char *q;
size_t m;
intptr_t a, b, c;
int i, l, r, rc, beg, end;
int i, l, r, beg, end;
KERNTRACE("__munmap_impl(%p, %'zu)", p, n);
l = FRAME(p);
r = FRAME(p + n - 1);
@ -113,9 +113,6 @@ static dontasan void __munmap_impl(char *p, size_t n) {
}
dontasan int __munmap_unlocked(char *p, size_t n) {
unsigned i;
char poison;
intptr_t a, b, x, y;
unassert(!__vforked);
if (UNLIKELY(!n)) {
STRACE("munmap n is 0");
@ -154,11 +151,10 @@ dontasan int __munmap_unlocked(char *p, size_t n) {
*/
int munmap(void *p, size_t n) {
int rc;
size_t toto;
__mmi_lock();
rc = __munmap_unlocked(p, n);
#if SYSDEBUG
toto = __strace > 0 ? __get_memtrack_size(&_mmi) : 0;
size_t toto = __strace > 0 ? __get_memtrack_size(&_mmi) : 0;
#endif
__mmi_unlock();
STRACE("munmap(%.12p, %'zu) → %d% m (%'zu bytes total)", p, n, rc, toto);

View file

@ -159,24 +159,25 @@ dontasan textstartup void __printargs(const char *prologue) {
{"%-14d", AT_MINSIGSTKSZ, "AT_MINSIGSTKSZ"},
};
long key;
int e, x;
char **env;
sigset_t ss;
bool gotsome;
int e, x, flags;
unsigned i, n;
uintptr_t *auxp;
unsigned i, n, b;
struct rlimit rlim;
struct utsname uts;
struct sigaction sa;
struct sched_param sp;
struct termios termios;
struct AuxiliaryValue *auxinfo;
const struct AuxiliaryValue *auxinfo;
union {
char path[PATH_MAX];
struct pollfd pfds[128];
} u;
(void)x;
if (!PLEDGED(STDIO)) return;
ftrace_enabled(-1);
@ -351,7 +352,7 @@ dontasan textstartup void __printargs(const char *prologue) {
PRINT("");
PRINT("CAPABILITIES");
if (prctl(PR_CAPBSET_READ, 0) != -1) {
for (gotsome = i = 0; i <= CAP_LAST_CAP; ++i) {
for (gotsome = false, i = 0; i <= CAP_LAST_CAP; ++i) {
if (prctl(PR_CAPBSET_READ, i) == 1) {
char buf[64];
PRINT(" ☼ %s", (DescribeCapability)(buf, i));
@ -368,7 +369,7 @@ dontasan textstartup void __printargs(const char *prologue) {
PRINT("");
PRINT("RESOURCE LIMITS");
for (gotsome = i = 0; i < RLIM_NLIMITS; ++i) {
for (gotsome = false, i = 0; i < RLIM_NLIMITS; ++i) {
if (!getrlimit(i, &rlim)) {
char buf[20];
if (rlim.rlim_cur == RLIM_INFINITY) rlim.rlim_cur = -1;

View file

@ -25,7 +25,7 @@
#include "libc/nt/thread.h"
#include "libc/sysv/consts/arch.h"
#include "libc/thread/tls.h"
#include "libc/thread/tls2.h"
#include "libc/thread/tls2.internal.h"
int sys_set_tls();
@ -33,7 +33,6 @@ textstartup void __set_tls(struct CosmoTib *tib) {
tib = __adj_tls(tib);
#ifdef __x86_64__
// ask the operating system to change the x86 segment register
int ax, dx;
if (IsWindows()) {
__tls_index = TlsAlloc();
npassert(0 <= __tls_index && __tls_index < 64);

View file

@ -51,7 +51,6 @@
* @raise EINVAL if `name` isn't valid
*/
long sysconf(int name) {
int n;
switch (name) {
case _SC_CLK_TCK:
return CLK_TCK;

View file

@ -185,7 +185,7 @@ __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
wa->argv[0][i] = '/';
}
}
const char16_t *env16 = __imp_GetEnvironmentStringsW();
char16_t *env16 = __imp_GetEnvironmentStringsW();
GetDosEnviron(env16, wa->envblock, ARRAYLEN(wa->envblock) - 8, wa->envp,
ARRAYLEN(wa->envp) - 1);
__imp_FreeEnvironmentStringsW(env16);

View file

@ -46,7 +46,7 @@ __static_yoink(APE_COM_NAME);
static struct Zipos __zipos;
static atomic_uint __zipos_once;
static void __zipos_dismiss(const uint8_t *map, const uint8_t *cdir, long pg) {
static void __zipos_dismiss(uint8_t *map, const uint8_t *cdir, long pg) {
uint64_t i, n, c, ef, lf, mo, lo, hi;
// determine the byte range of zip file content (excluding central dir)
@ -154,6 +154,7 @@ static void __zipos_init(void) {
progpath = 0;
msg = -777;
}
(void)msg;
STRACE("__zipos_get(%#s) → %d% m", progpath, msg);
}

View file

@ -154,7 +154,7 @@ static int __zipos_load(struct Zipos *zipos, size_t cf, int flags,
struct ZiposUri *name) {
size_t lf;
size_t size;
int rc, fd, minfd;
int fd, minfd;
struct ZiposHandle *h;
if (cf == ZIPOS_SYNTHETIC_DIRECTORY) {
size = name->len;