Merge pull request #2026 from profanity-im/debug-1994

Debug & fix #1994
This commit is contained in:
Michael Vetter
2025-03-21 15:59:07 +01:00
committed by GitHub
8 changed files with 68 additions and 51 deletions

View File

@@ -9435,17 +9435,16 @@ _url_aesgcm_method(ProfWin* window, const char* cmd_template, gchar* url, gchar*
auto_gchar gchar* filename = _prepare_filename(window, url, path); auto_gchar gchar* filename = _prepare_filename(window, url, path);
if (!filename) if (!filename)
return; return;
auto_char char* id = get_random_string(4); AESGCMDownload* download = calloc(1, sizeof(AESGCMDownload));
AESGCMDownload* download = malloc(sizeof(AESGCMDownload)); download->id = get_random_string(4);
download->window = window;
download->url = strdup(url); download->url = strdup(url);
download->filename = strdup(filename); download->filename = strdup(filename);
download->id = strdup(id);
if (cmd_template != NULL) { if (cmd_template != NULL) {
download->cmd_template = strdup(cmd_template); download->cmd_template = strdup(cmd_template);
} else { } else {
download->cmd_template = NULL; download->cmd_template = NULL;
} }
download->window = window;
pthread_create(&(download->worker), NULL, &aesgcm_file_get, download); pthread_create(&(download->worker), NULL, &aesgcm_file_get, download);
aesgcm_download_add_download(download); aesgcm_download_add_download(download);
@@ -9458,38 +9457,38 @@ _download_install_plugin(ProfWin* window, gchar* url, gchar* path)
auto_gchar gchar* filename = _prepare_filename(window, url, path); auto_gchar gchar* filename = _prepare_filename(window, url, path);
if (!filename) if (!filename)
return FALSE; return FALSE;
HTTPDownload* download = malloc(sizeof(HTTPDownload)); HTTPDownload* download = calloc(1, sizeof(HTTPDownload));
download->window = window; download->id = get_random_string(4);
download->url = strdup(url); download->url = strdup(url);
download->filename = strdup(filename); download->filename = strdup(filename);
download->id = get_random_string(4);
download->cmd_template = NULL; download->cmd_template = NULL;
download->window = window;
download->silent = TRUE;
pthread_create(&(download->worker), NULL, &plugin_download_install, download); pthread_create(&(download->worker), NULL, &plugin_download_install, download);
plugin_download_add_download(download); plugin_download_add_download(download);
return TRUE; return TRUE;
} }
void static void
_url_http_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* path) _url_http_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* path)
{ {
auto_gchar gchar* filename = _prepare_filename(window, url, path); auto_gchar gchar* filename = _prepare_filename(window, url, path);
if (!filename) if (!filename)
return; return;
auto_char char* id = get_random_string(4); HTTPDownload* download = calloc(1, sizeof(HTTPDownload));
HTTPDownload* download = malloc(sizeof(HTTPDownload)); download->id = get_random_string(4);
download->window = window;
download->url = strdup(url); download->url = strdup(url);
download->filename = strdup(filename); download->filename = strdup(filename);
download->id = strdup(id);
download->cmd_template = cmd_template ? strdup(cmd_template) : NULL; download->cmd_template = cmd_template ? strdup(cmd_template) : NULL;
download->window = window;
download->silent = FALSE; download->silent = FALSE;
pthread_create(&(download->worker), NULL, &http_file_get, download); pthread_create(&(download->worker), NULL, &http_file_get, download);
http_download_add_download(download); http_download_add_download(download);
} }
void static void
_url_external_method(const char* cmd_template, const char* url, gchar* filename) _url_external_method(const char* cmd_template, const char* url, gchar* filename)
{ {
auto_gcharv gchar** argv = format_call_external_argv(cmd_template, url, filename); auto_gcharv gchar** argv = format_call_external_argv(cmd_template, url, filename);

View File

@@ -49,16 +49,25 @@ int
omemo_crypto_init(void) omemo_crypto_init(void)
{ {
if (!gcry_check_version(GCRYPT_VERSION)) { if (!gcry_check_version(GCRYPT_VERSION)) {
return -1; return GPG_ERR_UNKNOWN_VERSION;
} }
gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN); gcry_error_t ret;
ret = gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
if (ret != 0)
return ret;
gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0); ret = gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
if (ret != 0)
return ret;
gcry_control(GCRYCTL_RESUME_SECMEM_WARN); ret = gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
if (ret != 0)
return ret;
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); ret = gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
if (ret != 0)
return ret;
/* Ask for a first random buffer to ensure CSPRNG is initialized. /* Ask for a first random buffer to ensure CSPRNG is initialized.
* Thus we control the memleak produced by gcrypt initialization and we can * Thus we control the memleak produced by gcrypt initialization and we can

View File

@@ -133,8 +133,9 @@ omemo_init(void)
prof_add_shutdown_routine(_omemo_close); prof_add_shutdown_routine(_omemo_close);
if (omemo_crypto_init() != 0) { gcry_error_t crypt_res = omemo_crypto_init();
cons_show("Error initializing OMEMO crypto: gcry_check_version() failed"); if (crypt_res != 0) {
cons_show("Error initializing OMEMO crypto: %s", gcry_strerror(crypt_res));
} }
pthread_mutexattr_init(&omemo_static_data.attr); pthread_mutexattr_init(&omemo_static_data.attr);

View File

@@ -77,7 +77,7 @@ aesgcm_file_get(void* userdata)
// Create a temporary file used for storing the ciphertext that is to be // Create a temporary file used for storing the ciphertext that is to be
// retrieved from the https:// URL. // retrieved from the https:// URL.
auto_gchar char* tmpname = NULL; auto_gchar gchar* tmpname = NULL;
auto_gfd gint tmpfd = 0; auto_gfd gint tmpfd = 0;
if ((tmpfd = g_file_open_tmp("profanity.XXXXXX", &tmpname, NULL)) == -1) { if ((tmpfd = g_file_open_tmp("profanity.XXXXXX", &tmpname, NULL)) == -1) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
@@ -101,7 +101,7 @@ aesgcm_file_get(void* userdata)
// We wrap the HTTPDownload tool and use it for retrieving the ciphertext // We wrap the HTTPDownload tool and use it for retrieving the ciphertext
// and storing it in the temporary file previously opened. // and storing it in the temporary file previously opened.
HTTPDownload* http_dl = malloc(sizeof(HTTPDownload)); HTTPDownload* http_dl = calloc(1, sizeof(HTTPDownload));
http_dl->window = aesgcm_dl->window; http_dl->window = aesgcm_dl->window;
http_dl->worker = aesgcm_dl->worker; http_dl->worker = aesgcm_dl->worker;
http_dl->id = strdup(aesgcm_dl->id); http_dl->id = strdup(aesgcm_dl->id);
@@ -109,11 +109,17 @@ aesgcm_file_get(void* userdata)
http_dl->filename = strdup(tmpname); http_dl->filename = strdup(tmpname);
http_dl->cmd_template = NULL; http_dl->cmd_template = NULL;
http_dl->silent = FALSE; http_dl->silent = FALSE;
http_dl->return_bytes_received = TRUE;
aesgcm_dl->http_dl = http_dl; 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"); FILE* tmpfh = fopen(tmpname, "rb");
if (tmpfh == NULL) { if (tmpfh == NULL) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
"Downloading '%s' failed: Unable to open " "Downloading '%s' failed: Unable to open "
@@ -125,10 +131,9 @@ aesgcm_file_get(void* userdata)
gcry_error_t crypt_res; gcry_error_t crypt_res;
crypt_res = omemo_decrypt_file(tmpfh, outfh, crypt_res = omemo_decrypt_file(tmpfh, outfh,
http_dl->bytes_received, fragment); bytes_received, fragment);
fclose(tmpfh);
remove(tmpname); remove(tmpname);
g_free(tmpname);
if (crypt_res != GPG_ERR_NO_ERROR) { if (crypt_res != GPG_ERR_NO_ERROR) {
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id, http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,

View File

@@ -105,6 +105,7 @@ void*
http_file_get(void* userdata) http_file_get(void* userdata)
{ {
HTTPDownload* download = (HTTPDownload*)userdata; HTTPDownload* download = (HTTPDownload*)userdata;
ssize_t* ret = NULL;
char* err = NULL; char* err = NULL;
@@ -208,6 +209,10 @@ http_file_get(void* userdata)
"Downloading '%s': done\nSaved to '%s'", "Downloading '%s': done\nSaved to '%s'",
download->url, download->filename); download->url, download->filename);
win_mark_received(download->window, download->id); win_mark_received(download->window, download->id);
if (download->return_bytes_received) {
ret = malloc(sizeof(*ret));
*ret = download->bytes_received;
}
} }
} }
@@ -236,12 +241,12 @@ out:
download_processes = g_slist_remove(download_processes, download); download_processes = g_slist_remove(download_processes, download);
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
free(download->id);
free(download->url);
free(download->filename); free(download->filename);
free(download->url);
free(download->id);
free(download); free(download);
return NULL; return ret;
} }
void void

View File

@@ -58,6 +58,7 @@ typedef struct http_download_t
pthread_t worker; pthread_t worker;
int cancel; int cancel;
gboolean silent; gboolean silent;
gboolean return_bytes_received;
} HTTPDownload; } HTTPDownload;
void* http_file_get(void* userdata); void* http_file_get(void* userdata);

View File

@@ -298,39 +298,36 @@ http_file_put(void* userdata)
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
if (err) { if (err) {
gchar* msg; auto_gchar gchar* err_msg = NULL;
if (upload->cancel) { if (upload->cancel) {
msg = g_strdup_printf("Uploading '%s' failed: Upload was canceled", upload->filename); err_msg = g_strdup_printf("Uploading '%s' failed: Upload was canceled", upload->filename);
if (!msg) { if (!err_msg) {
msg = g_strdup(FALLBACK_MSG); err_msg = g_strdup(FALLBACK_MSG);
} }
} else { } else {
msg = g_strdup_printf("Uploading '%s' failed: %s", upload->filename, err); err_msg = g_strdup_printf("Uploading '%s' failed: %s", upload->filename, err);
if (!msg) { if (!err_msg) {
msg = g_strdup(FALLBACK_MSG); err_msg = g_strdup(FALLBACK_MSG);
} }
win_update_entry_message(upload->window, upload->put_url, msg); win_update_entry_message(upload->window, upload->put_url, err_msg);
} }
cons_show_error(msg); cons_show_error(err_msg);
g_free(msg);
} else { } else {
if (!upload->cancel) { if (!upload->cancel) {
msg = g_strdup_printf("Uploading '%s': 100%%", upload->filename); auto_gchar gchar* status_msg = g_strdup_printf("Uploading '%s': 100%%", upload->filename);
if (!msg) { if (!status_msg) {
msg = g_strdup(FALLBACK_MSG); status_msg = g_strdup(FALLBACK_MSG);
} }
win_update_entry_message(upload->window, upload->put_url, msg); win_update_entry_message(upload->window, upload->put_url, status_msg);
win_mark_received(upload->window, upload->put_url); win_mark_received(upload->window, upload->put_url);
g_free(msg);
char* url = NULL; char* url = NULL;
if (format_alt_url(upload->get_url, upload->alt_scheme, upload->alt_fragment, &url) != 0) { if (format_alt_url(upload->get_url, upload->alt_scheme, upload->alt_fragment, &url) != 0) {
gchar* msg = g_strdup_printf("Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url); auto_gchar gchar* fail_msg = g_strdup_printf("Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url);
if (!msg) { if (!fail_msg) {
msg = g_strdup(FALLBACK_MSG); fail_msg = g_strdup(FALLBACK_MSG);
} }
cons_show_error(msg); cons_show_error(fail_msg);
g_free(msg);
} else { } else {
switch (upload->window->type) { switch (upload->window->type) {
case WIN_CHAT: case WIN_CHAT:

View File

@@ -62,9 +62,9 @@ plugin_download_install(void* userdata)
{ {
HTTPDownload* plugin_dl = (HTTPDownload*)userdata; HTTPDownload* plugin_dl = (HTTPDownload*)userdata;
/* We need to dup those, because they're free'd inside `http_file_get()` */
auto_char char* path = strdup(plugin_dl->filename); auto_char char* path = strdup(plugin_dl->filename);
auto_char char* https_url = strdup(plugin_dl->url); auto_char char* https_url = strdup(plugin_dl->url);
plugin_dl->silent = TRUE;
http_file_get(plugin_dl); http_file_get(plugin_dl);