From 4405f33884187cc2146098e9ee10875678d559eb Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 18 Mar 2025 14:24:59 +0100 Subject: [PATCH] Optionally return bytes received from `http_file_get()` `http_dl->bytes_received` was already free'd when it was accessed inside `aesgcm_file_get()`. Change `http_file_get()` to optionally return the number of bytes received so we know how much data we have to decrypt. This fixes #1994 Signed-off-by: Steffen Jaeckel --- src/tools/aesgcm_download.c | 10 ++++++++-- src/tools/http_download.c | 7 ++++++- src/tools/http_download.h | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/tools/aesgcm_download.c b/src/tools/aesgcm_download.c index 4e10c286..366a0be4 100644 --- a/src/tools/aesgcm_download.c +++ b/src/tools/aesgcm_download.c @@ -109,9 +109,15 @@ aesgcm_file_get(void* userdata) http_dl->filename = strdup(tmpname); http_dl->cmd_template = NULL; http_dl->silent = FALSE; + http_dl->return_bytes_received = TRUE; aesgcm_dl->http_dl = http_dl; - http_file_get(http_dl); // TODO(wstrm): Verify result. + ssize_t* p_bytes_received = http_file_get(http_dl); + if (!p_bytes_received) { + return NULL; + } + ssize_t bytes_received = *p_bytes_received; + free(p_bytes_received); auto_FILE FILE* tmpfh = fopen(tmpname, "rb"); if (tmpfh == NULL) { @@ -125,7 +131,7 @@ aesgcm_file_get(void* userdata) gcry_error_t crypt_res; crypt_res = omemo_decrypt_file(tmpfh, outfh, - http_dl->bytes_received, fragment); + bytes_received, fragment); remove(tmpname); diff --git a/src/tools/http_download.c b/src/tools/http_download.c index b9f47837..0753141b 100644 --- a/src/tools/http_download.c +++ b/src/tools/http_download.c @@ -105,6 +105,7 @@ void* http_file_get(void* userdata) { HTTPDownload* download = (HTTPDownload*)userdata; + ssize_t* ret = NULL; char* err = NULL; @@ -208,6 +209,10 @@ http_file_get(void* userdata) "Downloading '%s': done\nSaved to '%s'", download->url, download->filename); win_mark_received(download->window, download->id); + if (download->return_bytes_received) { + ret = malloc(sizeof(*ret)); + *ret = download->bytes_received; + } } } @@ -241,7 +246,7 @@ out: free(download->id); free(download); - return NULL; + return ret; } void diff --git a/src/tools/http_download.h b/src/tools/http_download.h index f75ba4d7..99a9fc06 100644 --- a/src/tools/http_download.h +++ b/src/tools/http_download.h @@ -58,6 +58,7 @@ typedef struct http_download_t pthread_t worker; int cancel; gboolean silent; + gboolean return_bytes_received; } HTTPDownload; void* http_file_get(void* userdata);