fix: address mem issues

This commit is contained in:
javalsai 2025-06-06 18:12:45 +02:00
parent 82e92df95c
commit dc6424979e
Signed by: javalsai
SSH Key Fingerprint: SHA256:3G83yKhBUWVABVX/vPWH88xnK4+ptMtHkZGCRXD4Mk8
3 changed files with 45 additions and 24 deletions

View File

@ -4,6 +4,14 @@
#include "config.h"
#include "util.h"
// Alr so ima explain the bitfield returned by `cb` a bit
// 4 bits:
// 0b0001: break out of parsing (returning true)
// 0b0010: free the value
// 0b0100: free the key
// 0b1000: break out of parsing (returning false)
//
// This would return true if everything goes fine, false otherwise (malloc error, broke parsing, etc)
bool line_parser(FILE *fd, ssize_t *blksize,
u_char (*cb)(char *key, char *value)) {
size_t opt_size = 4096;
@ -41,19 +49,20 @@ bool line_parser(FILE *fd, ssize_t *blksize,
if (ret & 0b1000) {
free(buf);
return false;
}
if (ret & 0b0001) {
free(buf);
}
if (ret & 0b0001) {
free(buf);
break;
}
}
}
free(buf);
free(buf);
}
return true;
}
struct config *__config;
// Yanderedev code (wanna fix this with a table or smth)
u_char config_line_handler(char *k, char *v) {
if (strcmp(k, "colors.bg") == 0)
__config->theme.colors.bg = v;
@ -98,16 +107,13 @@ u_char config_line_handler(char *k, char *v) {
else if (strcmp(k, "functions.poweroff") == 0) {
__config->functions.poweroff = find_keyname(v);
return 0b0110;
}
else if (strcmp(k, "functions.reboot") == 0) {
} else if (strcmp(k, "functions.reboot") == 0) {
__config->functions.reboot = find_keyname(v);
return 0b0110;
}
else if (strcmp(k, "functions.refresh") == 0) {
} else if (strcmp(k, "functions.refresh") == 0) {
__config->functions.refresh = find_keyname(v);
return 0b0110;
}
else if (strcmp(k, "strings.f_poweroff") == 0)
} else if (strcmp(k, "strings.f_poweroff") == 0)
__config->strings.f_poweroff = v;
else if (strcmp(k, "strings.f_reboot") == 0)
__config->strings.f_reboot = v;
@ -126,15 +132,14 @@ u_char config_line_handler(char *k, char *v) {
else if (strcmp(k, "behavior.include_defshell") == 0) {
__config->behavior.include_defshell = strcmp(v, "true") == 0;
return 0b0110;
}
else if (strcmp(k, "behavior.source") == 0)
} else if (strcmp(k, "behavior.source") == 0)
vec_push(&__config->behavior.source, v);
else if (strcmp(k, "behavior.user_source") == 0)
vec_push(&__config->behavior.user_source, v);
else
return 0b1111;
return 0b100;
return 0b0100;
}
struct config *parse_config(char *path) {

View File

@ -54,6 +54,7 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
char *exec_buf = NULL;
char *tryexec_buf = NULL;
// This should be made a specific function
// Emm, if anything goes wrong just free the inner loop and `break;` fd and the rest is handled after
while (true) {
char *buf = malloc(sb->st_blksize);
ssize_t read_size = getline(&buf, &alloc_size, fd);
@ -64,7 +65,19 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
uint read;
char *key = malloc(read_size + sizeof(char));
if(key == NULL) {
free(buf);
// TODO: more sophisticated error handling??
break;
}
char *value = malloc(read_size + sizeof(char));
if(value == NULL) {
free(buf);
free(key);
// TODO: more sophisticated error handling??
break;
}
value[0] = '\0'; // I'm not sure if sscanf would null this string out
if ((read = sscanf(buf, "%[^=]=%[^\n]\n", key, value)) != 0) {
if (strcmp(key, "Name") == 0) {
found &= 0b001;
@ -78,13 +91,17 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) {
} else {
free(value);
}
}
} else {
free(value);
}
free(key);
free(buf);
free(buf);
if (found == 0b111) break;
}
/*printf("\nend parsing...\n");*/
// Generic handling of exit
fclose(fd);
// just add this to the list

View File

@ -68,7 +68,6 @@ static int selret_magic() {
return select(1, &set, NULL, NULL, &timeout);
}
// Vector shii
struct Vector vec_new() {
struct Vector vec;
@ -76,7 +75,7 @@ struct Vector vec_new() {
return vec;
}
int vec_push(struct Vector* vec, void* item) {
int vec_push(struct Vector *vec, void *item) {
if (vec->length >= vec->alloc_len) {
uint32_t new_size = vec->alloc_len + vec->alloc_size;
void **new_location = realloc(vec->pages, vec->alloc_size);
@ -93,33 +92,33 @@ int vec_push(struct Vector* vec, void* item) {
return 0;
}
void vec_free(struct Vector* vec) {
while(vec->length > 0)
void vec_free(struct Vector *vec) {
while (vec->length > 0)
free(vec->pages[--vec->length]);
vec_clear(vec);
}
void vec_clear(struct Vector* vec) {
void vec_clear(struct Vector *vec) {
free(vec->pages);
vec_reset(vec);
}
void vec_reset(struct Vector* vec) {
void vec_reset(struct Vector *vec) {
vec->length = 0;
vec->alloc_len = 0;
vec->alloc_size = 4096; // 4KiB page size?
vec->pages = NULL;
}
void* vec_pop(struct Vector* vec) {
void *vec_pop(struct Vector *vec) {
if (vec->length == 0)
return NULL;
return vec->pages[--vec->length];
}
void* vec_get(struct Vector* vec, size_t index) {
void *vec_get(struct Vector *vec, size_t index) {
if (index >= vec->length)
return NULL;