From 8cb74a7817bc5769a28a12cabb90276947f8051a Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sun, 20 Dec 2020 22:29:25 +0100 Subject: add some memory allocation failure checks --- src/net.c | 7 ++++++- src/wine.c | 31 +++++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/net.c b/src/net.c index c20f076..a90533b 100644 --- a/src/net.c +++ b/src/net.c @@ -18,7 +18,7 @@ static size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* us uint8_t* ptr = realloc(mem->memory, mem->size + realsize + 1); if(ptr == NULL) { - puts("out of memory"); + fprintf(stderr, "Unable to allocate memory\n"); return 0; } @@ -55,6 +55,7 @@ struct MemoryStruct* downloadToRam(const char* URL, long noprogress) if (chunk) { + // if we managed to allocate the chunk lets assume we can allocate at least 1 byte chunk->memory = malloc(1); chunk->size = 0; @@ -90,6 +91,10 @@ struct MemoryStruct* downloadToRam(const char* URL, long noprogress) curl_easy_cleanup(curl_handle); curl_global_cleanup(); } + else + { + fprintf(stderr, "Unable to allocate memory\n"); + } return chunk; } diff --git a/src/wine.c b/src/wine.c index 46bfd46..c8680d0 100644 --- a/src/wine.c +++ b/src/wine.c @@ -385,22 +385,29 @@ enum wine_type_t check_wine_ver(char* winepath, size_t size) char* winepathcopy = NULL; winepathcopy = malloc(size); - strncpy(winepathcopy, winepath, size); - strncat(winepathcopy, WINEBIN, size - strlen(winepathcopy)); - - if (isFile(winepathcopy)) + if (winepathcopy) { - free(winepathcopy); - return WINE_NORMAL; - } + strncpy(winepathcopy, winepath, size); + strncat(winepathcopy, WINEBIN, size - strlen(winepathcopy)); + + if (isFile(winepathcopy)) + { + free(winepathcopy); + return WINE_NORMAL; + } - strncpy(winepathcopy, winepath, size); - strncat(winepathcopy, PROTONBIN, size - strlen(winepathcopy)); + strncpy(winepathcopy, winepath, size); + strncat(winepathcopy, PROTONBIN, size - strlen(winepathcopy)); - if (isFile(winepathcopy)) + if (isFile(winepathcopy)) + { + free(winepathcopy); + return WINE_PROTON; + } + } + else { - free(winepathcopy); - return WINE_PROTON; + fprintf(stderr, "Unable to allocate memory\n"); } return WINE_NONE; -- cgit v1.2.3