From d0489da611e0f247c392c095eacf9220eae162be Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 27 May 2020 20:53:04 +0200 Subject: [PATCH] fix realloc properly --- client/src/proxmark3.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index a461d4c95..98fd33723 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -436,30 +436,36 @@ static void set_my_user_directory(void) { // if not found, default to current directory if (my_user_directory == NULL) { - char *cwd_Buffer = NULL; uint16_t pathLen = FILENAME_MAX; // should be a good starting point bool error = false; + char *cwd_buffer = (char *)calloc(pathLen, sizeof(uint8_t)); - cwd_Buffer = (char *)calloc(pathLen, sizeof(uint8_t)); - - while (!error && (GetCurrentDir(cwd_Buffer, pathLen) == NULL)) { + while (!error && (GetCurrentDir(cwd_buffer, pathLen) == NULL)) { if (errno == ERANGE) { // Need bigger buffer pathLen += 10; // if buffer was too small add 10 characters and try again - cwd_Buffer = realloc(cwd_Buffer, pathLen); + char *tmp = realloc(cwd_buffer, pathLen); + if (tmp == NULL) { + PrintAndLogEx(WARNING, "failed to allocate memory"); + free(cwd_buffer); + return; + } + cwd_buffer = tmp; } else { - error = true; - free(cwd_Buffer); - cwd_Buffer = NULL; + free(cwd_buffer); + return; } - printf("Len... %d\n", pathLen); + PrintAndLogEx(NORMAL, "Len... %d", pathLen); } if (!error) { - for (int i = 0; i < strlen(cwd_Buffer); i++) - if (cwd_Buffer[i] == '\\') cwd_Buffer[i] = '/'; + for (int i = 0; i < strlen(cwd_buffer); i++) { + if (cwd_buffer[i] == '\\') { + cwd_buffer[i] = '/'; + } + } - my_user_directory = cwd_Buffer; + my_user_directory = cwd_buffer; } } }