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

@ -29,7 +29,7 @@ int LuaEncodeJsonData(lua_State *, char **, int, struct EncoderConfig);
int LuaEncodeLuaData(lua_State *, char **, int, struct EncoderConfig);
int LuaEncodeUrl(lua_State *);
int LuaParseUrl(lua_State *);
int LuaPushHeader(lua_State *, struct HttpMessage *, char *, int);
int LuaPushHeader(lua_State *, struct HttpMessage *, const char *, int);
int LuaPushHeaders(lua_State *, struct HttpMessage *, const char *);
void LuaPrintStack(lua_State *);
void LuaPushLatin1(lua_State *, const char *, size_t);

View file

@ -73,7 +73,7 @@ linenoiseCompletionCallback *lua_repl_completions_callback;
struct linenoiseState *lua_repl_linenoise;
const char *lua_progname;
static lua_State *globalL;
static const char *g_historypath;
static char *g_historypath;
/*
** {==================================================================
@ -105,8 +105,9 @@ void lua_readline_completions (const char *p, linenoiseCompletions *c) {
size_t n;
bool found;
lua_State *L;
const char *a;
const char *name;
char *a, *b, *s, *component;
char *b, *s, *component;
// start searching globals
L = globalL;
@ -318,7 +319,6 @@ static void lstop (lua_State *L, lua_Debug *ar) {
static int multiline (lua_State *L) {
for (;;) { /* repeat until gets a complete statement */
size_t len;
ssize_t rc;
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
if (!incomplete(L, status) || pushline(L, 0) != 1)

View file

@ -70,9 +70,6 @@ STATIC_STACK_SIZE(0x80000);
#define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX
static lua_State *globalL = NULL;
static bool lua_stdin_is_tty(void) {
return isatty(0);
}

View file

@ -69,8 +69,8 @@ OnError:
static int SerializeString(lua_State *L, char **buf, int idx,
struct Serializer *z) {
char *s;
size_t m;
const char *s;
s = lua_tolstring(L, idx, &m);
if (!(s = EscapeJsStringLiteral(&z->strbuf, &z->strbuflen, s, m, &m))) {
goto OnError;

View file

@ -113,7 +113,7 @@ OnError:
static int SerializeNumber(lua_State *L, char **buf, int idx) {
int64_t x;
char ibuf[24];
char ibuf[128];
if (lua_isinteger(L, idx)) {
x = luaL_checkinteger(L, idx);
if (x == -9223372036854775807 - 1) {
@ -252,7 +252,6 @@ OnError:
static int SerializeArray(lua_State *L, char **buf, struct Serializer *z,
int depth) {
size_t i, n;
const char *s;
RETURN_ON_ERROR(appendw(buf, '{'));
n = lua_rawlen(L, -1);
for (i = 1; i <= n; i++) {
@ -269,7 +268,6 @@ OnError:
static int SerializeObject(lua_State *L, char **buf, struct Serializer *z,
int depth, bool multi) {
int rc;
size_t n;
const char *s;
bool comma = false;
@ -308,8 +306,8 @@ OnError:
static int SerializeSorted(lua_State *L, char **buf, struct Serializer *z,
int depth, bool multi) {
int i;
size_t n;
int i, rc;
const char *s;
struct StrList sl = {0};
lua_pushnil(L);
@ -354,7 +352,6 @@ static int SerializeTable(lua_State *L, char **buf, int idx,
struct Serializer *z, int depth) {
int rc;
bool multi;
intptr_t rsp, bot;
if (UNLIKELY(!HaveStackMemory(getauxval(AT_PAGESZ)))) {
z->reason = "out of stack";
return -1;

View file

@ -25,23 +25,28 @@
#include "third_party/lua/lua.h"
int LuaEncodeUrl(lua_State *L) {
char *data;
size_t size;
struct Url h;
int i, j, n;
const char *data;
struct Url h;
if (!lua_isnil(L, 1)) {
i = lua_gettop(L);
bzero(&h, sizeof(h));
luaL_checktype(L, 1, LUA_TTABLE);
if (lua_getfield(L, 1, "scheme"))
h.scheme.p = lua_tolstring(L, -1, &h.scheme.n);
h.scheme.p = (char *)lua_tolstring(L, -1, &h.scheme.n);
if (lua_getfield(L, 1, "fragment"))
h.fragment.p = lua_tolstring(L, -1, &h.fragment.n);
if (lua_getfield(L, 1, "user")) h.user.p = lua_tolstring(L, -1, &h.user.n);
if (lua_getfield(L, 1, "pass")) h.pass.p = lua_tolstring(L, -1, &h.pass.n);
if (lua_getfield(L, 1, "host")) h.host.p = lua_tolstring(L, -1, &h.host.n);
if (lua_getfield(L, 1, "port")) h.port.p = lua_tolstring(L, -1, &h.port.n);
if (lua_getfield(L, 1, "path")) h.path.p = lua_tolstring(L, -1, &h.path.n);
h.fragment.p = (char *)lua_tolstring(L, -1, &h.fragment.n);
if (lua_getfield(L, 1, "user"))
h.user.p = (char *)lua_tolstring(L, -1, &h.user.n);
if (lua_getfield(L, 1, "pass"))
h.pass.p = (char *)lua_tolstring(L, -1, &h.pass.n);
if (lua_getfield(L, 1, "host"))
h.host.p = (char *)lua_tolstring(L, -1, &h.host.n);
if (lua_getfield(L, 1, "port"))
h.port.p = (char *)lua_tolstring(L, -1, &h.port.n);
if (lua_getfield(L, 1, "path"))
h.path.p = (char *)lua_tolstring(L, -1, &h.path.n);
lua_settop(L, i); // restore stack position
if (lua_getfield(L, 1, "params")) {
luaL_checktype(L, -1, LUA_TTABLE);
@ -55,10 +60,10 @@ int LuaEncodeUrl(lua_State *L) {
h.params.p =
xrealloc(h.params.p, ++h.params.n * sizeof(*h.params.p));
h.params.p[h.params.n - 1].key.p =
lua_tolstring(L, -1, &h.params.p[h.params.n - 1].key.n);
(char *)lua_tolstring(L, -1, &h.params.p[h.params.n - 1].key.n);
if (lua_geti(L, -2, 2)) {
h.params.p[h.params.n - 1].val.p =
lua_tolstring(L, -1, &h.params.p[h.params.n - 1].val.n);
h.params.p[h.params.n - 1].val.p = (char *)lua_tolstring(
L, -1, &h.params.p[h.params.n - 1].val.n);
} else {
h.params.p[h.params.n - 1].val.p = 0;
h.params.p[h.params.n - 1].val.n = 0;

View file

@ -22,7 +22,7 @@
#include "third_party/lua/lauxlib.h"
#include "third_party/lua/lua.h"
int LuaPushHeader(lua_State *L, struct HttpMessage *m, char *b, int h) {
int LuaPushHeader(lua_State *L, struct HttpMessage *m, const char *b, int h) {
char *val;
size_t vallen;
if (!kHttpRepeatable[h]) {

View file

@ -203,8 +203,8 @@ static dontinline int ReturnString(lua_State *L, const char *x) {
}
int LuaUnixSysretErrno(lua_State *L, const char *call, int olderr) {
int unixerr, winerr;
struct UnixErrno *ep;
int i, unixerr, winerr;
unixerr = errno;
winerr = IsWindows() ? GetLastError() : 0;
if (!IsTiny() && !(0 < unixerr && unixerr < (!IsWindows() ? 4096 : 65536))) {
@ -591,7 +591,7 @@ static int LuaUnixExecve(lua_State *L) {
return LuaUnixSysretErrno(L, "execve", olderr);
}
} else {
ezargs[0] = prog;
ezargs[0] = (char *)prog;
ezargs[1] = 0;
argv = ezargs;
envp = environ;
@ -736,7 +736,7 @@ static int LuaUnixWait(lua_State *L) {
// └─→ nil, unix.Errno
static int LuaUnixFcntl(lua_State *L) {
struct flock lock;
int rc, fd, cmd, olderr = errno;
int fd, cmd, olderr = errno;
fd = luaL_checkinteger(L, 1);
cmd = luaL_checkinteger(L, 2);
if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK) {
@ -920,7 +920,6 @@ static int LuaUnixSetresgid(lua_State *L) {
// ├─→ 0
// └─→ nil, unix.Errno
static int LuaUnixUtimensat(lua_State *L) {
struct timespec ts;
int olderr = errno;
return SysretInteger(
L, "utimensat", olderr,
@ -937,7 +936,6 @@ static int LuaUnixUtimensat(lua_State *L) {
// ├─→ 0
// └─→ nil, unix.Errno
static int LuaUnixFutimens(lua_State *L) {
struct timespec ts;
int olderr = errno;
return SysretInteger(
L, "futimens", olderr,
@ -953,7 +951,7 @@ static int LuaUnixFutimens(lua_State *L) {
// └─→ nil, unix.Errno
static int LuaUnixGettime(lua_State *L) {
struct timespec ts;
int rc, olderr = errno;
int olderr = errno;
if (!clock_gettime(luaL_optinteger(L, 1, CLOCK_REALTIME), &ts)) {
lua_pushinteger(L, ts.tv_sec);
lua_pushinteger(L, ts.tv_nsec);
@ -1178,14 +1176,14 @@ static bool IsSockoptBool(int l, int x) {
x == SO_KEEPALIVE || //
x == SO_ACCEPTCONN || //
x == SO_DONTROUTE; //
} else if (l = SOL_TCP) {
} else if (l == SOL_TCP) {
return x == TCP_NODELAY || //
x == TCP_CORK || //
x == TCP_QUICKACK || //
x == TCP_SAVE_SYN || //
x == TCP_FASTOPEN_CONNECT || //
x == TCP_DEFER_ACCEPT; //
} else if (l = SOL_IP) {
} else if (l == SOL_IP) {
return x == IP_HDRINCL; //
} else {
return false;
@ -1198,7 +1196,7 @@ static bool IsSockoptInt(int l, int x) {
x == SO_RCVBUF || //
x == SO_RCVLOWAT || //
x == SO_SNDLOWAT; //
} else if (l = SOL_TCP) {
} else if (l == SOL_TCP) {
return x == TCP_FASTOPEN || //
x == TCP_KEEPCNT || //
x == TCP_MAXSEG || //
@ -1207,7 +1205,7 @@ static bool IsSockoptInt(int l, int x) {
x == TCP_WINDOW_CLAMP || //
x == TCP_KEEPIDLE || //
x == TCP_KEEPINTVL; //
} else if (l = SOL_IP) {
} else if (l == SOL_IP) {
return x == IP_TOS || //
x == IP_MTU || //
x == IP_TTL; //
@ -1230,7 +1228,7 @@ static int LuaUnixSetsockopt(lua_State *L) {
struct linger l;
uint32_t optsize;
struct timeval tv;
int rc, fd, level, optname, optint, olderr = errno;
int fd, level, optname, optint, olderr = errno;
fd = luaL_checkinteger(L, 1);
level = luaL_checkinteger(L, 2);
optname = luaL_checkinteger(L, 3);
@ -1281,7 +1279,7 @@ static int LuaUnixGetsockopt(lua_State *L) {
uint32_t size;
struct linger l;
struct timeval tv;
int rc, fd, level, optname, optval, olderr = errno;
int fd, level, optname, optval, olderr = errno;
fd = luaL_checkinteger(L, 1);
level = luaL_checkinteger(L, 2);
optname = luaL_checkinteger(L, 3);
@ -1547,7 +1545,7 @@ static int LuaUnixPoll(lua_State *L) {
struct sigset *mask;
struct timespec ts, *tsp;
struct pollfd *fds, *fds2;
int i, fd, events, olderr = errno;
int i, events, olderr = errno;
luaL_checktype(L, 1, LUA_TTABLE);
if (!lua_isnoneornil(L, 2)) {
ts = timespec_frommillis(luaL_checkinteger(L, 2));
@ -1633,7 +1631,7 @@ static int LuaUnixRecv(lua_State *L) {
size_t got;
ssize_t rc;
lua_Integer bufsiz;
int fd, flags, pushed, olderr = errno;
int fd, flags, olderr = errno;
fd = luaL_checkinteger(L, 1);
bufsiz = luaL_optinteger(L, 2, 1500);
bufsiz = MIN(bufsiz, 0x7ffff000);
@ -1655,9 +1653,8 @@ static int LuaUnixRecv(lua_State *L) {
// ├─→ sent:int
// └─→ nil, unix.Errno
static int LuaUnixSend(lua_State *L) {
char *data;
ssize_t rc;
size_t sent, size;
size_t size;
const char *data;
int fd, flags, olderr = errno;
fd = luaL_checkinteger(L, 1);
data = luaL_checklstring(L, 2, &size);
@ -1670,9 +1667,9 @@ static int LuaUnixSend(lua_State *L) {
// ├─→ sent:int
// └─→ nil, unix.Errno
static int LuaUnixSendto(lua_State *L) {
char *data;
size_t size;
uint32_t salen;
const char *data;
struct sockaddr_storage ss;
int i, fd, flags, olderr = errno;
fd = luaL_checkinteger(L, 1);
@ -1697,7 +1694,6 @@ static int LuaUnixShutdown(lua_State *L) {
// ├─→ oldmask:unix.Sigset
// └─→ nil, unix.Errno
static int LuaUnixSigprocmask(lua_State *L) {
uint64_t imask;
int olderr = errno;
struct sigset oldmask;
if (!sigprocmask(luaL_checkinteger(L, 1),
@ -2734,7 +2730,7 @@ static int LuaUnixMemoryWrite(lua_State *L) {
if (lua_isnoneornil(L, b)) {
// unix.Memory:write(data:str[, offset:int])
// writes binary data, plus a nul terminator
if (i < n < m->size) {
if (i < n && n < m->size) {
// include lua string's implicit nul so this round trips with
// unix.Memory:read(offset:int) even when we're overwriting a
// larger string that was previously inserted
@ -2842,7 +2838,7 @@ static int LuaUnixMemoryXor(lua_State *L) {
static int LuaUnixMemoryWait(lua_State *L) {
lua_Integer expect;
int rc, olderr = errno;
struct timespec ts, now, *deadline;
struct timespec ts, *deadline;
expect = luaL_checkinteger(L, 3);
if (!(INT32_MIN <= expect && expect <= INT32_MAX)) {
luaL_argerror(L, 3, "must be an int32_t");
@ -3351,7 +3347,8 @@ static const luaL_Reg kLuaUnix[] = {
{0}, //
};
static void LoadMagnums(lua_State *L, struct MagnumStr *ms, const char *pfx) {
static void LoadMagnums(lua_State *L, const struct MagnumStr *ms,
const char *pfx) {
int i;
char b[64], *p;
p = stpcpy(b, pfx);