From 330e99381e700b6172a921513354f806b78ff13c Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Fri, 25 Oct 2024 18:26:59 +0200 Subject: [PATCH] Fix memory leak in base62_encode() --- src/misc.c | 3 +-- src/misc.h | 2 +- src/tty.c | 3 ++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/misc.c b/src/misc.c index 50fb339..bd0a429 100644 --- a/src/misc.c +++ b/src/misc.c @@ -121,10 +121,9 @@ unsigned long djb2_hash(const unsigned char *str) } // Function to encode a number to base62 -char *base62_encode(unsigned long num) +void *base62_encode(unsigned long num, char *output) { const char base62_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - char *output = (char *) malloc(5); // 4 characters + null terminator if (output == NULL) { tio_error_print("Memory allocation failed"); diff --git a/src/misc.h b/src/misc.h index 8ba8285..08ae5eb 100644 --- a/src/misc.h +++ b/src/misc.h @@ -30,7 +30,7 @@ void delay(long ms); int ctrl_key_code(unsigned char key); bool regex_match(const char *string, const char *pattern); unsigned long djb2_hash(const unsigned char *str); -char *base62_encode(unsigned long num); +void *base62_encode(unsigned long num, char *output); int read_poll(int fd, void *data, size_t len, int timeout); double get_current_time(void); bool match_patterns(const char *string, const char *patterns); diff --git a/src/tty.c b/src/tty.c index 7dbd286..c4785b9 100644 --- a/src/tty.c +++ b/src/tty.c @@ -1735,7 +1735,8 @@ GList *tty_search_for_serial_devices(void) // Hash remaining string to get unique topology ID unsigned long hash = djb2_hash((const unsigned char *)devices_path); - char *tid = base62_encode(hash); + char tid[5]; + base62_encode(hash, tid); free(devices_path); // Construct the path to the device's driver symlink