Handle an OOM error when downloading

This change is mainly for WiiFlow, but it doesn't hurt to include it here too.
This commit is contained in:
wiidev 2021-02-04 16:42:35 +00:00
parent 48956ad8a4
commit 8d5d3777c0

View File

@ -172,6 +172,14 @@ bool read_chunked(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos
#endif #endif
capacity *= 2; capacity *= 2;
buffer->data = MEM2_realloc(buffer->data, capacity); 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) if ((ret = https_read(httpinfo, &buffer->data[start_pos], capacity - start_pos, false)) < 1)
return false; return false;
@ -213,6 +221,14 @@ bool read_all(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos)
#endif #endif
capacity *= 2; capacity *= 2;
buffer->data = MEM2_realloc(buffer->data, capacity); 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) if ((ret = https_read(httpinfo, &buffer->data[start_pos], capacity - start_pos, false)) == 0)
break; break;
@ -360,7 +376,7 @@ void downloadfile(const char *url, struct download *buffer)
} }
else else
return; return;
if (path == NULL) if (!path)
return; return;
// Get the host // Get the host
int domainlength = path - url - 7 - httpinfo.use_https; int domainlength = path - url - 7 - httpinfo.use_https;
@ -447,7 +463,7 @@ void downloadfile(const char *url, struct download *buffer)
return; return;
} }
// Attempt to resume the session // 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 #ifdef DEBUG_NETWORK
gprintf("Failed to set session (session timed out?)\n"); gprintf("Failed to set session (session timed out?)\n");
@ -471,7 +487,7 @@ void downloadfile(const char *url, struct download *buffer)
usleep(10000); usleep(10000);
} }
// Check if we resumed successfully // Check if we resumed successfully
if (session != NULL && !wolfSSL_session_reused(httpinfo.ssl)) if (session && !wolfSSL_session_reused(httpinfo.ssl))
{ {
#ifdef DEBUG_NETWORK #ifdef DEBUG_NETWORK
gprintf("Failed to resume session\n"); gprintf("Failed to resume session\n");