Fix memory leak in base62_encode()

This commit is contained in:
Martin Lund 2024-10-25 18:26:59 +02:00
parent 7e314b2cc3
commit 330e99381e
3 changed files with 4 additions and 4 deletions

View file

@ -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");

View file

@ -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);

View file

@ -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