From 13f8225fed47f3451bc8d601657cdfdf1c3379a6 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 21 Oct 2025 22:57:46 -0500 Subject: Touch up code in shared.c --- src/shared.c | 132 ++++++++++++++++++++++------------------------------------- 1 file changed, 49 insertions(+), 83 deletions(-) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index 0a2121b..9a147eb 100644 --- a/src/shared.c +++ b/src/shared.c @@ -19,18 +19,17 @@ return (retval);\ } while (0) -/// Defines how `x___alloc()` functions should exit. `___VXGG___XALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions -/// registered with `atexit()` and `on_exit()`. `___VXGG___XALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `x___alloc()` -/// type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type -#define ___VXGG___XALLOC_EXIT_ON_ERROR___ 1 +/// Determines how `vx__alloc()` functions exit. `> 0` calls `error()`, otherwise calls `abort()`. `vx__alloc()` type functions will +/// always halt execution +#define ___VXGG___VXALLOC_EXIT_ON_ERROR___ 1 -/// Defines whether vxgg functions that can error print out a short warning of the error when one is encountered. -/// `___VXGG___VERBOSE_ERRORS___ > 0` will print diagnostic error messages, and will do nothing otherwise +/// Determines whether vxgg functions that can error print out a short warning of the error when one is encountered. +/// `> 0` will print diagnostic error messages, and will do nothing otherwise #define ___VXGG___VERBOSE_ERRORS___ 1 //! Macro to exit on an alloc error instead of doing the terrible nested if statement that was being used previously #define XALLOC_EXIT(msg, ...) do {\ - if(!___VXGG___XALLOC_EXIT_ON_ERROR___)\ + if(!___VXGG___VXALLOC_EXIT_ON_ERROR___)\ abort();\ if(!___VXGG___VERBOSE_ERRORS___)\ exit(EXIT_FAILURE);\ @@ -38,9 +37,6 @@ exit(EXIT_FAILURE); /* Makes gcc happy */\ } while (0) -//! Holdover macro because I'm lazy. Used to call either malloc or xmalloc, but the xalloc functions were a bad idea, so I removed them -#define VALLOC(nmemb, size) malloc((nmemb) * (size)) - //! Error macro that gcc will not complain about #define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0) //! Spit out a warning using `error` @@ -60,6 +56,7 @@ typedef struct cl { int size; //!< The size of each array int used; //!< The current number of used elements in each array } cleanup; +// While the cleanup thing is useful, it's also a complete fucking mess. Swtich to error gotos asap #include #include @@ -83,16 +80,15 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { // Bytes read == 0, return 0 // Bytes read < 0, free string, return -1; // When string hits capacity, double the capacity, and reallocate the string - if(!str || initsize < 1) ERRRET(EINVAL, -1); - const int ECODE = -100; - char *lstr = NULL, *tmp = NULL; + if(!str || initsize < 1 || fd < 0) ERRRET(EINVAL, -1); + + char *lstr = NULL, *tmp = NULL; ssize_t bytesread = -1; - int csize = initsize, ccap = initsize; + int csize = initsize, ccap = initsize, eflag = 0; lstr = calloc(initsize, sizeof(char)); - if(!lstr) - return -1; + if(!lstr) return -1; while((bytesread = read(fd, lstr + (csize - ccap), ccap)) > 0) { ccap -= bytesread; @@ -101,35 +97,15 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { ccap = csize / 2; tmp = realloc(lstr, csize * sizeof(char)); - if(!tmp) { - if(___VXGG___VERBOSE_ERRORS___) - error(0, errno, "Could not reallocate enough space for lstr"); - - free(lstr); - lstr = NULL; // Need to set this because of the break - bytesread = ECODE; - break; - } + if(!tmp) {eflag = 1; goto ERR_rwbuf;} lstr = tmp; } } - if(bytesread < 0 && bytesread != ECODE) { - if(___VXGG___VERBOSE_ERRORS___) - error(0, errno, "Ran into a read() error"); - - free(lstr); - lstr = NULL; - } + if(bytesread < 0) {eflag = 2; goto ERR_rwbuf;} if(lstr) { tmp = realloc(lstr, csize - ccap + 1); - if(!tmp) { - if(___VXGG___VERBOSE_ERRORS___) - error(0, errno, "Could not shrink lstr after reading buffer"); - - free(lstr); - bytesread = ECODE; - } + if(!tmp) {eflag = 3; goto ERR_rwbuf;} lstr = tmp; } @@ -138,7 +114,20 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { } *str = lstr; - return ((bytesread == 0) ? (csize - ccap) : -1); + return csize - ccap; + +ERR_rwbuf: + if(___VXGG___VERBOSE_ERRORS___) { + switch (eflag) { + case 1: WARN(errno, "Could not reallocate enough space for lstr",); + case 2: WARN(errno, "Ran into a read() error",); + case 3: WARN(errno, "Could not shrink lstr after reading buffer",); + default: WARN(errno, "Ran into some error",); + } + } + free(lstr); + + return -1; } @@ -152,7 +141,7 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { */ int wwbuf(int fd, const unsigned char *buf, int len) { - if(!buf || len <= 0) ERRRET(EINVAL, -1); + if(!buf || len <= 0 || fd < 0) ERRRET(EINVAL, -1); int total = 0; int left = len; @@ -182,28 +171,18 @@ char * vxdirname(const char * const path) { char *tmp = NULL; if(!path) { // Path being null is a special case which should return early, before anything else (as to avoid null dereference) tmp = strdup("."); - if(!tmp) { - WARN(errno, " could not strdup \".\" for set path result \"NULL\"", ); - return NULL; - } - - return tmp; - } - - unsigned char flag = 0; - if(strcmp(path, ".") == 0) {tmp = strdup("."); flag++;} - if(strcmp(path, "/") == 0 && !flag) {tmp = strdup("/"); flag++;} - if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} - - if(flag) { - if(!tmp) { - WARN(errno, " could not strdup a set path result", ); - return NULL; - } - + if(!tmp && ___VXGG___VERBOSE_ERRORS___) WARN(errno, " could not strdup \".\" for set path result \"NULL\"", ); return tmp; } + const char * const special[] = {"..", ".", "/"}; + for(int i = 0; i < STATIC_ARRAY_LEN(special); i++) { + if(strncmp(path, special[i], strlen(special[i])) == 0) { + tmp = strdup(special[i]); + if(!tmp && ___VXGG___VERBOSE_ERRORS___) WARN(errno, " could not strdup a set path result", ); + return tmp; + } + } /* From the manpages: (man 3 dirname) // +=======================================+ @@ -218,7 +197,7 @@ char * vxdirname(const char * const path) { // Get a temp copy of the path for manipulation purposes tmp = strdup(path); if(!tmp) { - WARN(errno, " could not strdup the given path \"%s\" for internal manipulation", , path); + if(___VXGG___VERBOSE_ERRORS___) WARN(errno, " could not strdup the given path \"%s\" for internal manipulation", , path); return NULL; } @@ -239,24 +218,14 @@ char * vxdirname(const char * const path) { count++; } - if(count == 0 || count == 1) - free(tmp); - if(count == 0) { - tmp = strdup("."); - if(!tmp) { - WARN(errno, " could not strdup \".\" for set path result", ); - return NULL; - } - return tmp; - } else if(count == 1) { - tmp = strdup("/"); - if(!tmp) { - WARN(errno, " could not strdup \"/\" for set path result", ); - return NULL; - } - return tmp; - } - // This is retarded, fix it + if(count < 2) { + free(tmp); + if(count == 1) tmp = strdup("/"); + else tmp = strdup("."); + + if(!tmp && ___VXGG___VERBOSE_ERRORS___) WARN(errno, " Error: Could not strdup \"%s\" for set path result", , ((count == 0) ? "." : "/")); + return tmp; + } for(size_t i = 0, c2 = 0; i < pathlen; i++) { if(tmp[i] == '/') @@ -267,10 +236,7 @@ char * vxdirname(const char * const path) { char * const actual = strdup(tmp); free(tmp); - if(!actual) { - WARN(errno, " could not strdup tmp string to make a shorter end string", ); - return NULL; - } + if(!actual && ___VXGG___VERBOSE_ERRORS___) WARN(errno, " could not strdup tmp string to make a shorter end string", ); return actual; } -- cgit v1.2.3