From 8d5d3777c09c2d2e7810ad994543d3628e47993c Mon Sep 17 00:00:00 2001 From: wiidev Date: Thu, 4 Feb 2021 16:42:35 +0000 Subject: [PATCH] Handle an OOM error when downloading This change is mainly for WiiFlow, but it doesn't hurt to include it here too. --- source/network/https.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/source/network/https.c b/source/network/https.c index c4ce9e1d..36d8177d 100644 --- a/source/network/https.c +++ b/source/network/https.c @@ -172,6 +172,14 @@ bool read_chunked(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos #endif capacity *= 2; buffer->data = MEM2_realloc(buffer->data, capacity); + if (!buffer->data) // A custom theme is using too much memory + { +#ifdef DEBUG_NETWORK + gprintf("Out of memory!\n"); +#endif + errno = ENOMEM; + return false; + } } if ((ret = https_read(httpinfo, &buffer->data[start_pos], capacity - start_pos, false)) < 1) return false; @@ -213,6 +221,14 @@ bool read_all(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos) #endif capacity *= 2; buffer->data = MEM2_realloc(buffer->data, capacity); + if (!buffer->data) // A custom theme is using too much memory + { +#ifdef DEBUG_NETWORK + gprintf("Out of memory!\n"); +#endif + errno = ENOMEM; + return false; + } } if ((ret = https_read(httpinfo, &buffer->data[start_pos], capacity - start_pos, false)) == 0) break; @@ -360,7 +376,7 @@ void downloadfile(const char *url, struct download *buffer) } else return; - if (path == NULL) + if (!path) return; // Get the host int domainlength = path - url - 7 - httpinfo.use_https; @@ -447,7 +463,7 @@ void downloadfile(const char *url, struct download *buffer) return; } // Attempt to resume the session - if (session != NULL && wolfSSL_set_session(httpinfo.ssl, session) != SSL_SUCCESS) + if (session && wolfSSL_set_session(httpinfo.ssl, session) != SSL_SUCCESS) { #ifdef DEBUG_NETWORK gprintf("Failed to set session (session timed out?)\n"); @@ -471,7 +487,7 @@ void downloadfile(const char *url, struct download *buffer) usleep(10000); } // Check if we resumed successfully - if (session != NULL && !wolfSSL_session_reused(httpinfo.ssl)) + if (session && !wolfSSL_session_reused(httpinfo.ssl)) { #ifdef DEBUG_NETWORK gprintf("Failed to resume session\n");