error: fix memory leaks and other bugs

It was really late when i wrote this okay
This commit is contained in:
anna 2021-07-15 22:50:33 +02:00
parent 630530109b
commit c71e36e7c8
Signed by: fef
GPG key ID: EC22E476DC2D3D84

View file

@ -16,22 +16,22 @@
void yeet(error *err, u32 number, const char *restrict fmt, ...) void yeet(error *err, u32 number, const char *restrict fmt, ...)
{ {
va_list vargs; va_list vargs;
usize msg_capacity = 64; usize msg_size = 64;
char *msg = nil; char *msg = nil;
int vsnprintf_ret;
if (fmt != nil) { if (fmt != nil) {
do { do {
msg = nalloc(msg_capacity, nil); msg = nalloc(msg_size, nil);
va_start(vargs, fmt); va_start(vargs, fmt);
vsnprintf_ret = vsnprintf(msg, msg_capacity, fmt, vargs); int required_size = vsnprintf(msg, msg_size, fmt, vargs);
va_end(vargs); va_end(vargs);
if (vsnprintf_ret > msg_capacity) { /* required_size excludes NUL, therefore >= */
msg_capacity = vsnprintf_ret; if (required_size >= msg_size) {
msg_size = required_size + 1;
nfree(msg); nfree(msg);
msg = nil; msg = nil;
} else if (vsnprintf_ret < 0) { } else if (required_size < 0) {
write(1, "Runtime error\n", 14); write(2, "Runtime error\n", strlen("Runtime error\n"));
exit(1); exit(1);
} }
} while (msg == nil); } while (msg == nil);
@ -46,6 +46,7 @@ void yeet(error *err, u32 number, const char *restrict fmt, ...)
err->_number = number; err->_number = number;
err->_message = nstr(msg, nil); err->_message = nstr(msg, nil);
nfree(msg);
} }
void neat(error *err) void neat(error *err)