Apply clang-format update to repo (#1154)

Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)

I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.

My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.

It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.

fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
This commit is contained in:
Jōshin 2024-04-25 10:38:00 -07:00 committed by GitHub
parent 342d0c81e5
commit 6e6fc38935
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
863 changed files with 9201 additions and 4627 deletions

View file

@ -118,7 +118,8 @@ WinThreadEntry(int rdi, // rcx
int rdx, // r8
struct CloneArgs *wt) { // r9
int rc;
if (wt->tls) __set_tls_win32(wt->tls);
if (wt->tls)
__set_tls_win32(wt->tls);
*wt->ctid = wt->tid;
rc = __stack_call(wt->arg, wt->tid, 0, 0, wt->func, wt);
// we can now clear ctid directly since we're no longer using our own
@ -465,17 +466,20 @@ static errno_t CloneFreebsd(int (*func)(void *, int), char *stk, size_t stksz,
: CFLAG_CONSTRAINT(failed), "=a"(ax)
: "1"(__NR_thr_new), "D"(&params), "S"(sizeof(params))
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory");
if (failed) return ax;
if (failed)
return ax;
#elif defined(__aarch64__)
register long x0 asm("x0") = (long)&params;
register long x1 asm("x1") = sizeof(params);
register int x8 asm("x8") = 0x1c7; // thr_new
asm volatile("svc\t0" : "+r"(x0) : "r"(x1), "r"(x8) : "memory");
if (x0) return x0;
if (x0)
return x0;
#else
#error "unsupported architecture"
#endif
if (flags & CLONE_PARENT_SETTID) *ptid = tid;
if (flags & CLONE_PARENT_SETTID)
*ptid = tid;
return 0;
}

View file

@ -25,7 +25,8 @@
*/
int CloseSymbolTable(struct SymbolTable **table) {
struct SymbolTable *t;
if (!*table) return 0;
if (!*table)
return 0;
t = *table;
*table = 0;
return munmap(t, t->mapsize);

View file

@ -104,7 +104,8 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1, char *exename,
char **argv = (char **)(sp + 1);
char **envp = (char **)(sp + 1 + argc + 1);
unsigned long *auxv = (unsigned long *)(sp + 1 + argc + 1);
while (*auxv++) donothing;
while (*auxv++)
donothing;
// set helpful globals
__argc = argc;

View file

@ -109,7 +109,8 @@ static void EfiInitVga(struct mman *mm, EFI_SYSTEM_TABLE *SystemTable) {
default:
notpossible;
}
if (!bytes_per_pix) notpossible;
if (!bytes_per_pix)
notpossible;
mm->pc_video_type = vid_typ;
mm->pc_video_stride = GraphMode->Info->PixelsPerScanLine * bytes_per_pix;
mm->pc_video_width = GraphMode->Info->HorizontalResolution;
@ -217,7 +218,8 @@ __msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
SystemTable->BootServices->HandleProtocol(ImageHandle,
&kEfiLoadedImageProtocol, &ImgInfo);
CmdLine = (const char16_t *)ImgInfo->LoadOptions;
if (!CmdLine || !CmdLine[0]) CmdLine = u"BOOTX64.EFI";
if (!CmdLine || !CmdLine[0])
CmdLine = u"BOOTX64.EFI";
Args = GetDosArgv(CmdLine, ArgBlock->ArgBlock, sizeof(ArgBlock->ArgBlock),
ArgBlock->Args, ARRAYLEN(ArgBlock->Args));
@ -226,7 +228,8 @@ __msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
* TODO: if needed, switch to a video mode that has a linear frame buffer
* type we support.
*/
if (_weaken(vga_console)) EfiInitVga(mm, SystemTable);
if (_weaken(vga_console))
EfiInitVga(mm, SystemTable);
/*
* Gets a pointer to the ACPI RSDP.
@ -250,7 +253,8 @@ __msabi EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
case EfiLoaderData:
case EfiBootServicesCode:
case EfiBootServicesData:
if (Desc->PhysicalStart != 0) break;
if (Desc->PhysicalStart != 0)
break;
/* fallthrough */
case EfiConventionalMemory:
mm->e820[j].addr = Desc->PhysicalStart;

View file

@ -24,27 +24,48 @@
#include "libc/sysv/errfuns.h"
long fpathconf(int fd, int name) {
if (name == _PC_LINK_MAX) return _POSIX_LINK_MAX;
if (name == _PC_MAX_CANON) return _POSIX_MAX_CANON;
if (name == _PC_MAX_INPUT) return _POSIX_MAX_INPUT;
if (name == _PC_NAME_MAX) return NAME_MAX;
if (name == _PC_PATH_MAX) return PATH_MAX;
if (name == _PC_PIPE_BUF) return PIPE_BUF;
if (name == _PC_CHOWN_RESTRICTED) return 1;
if (name == _PC_NO_TRUNC) return 1;
if (name == _PC_VDISABLE) return 0;
if (name == _PC_SYNC_IO) return 1;
if (name == _PC_ASYNC_IO) return -1;
if (name == _PC_PRIO_IO) return -1;
if (name == _PC_SOCK_MAXBUF) return -1;
if (name == _PC_FILESIZEBITS) return FILESIZEBITS;
if (name == _PC_REC_INCR_XFER_SIZE) return 4096;
if (name == _PC_REC_MAX_XFER_SIZE) return 4096;
if (name == _PC_REC_MIN_XFER_SIZE) return 4096;
if (name == _PC_REC_XFER_ALIGN) return 4096;
if (name == _PC_ALLOC_SIZE_MIN) return 4096;
if (name == _PC_SYMLINK_MAX) return -1;
if (name == _PC_2_SYMLINKS) return 1;
if (name == _PC_LINK_MAX)
return _POSIX_LINK_MAX;
if (name == _PC_MAX_CANON)
return _POSIX_MAX_CANON;
if (name == _PC_MAX_INPUT)
return _POSIX_MAX_INPUT;
if (name == _PC_NAME_MAX)
return NAME_MAX;
if (name == _PC_PATH_MAX)
return PATH_MAX;
if (name == _PC_PIPE_BUF)
return PIPE_BUF;
if (name == _PC_CHOWN_RESTRICTED)
return 1;
if (name == _PC_NO_TRUNC)
return 1;
if (name == _PC_VDISABLE)
return 0;
if (name == _PC_SYNC_IO)
return 1;
if (name == _PC_ASYNC_IO)
return -1;
if (name == _PC_PRIO_IO)
return -1;
if (name == _PC_SOCK_MAXBUF)
return -1;
if (name == _PC_FILESIZEBITS)
return FILESIZEBITS;
if (name == _PC_REC_INCR_XFER_SIZE)
return 4096;
if (name == _PC_REC_MAX_XFER_SIZE)
return 4096;
if (name == _PC_REC_MIN_XFER_SIZE)
return 4096;
if (name == _PC_REC_XFER_ALIGN)
return 4096;
if (name == _PC_ALLOC_SIZE_MIN)
return 4096;
if (name == _PC_SYMLINK_MAX)
return -1;
if (name == _PC_2_SYMLINKS)
return 1;
return einval();
}

View file

@ -61,7 +61,8 @@ __funline int GetNestingLevelImpl(struct StackFrame *frame) {
__funline int GetNestingLevel(struct CosmoFtrace *ft, struct StackFrame *sf) {
int nesting;
nesting = GetNestingLevelImpl(sf);
if (nesting < ft->ft_skew) ft->ft_skew = nesting;
if (nesting < ft->ft_skew)
ft->ft_skew = nesting;
nesting -= ft->ft_skew;
return MIN(MAX_NESTING, nesting);
}
@ -84,10 +85,12 @@ privileged void ftracer(void) {
struct PosixThread *pt;
sf = __builtin_frame_address(0);
st = (uintptr_t)__argv - sizeof(uintptr_t);
if (__ftrace <= 0) return;
if (__ftrace <= 0)
return;
if (__tls_enabled) {
tib = __get_tls_privileged();
if (tib->tib_ftrace <= 0) return;
if (tib->tib_ftrace <= 0)
return;
ft = &tib->tib_ftracer;
if ((char *)sf >= tib->tib_sigstack_addr &&
(char *)sf <= tib->tib_sigstack_addr + tib->tib_sigstack_size) {

View file

@ -21,6 +21,7 @@
long __get_avphys_pages(void) {
struct sysinfo si;
if (sysinfo(&si) == -1) return -1;
if (sysinfo(&si) == -1)
return -1;
return (((int64_t)si.freeram + si.bufferram) * si.mem_unit) / FRAMESIZE;
}

View file

@ -31,7 +31,8 @@ struct DosArgv {
textwindows void DecodeDosArgv(int ignore, struct DosArgv *st) {
wint_t x, y;
for (;;) {
if (!(x = *st->s++)) break;
if (!(x = *st->s++))
break;
if (!IsUcs2(x)) {
if ((y = *st->s++)) {
x = MergeUtf16(x, y);
@ -48,7 +49,8 @@ static textwindows void AppendDosArgv(wint_t wc, struct DosArgv *st) {
uint64_t w;
w = tpenc(wc);
do {
if (st->p >= st->pe) break;
if (st->p >= st->pe)
break;
*st->p++ = w & 0xff;
} while (w >>= 8);
}
@ -97,13 +99,15 @@ textwindows int GetDosArgv(const char16_t *cmdline, char *buf, size_t size,
while (st->wc && (st->wc == ' ' || st->wc == '\t')) {
DecodeDosArgv(ignore, st);
}
if (!st->wc) break;
if (!st->wc)
break;
if (++argc < max) {
argv[argc - 1] = st->p < st->pe ? st->p : NULL;
}
inquote = false;
while (st->wc) {
if (!inquote && (st->wc == ' ' || st->wc == '\t')) break;
if (!inquote && (st->wc == ' ' || st->wc == '\t'))
break;
if (st->wc == '"' || st->wc == '\\') {
slashes = Count('\\', st);
quotes = Count('"', st);
@ -121,7 +125,8 @@ textwindows int GetDosArgv(const char16_t *cmdline, char *buf, size_t size,
quotes--;
}
if (quotes > 0) {
if (!inquote) quotes--;
if (!inquote)
quotes--;
for (i = 3; i <= quotes + 1; i += 3) {
AppendDosArgv('"', st);
}
@ -136,7 +141,9 @@ textwindows int GetDosArgv(const char16_t *cmdline, char *buf, size_t size,
AppendDosArgv('\0', st);
}
AppendDosArgv('\0', st);
if (size) buf[MIN(st->p - buf, size - 1)] = '\0';
if (max) argv[MIN(argc, max - 1)] = NULL;
if (size)
buf[MIN(st->p - buf, size - 1)] = '\0';
if (max)
argv[MIN(argc, max - 1)] = NULL;
return argc;
}

View file

@ -43,7 +43,8 @@ static abi axdx_t Recode16to8(char *dst, size_t dstsize, const char16_t *src) {
uint64_t w;
wint_t x, y;
for (v = false, r.ax = 0, r.dx = 0;;) {
if (!(x = src[r.dx++])) break;
if (!(x = src[r.dx++]))
break;
if (!IsUcs2(x)) {
y = src[r.dx++];
x = MergeUtf16(x, y);
@ -117,7 +118,8 @@ abi int GetDosEnviron(const char16_t *env, char *buf, size_t size, char **envp,
i = 0;
--size;
while (*env) {
if (i + 1 < max) envp[i++] = buf;
if (i + 1 < max)
envp[i++] = buf;
r = Recode16to8(buf, size, env);
if ((p = MemChr(buf, '=', r.ax)) && IsAlpha(p[1]) && p[2] == ':' &&
(p[3] == '\\' || p[3] == '/')) {

View file

@ -21,6 +21,7 @@
long __get_phys_pages(void) {
struct sysinfo si;
if (sysinfo(&si) == -1) return -1;
if (sysinfo(&si) == -1)
return -1;
return ((int64_t)si.totalram * si.mem_unit) / FRAMESIZE;
}

View file

@ -23,7 +23,9 @@
long __get_rlimit(int resource) {
struct rlimit rl;
if (getrlimit(resource, &rl) == -1) return -1;
if (rl.rlim_cur == RLIM_INFINITY) return -1;
if (getrlimit(resource, &rl) == -1)
return -1;
if (rl.rlim_cur == RLIM_INFINITY)
return -1;
return MIN(rl.rlim_cur, LONG_MAX);
}

View file

@ -29,6 +29,7 @@ char *GetSymbolByAddr(int64_t addr) {
struct SymbolTable *st;
st = GetSymbolTable();
i = __get_symbol(st, addr);
if (i == -1) i = __get_symbol(st, addr - 1);
if (i == -1)
i = __get_symbol(st, addr - 1);
return __get_symbol_name(st, i);
}

View file

@ -122,7 +122,8 @@ static struct SymbolTable *GetSymbolTableFromElf(void) {
*/
struct SymbolTable *GetSymbolTable(void) {
struct Zipos *z;
if (pthread_spin_trylock(&g_lock)) return 0;
if (pthread_spin_trylock(&g_lock))
return 0;
if (!__symtab && !__isworker) {
if (_weaken(__zipos_get) && (z = _weaken(__zipos_get)())) {
if ((__symtab = GetSymbolTableFromZip(z))) {

View file

@ -46,7 +46,8 @@ bool __grow(void *pp, size_t *capacity, size_t itemsize, size_t extra) {
n2 = (*p ? n1 + (n1 >> 1) : MAX(4, INITIAL_CAPACITY / itemsize)) + extra;
if (!ckd_mul(&t1, n1, itemsize) && !ckd_mul(&t2, n2, itemsize)) {
if (_weaken(realloc) && (p2 = _weaken(realloc)(p1, ROUNDUP(t2, 32)))) {
if (!p1 && *p) memcpy(p2, *p, t1);
if (!p1 && *p)
memcpy(p2, *p, t1);
bzero((char *)p2 + t1, t2 - t1);
*capacity = n2;
*p = p2;

View file

@ -36,17 +36,21 @@ static privileged bool IsVirginFunction(const code_t *func) {
long i;
// function must be preceeded by 9 nops
for (i = -9; i < 0; ++i) {
if (func[i] != 0x90) return false;
if (func[i] != 0x90)
return false;
}
// function must start with `nop nop` or `xchg %ax,%ax`
if (func[0] == 0x90 && func[1] == 0x90) return true;
if (func[0] == 0x66 && func[1] == 0x90) return true;
if (func[0] == 0x90 && func[1] == 0x90)
return true;
if (func[0] == 0x66 && func[1] == 0x90)
return true;
return false;
#elif defined(__aarch64__)
long i;
// function must be preceeded by 6 nops
for (i = -6; i < 0; ++i) {
if (func[i] != 0xd503201f) return false;
if (func[i] != 0xd503201f)
return false;
}
// function must start with one nop
return func[0] == 0xd503201f;
@ -57,7 +61,8 @@ static privileged void HookFunction(code_t *func, void *dest) {
long dp;
#ifdef __x86_64__
dp = (intptr_t)dest - (intptr_t)(func - 7 + 5);
if (!(INT32_MIN <= dp && dp <= INT32_MAX)) return;
if (!(INT32_MIN <= dp && dp <= INT32_MAX))
return;
// emit `ud2` signature for safety and checkability
func[-9] = 0x0f;
func[-8] = 0x0b;
@ -75,7 +80,8 @@ static privileged void HookFunction(code_t *func, void *dest) {
func[+1] = -7 - 2;
#elif defined(__aarch64__)
dp = (code_t *)dest - (func - 3);
if (!(-33554432 <= dp && dp <= +33554431)) return;
if (!(-33554432 <= dp && dp <= +33554431))
return;
func[-6] = 0xd4200000 | (31337 << 5); // brk #31337
func[-5] = 0xa9bf7bfd; // stp x29,x30,[sp, #-16]!
func[-4] = 0x910003fd; // mov x29,sp
@ -110,16 +116,21 @@ privileged int __hook(void *dest, struct SymbolTable *st) {
long i;
code_t *p, *pe;
intptr_t lowest;
if (!st) return -1;
if (!st)
return -1;
__morph_begin();
lowest = MAX((intptr_t)__executable_start, (intptr_t)_ereal);
for (i = 0; i < st->count; ++i) {
if (st->symbols[i].x < 9) continue;
if (st->addr_base + st->symbols[i].x < lowest) continue;
if (st->addr_base + st->symbols[i].y >= (intptr_t)__privileged_start) break;
if (st->symbols[i].x < 9)
continue;
if (st->addr_base + st->symbols[i].x < lowest)
continue;
if (st->addr_base + st->symbols[i].y >= (intptr_t)__privileged_start)
break;
p = (code_t *)((char *)st->addr_base + st->symbols[i].x);
pe = (code_t *)((char *)st->addr_base + st->symbols[i].y);
if (pe - p < 2) continue;
if (pe - p < 2)
continue;
if (IsVirginFunction(p)) {
// kprintf("hooking %t\n", p);
HookFunction(p, dest);

View file

@ -21,12 +21,17 @@
static inline bool IsMemtrackedImpl(int x, int y) {
unsigned i;
i = __find_memory(&_mmi, x);
if (i == _mmi.i) return false;
if (x < _mmi.p[i].x) return false;
if (i == _mmi.i)
return false;
if (x < _mmi.p[i].x)
return false;
for (;;) {
if (y <= _mmi.p[i].y) return true;
if (++i == _mmi.i) return false;
if (_mmi.p[i].x != _mmi.p[i - 1].y + 1) return false;
if (y <= _mmi.p[i].y)
return true;
if (++i == _mmi.i)
return false;
if (_mmi.p[i].x != _mmi.p[i - 1].y + 1)
return false;
}
}

View file

@ -29,8 +29,10 @@
*/
char __is_stack_overflow(siginfo_t *si, void *arg) {
ucontext_t *uc = arg;
if (!si || !uc) return false;
if (si->si_signo != SIGSEGV && si->si_signo != SIGBUS) return false;
if (!si || !uc)
return false;
if (si->si_signo != SIGSEGV && si->si_signo != SIGBUS)
return false;
intptr_t sp = uc->uc_mcontext.SP;
intptr_t fp = (intptr_t)si->si_addr;
return ABS(fp - sp) < getauxval(AT_PAGESZ);

View file

@ -77,7 +77,8 @@ static inline pureconst unsigned long __rounddown2pow(unsigned long x) {
}
static wontreturn void __mmap_die(const char *s) {
if (_weaken(__die)) _weaken(__die)();
if (_weaken(__die))
_weaken(__die)();
STRACE("%s %m", s);
_Exit(199);
}
@ -89,9 +90,12 @@ static inline bool __overlaps_existing_mapping(char *p, size_t n) {
b = FRAME(p + (n - 1));
i = __find_memory(&_mmi, a);
if (i < _mmi.i) {
if (a <= _mmi.p[i].x && _mmi.p[i].x <= b) return true;
if (a <= _mmi.p[i].y && _mmi.p[i].y <= b) return true;
if (_mmi.p[i].x <= a && b <= _mmi.p[i].y) return true;
if (a <= _mmi.p[i].x && _mmi.p[i].x <= b)
return true;
if (a <= _mmi.p[i].y && _mmi.p[i].y <= b)
return true;
if (_mmi.p[i].x <= a && b <= _mmi.p[i].y)
return true;
}
return false;
}
@ -219,7 +223,8 @@ static textwindows dontinline void *__map_memories(char *addr, size_t size,
oi = fd == -1 ? 0 : off + m;
sz = size - m;
dm = sys_mmap(addr + m, sz, prot, f, fd, oi);
if (dm.addr == MAP_FAILED) return MAP_FAILED;
if (dm.addr == MAP_FAILED)
return MAP_FAILED;
iscow = (flags & MAP_TYPE) != MAP_SHARED && fd != -1;
readonlyfile = (flags & MAP_TYPE) == MAP_SHARED && fd != -1 &&
(g_fds.p[fd].flags & O_ACCMODE) == O_RDONLY;

View file

@ -43,7 +43,8 @@ __funline void __morph_mprotect(void *addr, size_t size, int prot, int ntprot) {
: "1"(__NR_mprotect), "D"(addr), "S"(size), "2"(prot)
: "rcx", "r8", "r9", "r10", "r11", "memory");
#ifndef NDEBUG
if (cf) ax = -ax;
if (cf)
ax = -ax;
if (ax == -EPERM) {
kprintf("error: need pledge(prot_exec) permission to code morph\n");
}

View file

@ -49,15 +49,23 @@ static struct SymbolTable *OpenSymbolTableImpl(const char *filename) {
const Elf64_Sym *symtab, *sym;
ptrdiff_t names_offset, name_base_offset, stp_offset;
map = MAP_FAILED;
if ((fd = open(filename, O_RDONLY | O_CLOEXEC)) == -1) return 0;
if ((filesize = lseek(fd, 0, SEEK_END)) == -1) goto SystemError;
if (filesize > INT_MAX) goto RaiseE2big;
if (filesize < 64) goto RaiseEnoexec;
if ((fd = open(filename, O_RDONLY | O_CLOEXEC)) == -1)
return 0;
if ((filesize = lseek(fd, 0, SEEK_END)) == -1)
goto SystemError;
if (filesize > INT_MAX)
goto RaiseE2big;
if (filesize < 64)
goto RaiseEnoexec;
elf = map = mmap(0, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
if (map == MAP_FAILED) goto SystemError;
if (READ32LE((char *)map) != READ32LE("\177ELF")) goto RaiseEnoexec;
if (!(name_base = GetStrtab(map, &m))) goto RaiseEnobufs;
if (!(symtab = GetSymtab(map, &n))) goto RaiseEnobufs;
if (map == MAP_FAILED)
goto SystemError;
if (READ32LE((char *)map) != READ32LE("\177ELF"))
goto RaiseEnoexec;
if (!(name_base = GetStrtab(map, &m)))
goto RaiseEnobufs;
if (!(symtab = GetSymtab(map, &n)))
goto RaiseEnobufs;
tsz = 0;
tsz += sizeof(struct SymbolTable);
tsz += sizeof(struct Symbol) * n;
@ -71,7 +79,8 @@ static struct SymbolTable *OpenSymbolTableImpl(const char *filename) {
tsz += sizeof(const Elf64_Sym *) * n;
tsz = ROUNDUP(tsz, FRAMESIZE);
t = mmap(0, tsz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (t == MAP_FAILED) goto SystemError;
if (t == MAP_FAILED)
goto SystemError;
t->magic = SYMBOLS_MAGIC;
t->abi = SYMBOLS_ABI;
t->size = size;
@ -90,8 +99,10 @@ static struct SymbolTable *OpenSymbolTableImpl(const char *filename) {
ELF64_ST_TYPE(sym->st_info) == STT_OBJECT))) {
continue;
}
if (sym->st_value > t->addr_end) continue;
if (sym->st_value < t->addr_base) continue;
if (sym->st_value > t->addr_end)
continue;
if (sym->st_value < t->addr_base)
continue;
x = sym->st_value - t->addr_base;
stp[m++] = (unsigned long)x << 32 | i;
}

View file

@ -44,7 +44,8 @@ void __warn_if_powersave(void) {
if ((fd = __sys_openat(AT_FDCWD, FILE, O_RDONLY, 0)) != -1) {
sys_read(fd, buf, 15);
sys_close(fd);
if (!startswith(buf, "powersave")) return;
if (!startswith(buf, "powersave"))
return;
sys_write(2, WARN, sizeof(WARN) - 1);
}
ALLOW_CANCELATION;

View file

@ -94,11 +94,15 @@ static abi char16_t *StrStr(const char16_t *haystack, const char16_t *needle) {
size_t i;
for (;;) {
for (i = 0;; ++i) {
if (!needle[i]) return (/*unconst*/ char16_t *)haystack;
if (!haystack[i]) break;
if (needle[i] != haystack[i]) break;
if (!needle[i])
return (/*unconst*/ char16_t *)haystack;
if (!haystack[i])
break;
if (needle[i] != haystack[i])
break;
}
if (!*haystack++) break;
if (!*haystack++)
break;
}
return 0;
}
@ -124,7 +128,8 @@ static abi bool32 WinFileExists(const char *path) {
uint16_t path16[PATH_MAX];
size_t z = ARRAYLEN(path16);
size_t n = tprecode8to16(path16, z, path).ax;
if (n >= z - 1) return false;
if (n >= z - 1)
return false;
return __imp_GetFileAttributesW(path16) != -1u;
}
@ -236,7 +241,8 @@ static abi wontreturn void WinInit(const char16_t *cmdline) {
// normalize executable path
if (wa->argv[0] && !WinFileExists(wa->argv[0])) {
unsigned i, n = 0;
while (wa->argv[0][n]) ++n;
while (wa->argv[0][n])
++n;
if (n + 4 < sizeof(wa->argv0buf)) {
for (i = 0; i < n; ++i) {
wa->argv0buf[i] = wa->argv[0][i];
@ -302,7 +308,8 @@ abi int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
cmdline = MyCommandLine();
#if SYSDEBUG
// sloppy flag-only check for early initialization
if (StrStr(cmdline, u"--strace")) ++__strace;
if (StrStr(cmdline, u"--strace"))
++__strace;
#endif
if (_weaken(WinSockInit)) {
_weaken(WinSockInit)();

View file

@ -28,7 +28,8 @@
static int __zipos_dupfd(int fd, int cmd, int start) {
int rc;
if (start < 0) return einval();
if (start < 0)
return einval();
if (IsWindows()) {
return sys_dup_nt(fd, -1, (cmd == F_DUPFD_CLOEXEC ? _O_CLOEXEC : 0),
start);

View file

@ -26,7 +26,8 @@
* @asyncsignalsafe
*/
int __zipos_fstat(struct ZiposHandle *h, struct stat *st) {
if (__zipos_stat_impl(h->zipos, h->cfile, st)) return -1;
if (__zipos_stat_impl(h->zipos, h->cfile, st))
return -1;
st->st_ino = __zipos_inode(h->zipos, h->cfile, h->data, h->size);
return 0;
}

View file

@ -54,14 +54,17 @@ static void __zipos_dismiss(uint8_t *map, const uint8_t *cdir, long pg) {
n = GetZipCdirRecords(cdir);
for (lo = c, hi = i = 0; i < n; ++i, c += ZIP_CFILE_HDRSIZE(map + c)) {
lf = GetZipCfileOffset(map + c);
if (lf < lo) lo = lf;
if (lf < lo)
lo = lf;
ef = lf + ZIP_LFILE_HDRSIZE(map + lf) + GetZipLfileCompressedSize(map + lf);
if (ef > hi) hi = ef;
if (ef > hi)
hi = ef;
}
// unmap the executable portion beneath the local files
mo = ROUNDDOWN(lo, FRAMESIZE);
if (mo) munmap(map, mo);
if (mo)
munmap(map, mo);
// this is supposed to reduce our rss usage but does it really?
lo = ROUNDDOWN(lo, pg);
@ -81,7 +84,8 @@ static int __zipos_compare_names(const void *a, const void *b, void *c) {
if (n) {
int res =
memcmp(ZIP_CFILE_NAME(z->map + *x), ZIP_CFILE_NAME(z->map + *y), n);
if (res) return res;
if (res)
return res;
}
return xn - yn; // xn and yn are 16-bit
}

View file

@ -27,7 +27,8 @@ uint64_t __zipos_inode(struct Zipos *zipos, int64_t cfile, //
const void *name, size_t namelen) {
unassert(cfile >= 0);
if (cfile == ZIPOS_SYNTHETIC_DIRECTORY) {
if (namelen && ((char *)name)[namelen - 1] == '/') --namelen;
if (namelen && ((char *)name)[namelen - 1] == '/')
--namelen;
cfile = INT64_MIN | __fnv(name, namelen);
}
return cfile;

View file

@ -39,8 +39,10 @@ size_t __zipos_normpath(char *d, const char *s, size_t n) {
s[1] == '.' && //
(!s[2] || s[2] == '/')) {
// matched "/../" or "^..$" or "^../" or "/..$"
while (p > d && p[-1] == '/') --p;
while (p > d && p[-1] != '/') --p;
while (p > d && p[-1] == '/')
--p;
while (p > d && p[-1] != '/')
--p;
} else {
*p++ = *s;
}

View file

@ -22,7 +22,8 @@
int __zipos_notat(int dirfd, const char *path) {
struct ZiposUri zipname;
if (!path) return efault();
if (!path)
return efault();
if (__isfdkind(dirfd, kFdZip) || __zipos_parseuri(path, &zipname) != -1) {
return einval();
}

View file

@ -76,7 +76,8 @@ static void *__zipos_mmap_space(size_t mapsize) {
offset = __zipos_maptotal;
__zipos_maptotal += mapsize;
start = (char *)kMemtrackZiposStart;
if (!__zipos_mapend) __zipos_mapend = start;
if (!__zipos_mapend)
__zipos_mapend = start;
__zipos_mapend = _extend(start, __zipos_maptotal, __zipos_mapend, MAP_PRIVATE,
kMemtrackZiposStart + kMemtrackZiposSize);
return start + offset;
@ -98,7 +99,8 @@ void __zipos_drop(struct ZiposHandle *h) {
h->mapsize - sizeof(struct ZiposHandle), kAsanHeapFree);
}
__zipos_lock();
do h->next = h->zipos->freelist;
do
h->next = h->zipos->freelist;
while (!_cmpxchg(&h->zipos->freelist, h->next, h));
__zipos_unlock();
}
@ -113,7 +115,8 @@ StartOver:
ph = &zipos->freelist;
while ((h = *ph)) {
if (h->mapsize >= mapsize) {
if (!_cmpxchg(ph, h, h->next)) goto StartOver;
if (!_cmpxchg(ph, h, h->next))
goto StartOver;
break;
}
ph = &h->next;
@ -168,8 +171,10 @@ static int __zipos_load(struct Zipos *zipos, size_t cf, int flags,
struct ZiposHandle *h;
if (cf == ZIPOS_SYNTHETIC_DIRECTORY) {
size = name->len;
if (!(h = __zipos_alloc(zipos, size + 1))) return -1;
if (size) memcpy(h->data, name->path, size);
if (!(h = __zipos_alloc(zipos, size + 1)))
return -1;
if (size)
memcpy(h->data, name->path, size);
h->data[size] = 0;
h->mem = h->data;
} else {
@ -178,11 +183,13 @@ static int __zipos_load(struct Zipos *zipos, size_t cf, int flags,
size = GetZipLfileUncompressedSize(zipos->map + lf);
switch (ZIP_LFILE_COMPRESSIONMETHOD(zipos->map + lf)) {
case kZipCompressionNone:
if (!(h = __zipos_alloc(zipos, 0))) return -1;
if (!(h = __zipos_alloc(zipos, 0)))
return -1;
h->mem = ZIP_LFILE_CONTENT(zipos->map + lf);
break;
case kZipCompressionDeflate:
if (!(h = __zipos_alloc(zipos, size))) return -1;
if (!(h = __zipos_alloc(zipos, size)))
return -1;
if (!__inflate(h->data, size, ZIP_LFILE_CONTENT(zipos->map + lf),
GetZipLfileCompressedSize(zipos->map + lf))) {
h->mem = h->data;

View file

@ -53,7 +53,8 @@ static ssize_t __zipos_read_impl(struct ZiposHandle *h, const struct iovec *iov,
}
for (i = 0; i < iovlen && y < h->size; ++i, y += b) {
b = MIN(iov[i].iov_len, h->size - y);
if (b) memcpy(iov[i].iov_base, h->mem + y, b);
if (b)
memcpy(iov[i].iov_base, h->mem + y, b);
}
if (opt_offset == -1) {
unassert(y != SIZE_MAX);

View file

@ -29,9 +29,12 @@
int __zipos_stat(struct ZiposUri *name, struct stat *st) {
ssize_t cf;
struct Zipos *zipos;
if (!(zipos = __zipos_get())) return enoexec();
if ((cf = __zipos_find(zipos, name)) == -1) return -1;
if (__zipos_stat_impl(zipos, cf, st)) return -1;
if (!(zipos = __zipos_get()))
return enoexec();
if ((cf = __zipos_find(zipos, name)) == -1)
return -1;
if (__zipos_stat_impl(zipos, cf, st))
return -1;
st->st_ino = __zipos_inode(zipos, cf, name->path, name->len);
return 0;
}