Fix bugs and make code tinier

- Fixed bug where stdio eof wasn't being sticky
- Fixed bug where fseeko() wasn't clearing eof state
- Removed assert() usage from libc favoring _unassert() / _npassert()
This commit is contained in:
Justine Tunney 2022-10-09 22:38:28 -07:00
parent 9b7c8db846
commit d5910e2673
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
115 changed files with 510 additions and 290 deletions

View file

@ -62,18 +62,13 @@ static wontreturn void __arena_die(void) {
_exit(83);
}
static wontreturn void __arena_not_implemented(void) {
assert(!"not implemented");
__arena_die();
}
forceinline void __arena_check(void) {
assert(__arena.depth);
_unassert(__arena.depth);
}
forceinline void __arena_check_pointer(void *p) {
assert(BASE + __arena.offset[__arena.depth - 1] <= (uintptr_t)p &&
(uintptr_t)p < BASE + __arena.offset[__arena.depth]);
_unassert(BASE + __arena.offset[__arena.depth - 1] <= (uintptr_t)p &&
(uintptr_t)p < BASE + __arena.offset[__arena.depth]);
}
forceinline bool __arena_is_arena_pointer(void *p) {
@ -326,7 +321,7 @@ void __arena_push(void) {
if (!__arena.depth) {
__arena_install();
} else {
assert(__arena.depth < ARRAYLEN(__arena.offset) - 1);
_unassert(__arena.depth < ARRAYLEN(__arena.offset) - 1);
}
__arena.offset[__arena.depth + 1] = __arena.offset[__arena.depth];
++__arena.depth;

View file

@ -25,9 +25,9 @@
#include "libc/str/str.h"
#include "libc/thread/tls.h"
static inline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame,
struct StackFrame *parent,
void *ptr) {
forceinline bool PointerNotOwnedByParentStackFrame(struct StackFrame *frame,
struct StackFrame *parent,
void *ptr) {
return !(((intptr_t)ptr > (intptr_t)frame) &&
((intptr_t)ptr < (intptr_t)parent));
}
@ -93,8 +93,8 @@ static void DeferFunction(struct StackFrame *frame, void *fn, void *arg) {
void __defer(void *rbp, void *fn, void *arg) {
struct StackFrame *f, *frame = rbp;
f = __builtin_frame_address(0);
assert(f->next == frame);
assert(PointerNotOwnedByParentStackFrame(f, frame, arg));
_unassert(f->next == frame);
_unassert(PointerNotOwnedByParentStackFrame(f, frame, arg));
DeferFunction(frame, fn, arg);
}

View file

@ -52,9 +52,9 @@ struct Tarjan {
static bool TarjanPush(struct Tarjan *t, int v) {
int *q;
assert(t->S.i >= 0);
assert(t->S.n >= 0);
assert(0 <= v && v < t->Vn);
_unassert(t->S.i >= 0);
_unassert(t->S.n >= 0);
_unassert(0 <= v && v < t->Vn);
if (t->S.i == t->S.n) {
if ((q = realloc(t->S.p, (t->S.n + (t->S.n >> 1) + 8) * sizeof(*t->S.p)))) {
t->S.p = q;
@ -67,13 +67,13 @@ static bool TarjanPush(struct Tarjan *t, int v) {
}
static int TarjanPop(struct Tarjan *t) {
assert(t->S.i > 0);
_unassert(t->S.i > 0);
return t->S.p[--t->S.i];
}
static bool TarjanConnect(struct Tarjan *t, int v) {
int fs, w, e;
assert(0 <= v && v < t->Vn);
_unassert(0 <= v && v < t->Vn);
t->V[v].index = t->index;
t->V[v].lowlink = t->index;
t->V[v].onstack = true;
@ -135,12 +135,12 @@ int _tarjan(int vertex_count, const int (*edges)[2], int edge_count,
int *out_opt_componentcount) {
int i, rc, v, e;
struct Tarjan *t;
assert(0 <= edge_count && edge_count <= INT_MAX);
assert(0 <= vertex_count && vertex_count <= INT_MAX);
_unassert(0 <= edge_count && edge_count <= INT_MAX);
_unassert(0 <= vertex_count && vertex_count <= INT_MAX);
for (i = 0; i < edge_count; ++i) {
if (i) assert(edges[i - 1][0] <= edges[i][0]);
assert(edges[i][0] < vertex_count);
assert(edges[i][1] < vertex_count);
if (i) _unassert(edges[i - 1][0] <= edges[i][0]);
_unassert(edges[i][0] < vertex_count);
_unassert(edges[i][1] < vertex_count);
}
if (!(t = calloc(1, (sizeof(struct Tarjan) +
sizeof(struct Vertex) * vertex_count)))) {
@ -175,7 +175,7 @@ int _tarjan(int vertex_count, const int (*edges)[2], int edge_count,
if (out_opt_components) {
*out_opt_componentcount = t->Ci;
}
assert(t->Ri == vertex_count);
_unassert(t->Ri == vertex_count);
free(t->S.p);
free(t);
return rc;

View file

@ -22,6 +22,6 @@
#include "libc/str/str.h"
dontdiscard void *unhexstr(const char *hexdigs) {
assert(strlen(hexdigs) % 2 == 0);
_unassert(strlen(hexdigs) % 2 == 0);
return unhexbuf(malloc(strlen(hexdigs) / 2), strlen(hexdigs) / 2, hexdigs);
}

View file

@ -43,7 +43,7 @@ int(vasprintf)(char **strp, const char *fmt, va_list va) {
if ((p2 = realloc(p, size))) {
p = p2;
wrote = (vsnprintf)(p, size, fmt, vb);
assert(wrote == size - 1);
_unassert(wrote == size - 1);
rc = wrote;
}
}