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
1 changed files with 19 additions and 3 deletions

View File

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