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

@ -42,6 +42,6 @@ char *ctermid(char *s) {
if (s) {
return strcpy(s, tty);
} else {
return tty;
return (char *)tty;
}
}

View file

@ -294,13 +294,13 @@ DIR *fdopendir(int fd) {
// get path of this file descriptor and ensure trailing slash
size_t len;
char *name;
const char *name;
if (h->cfile != ZIPOS_SYNTHETIC_DIRECTORY) {
len = ZIP_CFILE_NAMESIZE(h->zipos->map + h->cfile);
name = ZIP_CFILE_NAME(h->zipos->map + h->cfile);
} else {
len = h->size;
name = (char *)h->data;
name = (const char *)h->data;
}
if (len + 2 > ZIPOS_PATH_MAX) {
free(dir);
@ -385,13 +385,13 @@ static struct dirent *readdir_zipos(DIR *dir) {
ent->d_ino = __zipos_inode(
dir->zip.zipos, __zipos_scan(dir->zip.zipos, &p), p.path, p.len);
} else {
uint8_t *s = ZIP_CFILE_NAME(dir->zip.zipos->map + dir->zip.offset);
const char *s = ZIP_CFILE_NAME(dir->zip.zipos->map + dir->zip.offset);
size_t n = ZIP_CFILE_NAMESIZE(dir->zip.zipos->map + dir->zip.offset);
if (n > dir->zip.prefix.len &&
!memcmp(dir->zip.prefix.path, s, dir->zip.prefix.len)) {
s += dir->zip.prefix.len;
n -= dir->zip.prefix.len;
uint8_t *p = memchr(s, '/', n);
const char *p = memchr(s, '/', n);
if (p) n = p - s;
if ((n = MIN(n, sizeof(ent->d_name) - 1)) &&
critbit0_emplace(&dir->zip.found, s, n) == 1) {

View file

@ -3,7 +3,7 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int dprintf(int, const char *, ...) printfesque(2) paramsnonnull((2));
int dprintf(int, const char *, ...) paramsnonnull((2));
int vdprintf(int, const char *, va_list) paramsnonnull();
COSMOPOLITAN_C_END_

View file

@ -38,7 +38,6 @@ FILE *fmemopen(void *buf, size_t size, const char *mode) {
FILE *f;
char *p;
int iomode;
unsigned flags;
iomode = fopenflags(mode);
if ((size && size > 0x7ffff000) || //
(!buf && (iomode & O_ACCMODE) != O_RDWR)) {

View file

@ -398,7 +398,7 @@ static int __fmt_stoa(int out(const char *, void *, size_t), void *arg,
unsigned n;
emit_f emit;
char *p, buf[1];
unsigned w, c, pad;
unsigned w, pad;
bool justdobytes, ignorenul;
p = data;
@ -604,7 +604,7 @@ static void __fmt_ldfpbits(union U *u, struct FPBits *b) {
// returns number of hex digits minus 1, or 0 for zero
static int __fmt_fpiprec(struct FPBits *b) {
FPI *fpi;
const FPI *fpi;
int i, j, k, m;
uint32_t *bits;
if (b->kind == STRTOG_Zero) return (b->ex = 0);
@ -794,10 +794,10 @@ int __fmt(void *fn, void *arg, const char *format, va_list va) {
wchar_t charbuf[1];
const char *alphabet;
unsigned char signbit, log2base;
int c, k, i1, bw, rc, bex, prec1, decpt;
int (*out)(const char *, void *, size_t);
char *se, *s0, *s, *q, qchar, special[8];
int d, w, n, sign, prec, flags, width, lasterr;
int c, k, i1, ui, bw, rc, bex, sgn, prec1, decpt;
x = 0;
lasterr = errno;
@ -852,7 +852,7 @@ int __fmt(void *fn, void *arg, const char *format, va_list va) {
continue;
} else if (format[1] == '.' && format[2] == '*' && format[3] == 's') {
n = va_arg(va, unsigned); // FAST PATH: PRECISION STRING
s = va_arg(va, const char *);
const char *s = va_arg(va, const char *);
if (s) {
n = strnlen(s, n);
} else {

View file

@ -38,7 +38,6 @@
*/
int fseek_unlocked(FILE *f, int64_t offset, int whence) {
int res;
ssize_t rc;
int64_t pos;
if (f->fd != -1) {
if (__fflush_impl(f) == -1) return -1;

View file

@ -26,7 +26,6 @@
static inline int64_t ftell_unlocked(FILE *f) {
int64_t pos;
uint32_t skew;
if (f->fd != -1) {
if (__fflush_impl(f) == -1) return -1;
if ((pos = lseek(f->fd, 0, SEEK_CUR)) != -1) {

View file

@ -101,7 +101,7 @@ size_t fwrite_unlocked(const void *data, size_t stride, size_t count, FILE *f) {
// (2) we avoid need for malloc() when it's out of room
iov[0].iov_base = f->buf;
iov[0].iov_len = f->beg;
iov[1].iov_base = data;
iov[1].iov_base = (void *)data;
iov[1].iov_len = n;
n += f->beg;
if (__robust_writev(f->fd, iov, 2) == -1) {

View file

@ -220,7 +220,7 @@ static void put_16(unsigned char *s, unsigned c, int e)
static unsigned get_32(const unsigned char *s, int e)
{
e &= 3;
return s[e]+0U<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
return (s[e]+0U)<<24 | s[e^1]<<16 | s[e^2]<<8 | s[e^3];
}
static void put_32(unsigned char *s, unsigned c, int e)
@ -240,7 +240,7 @@ static unsigned legacy_map(const unsigned char *map, unsigned c)
{
if (c < 4*map[-1]) return c;
unsigned x = c - 4*map[-1];
x = map[x*5/4]>>2*x%8 | map[x*5/4+1]<<8-2*x%8 & 1023;
x = (map[x*5/4]>>(2*x%8)) | ((map[x*5/4+1]<<(8-(2*x%8))) & 1023);
return x < 256 ? x : legacy_chars[x-256];
}
@ -535,7 +535,7 @@ size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restri
if (c >= 93 || d >= 94) {
c += (0xa1-0x81);
d += 0xa1;
if (c >= 93 || c>=0xc6-0x81 && d>0x52)
if (c >= 93 || ((c>=0xc6-0x81) && d>0x52))
goto ilseq;
if (d-'A'<26) d = d-'A';
else if (d-'a'<26) d = d-'a'+26;

View file

@ -77,7 +77,14 @@ FILE *popen(const char *cmdline, const char *mode) {
// we can't rely on cloexec because cocmd builtins don't execve
if (pipefds[0] != !dir) unassert(!close(pipefds[0]));
if (pipefds[1] != !dir) unassert(!close(pipefds[1]));
_Exit(_cocmd(3, (char *[]){"popen", "-c", cmdline, 0}, environ));
_Exit(_cocmd(3,
(char *[]){
"popen",
"-c",
(char *)cmdline,
0,
},
environ));
default:
f->pid = pid;
unassert(!close(pipefds[!dir]));

View file

@ -31,7 +31,7 @@ struct _posix_faction {
int newfildes;
unsigned mode;
};
const char *path;
char *path;
};
COSMOPOLITAN_C_END_

View file

@ -117,13 +117,14 @@ int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *file_actions,
int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *file_actions,
int fildes, const char *path, int oflag,
unsigned mode) {
char *path2;
if (fildes < 0) return EBADF;
if (IsWindows() && fildes > 2) return ENOTSUP;
if (!(path = strdup(path))) return ENOMEM;
if (!(path2 = strdup(path))) return ENOMEM;
return AddFileAction(file_actions, (struct _posix_faction){
.action = _POSIX_SPAWN_OPEN,
.fildes = fildes,
.path = path,
.path = path2,
.oflag = oflag,
.mode = mode,
});

View file

@ -20,20 +20,6 @@
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
static inline int puts_unlocked(const char *s, FILE *f) {
size_t n, r;
if ((n = strlen(s))) {
r = fwrite_unlocked(s, 1, n, f);
if (!r) return -1;
if (r < n) return r;
}
if (fputc_unlocked('\n', f) == -1) {
if (feof_unlocked(f)) return n;
return -1;
}
return n + 1;
}
/**
* Writes string w/ trailing newline to stdout.
*
@ -41,11 +27,9 @@ static inline int puts_unlocked(const char *s, FILE *f) {
* `errno` set and the `ferror(stdout)` state is updated
*/
int puts(const char *s) {
FILE *f;
int bytes;
f = stdout;
flockfile(f);
bytes = puts_unlocked(s, f);
funlockfile(f);
flockfile(stdout);
bytes = puts_unlocked(s);
funlockfile(stdout);
return bytes;
}

View file

@ -0,0 +1,40 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
/**
* Writes string w/ trailing newline to stdout.
*
* @return non-negative number on success, or `EOF` on error with
* `errno` set and the `ferror(stdout)` state is updated
*/
int puts_unlocked(const char *s) {
size_t n, r;
if ((n = strlen(s))) {
r = fwrite_unlocked(s, 1, n, stdout);
if (!r) return -1;
if (r < n) return r;
}
if (fputc_unlocked('\n', stdout) == -1) {
if (feof_unlocked(stdout)) return n;
return -1;
}
return n + 1;
}

View file

@ -43,7 +43,7 @@
*/
dontasan void *rngset(void *b, size_t n, uint64_t seed(void), size_t reseed) {
size_t m;
uint64_t i, x, t = 0;
uint64_t x, t = 0;
unsigned char *p = b;
if (IsAsan()) {
__asan_verify(b, n);

View file

@ -123,6 +123,7 @@ int vasprintf(char **, const char *, va_list) paramsnonnull() libcesque;
*/
int getc_unlocked(FILE *) paramsnonnull();
int puts_unlocked(const char *);
int getchar_unlocked(void);
int putc_unlocked(int, FILE *) paramsnonnull();
int putchar_unlocked(int);

View file

@ -72,7 +72,7 @@ int system(const char *cmdline) {
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
if (!(pid = fork())) {
sigprocmask(SIG_SETMASK, &savemask, 0);
_Exit(_cocmd(3, (char *[]){"system", "-c", cmdline, 0}, environ));
_Exit(_cocmd(3, (char *[]){"system", "-c", (char *)cmdline, 0}, environ));
} else if (pid == -1) {
wstatus = -1;
} else {

View file

@ -326,7 +326,6 @@ int __vcscanf(int callback(void *), //
} else if (tpdecodecb((wint_t *)&c, c, (void *)callback, arg) !=
-1) {
if (charbytes == sizeof(char16_t)) {
size_t k = 0;
unsigned w = EncodeUtf16(c);
do {
if ((j + 1) * 2 < bufsize) {

View file

@ -40,7 +40,7 @@ static int vdprintf_putc(const char *s, struct VdprintfState *t, size_t n) {
} else {
iov[0].iov_base = t->b;
iov[0].iov_len = t->n;
iov[1].iov_base = s;
iov[1].iov_base = (void *)s;
iov[1].iov_len = n;
if (__robust_writev(t->fd, iov, 2) == -1) {
return -1;