Skip to content

Commit b34a52a

Browse files
authored
Merge pull request #2026 from profanity-im/debug-1994
Debug & fix #1994
2 parents 6bdc557 + 6d3f20f commit b34a52a

File tree

8 files changed

+68
-51
lines changed

8 files changed

+68
-51
lines changed

src/command/cmd_funcs.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9435,17 +9435,16 @@ _url_aesgcm_method(ProfWin* window, const char* cmd_template, gchar* url, gchar*
94359435
auto_gchar gchar* filename = _prepare_filename(window, url, path);
94369436
if (!filename)
94379437
return;
9438-
auto_char char* id = get_random_string(4);
9439-
AESGCMDownload* download = malloc(sizeof(AESGCMDownload));
9440-
download->window = window;
9438+
AESGCMDownload* download = calloc(1, sizeof(AESGCMDownload));
9439+
download->id = get_random_string(4);
94419440
download->url = strdup(url);
94429441
download->filename = strdup(filename);
9443-
download->id = strdup(id);
94449442
if (cmd_template != NULL) {
94459443
download->cmd_template = strdup(cmd_template);
94469444
} else {
94479445
download->cmd_template = NULL;
94489446
}
9447+
download->window = window;
94499448

94509449
pthread_create(&(download->worker), NULL, &aesgcm_file_get, download);
94519450
aesgcm_download_add_download(download);
@@ -9458,38 +9457,38 @@ _download_install_plugin(ProfWin* window, gchar* url, gchar* path)
94589457
auto_gchar gchar* filename = _prepare_filename(window, url, path);
94599458
if (!filename)
94609459
return FALSE;
9461-
HTTPDownload* download = malloc(sizeof(HTTPDownload));
9462-
download->window = window;
9460+
HTTPDownload* download = calloc(1, sizeof(HTTPDownload));
9461+
download->id = get_random_string(4);
94639462
download->url = strdup(url);
94649463
download->filename = strdup(filename);
9465-
download->id = get_random_string(4);
94669464
download->cmd_template = NULL;
9465+
download->window = window;
9466+
download->silent = TRUE;
94679467

94689468
pthread_create(&(download->worker), NULL, &plugin_download_install, download);
94699469
plugin_download_add_download(download);
94709470
return TRUE;
94719471
}
94729472

9473-
void
9473+
static void
94749474
_url_http_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* path)
94759475
{
94769476
auto_gchar gchar* filename = _prepare_filename(window, url, path);
94779477
if (!filename)
94789478
return;
9479-
auto_char char* id = get_random_string(4);
9480-
HTTPDownload* download = malloc(sizeof(HTTPDownload));
9481-
download->window = window;
9479+
HTTPDownload* download = calloc(1, sizeof(HTTPDownload));
9480+
download->id = get_random_string(4);
94829481
download->url = strdup(url);
94839482
download->filename = strdup(filename);
9484-
download->id = strdup(id);
94859483
download->cmd_template = cmd_template ? strdup(cmd_template) : NULL;
9484+
download->window = window;
94869485
download->silent = FALSE;
94879486

94889487
pthread_create(&(download->worker), NULL, &http_file_get, download);
94899488
http_download_add_download(download);
94909489
}
94919490

9492-
void
9491+
static void
94939492
_url_external_method(const char* cmd_template, const char* url, gchar* filename)
94949493
{
94959494
auto_gcharv gchar** argv = format_call_external_argv(cmd_template, url, filename);

src/omemo/crypto.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,25 @@ int
4949
omemo_crypto_init(void)
5050
{
5151
if (!gcry_check_version(GCRYPT_VERSION)) {
52-
return -1;
52+
return GPG_ERR_UNKNOWN_VERSION;
5353
}
5454

55-
gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
55+
gcry_error_t ret;
56+
ret = gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
57+
if (ret != 0)
58+
return ret;
5659

57-
gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
60+
ret = gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
61+
if (ret != 0)
62+
return ret;
5863

59-
gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
64+
ret = gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
65+
if (ret != 0)
66+
return ret;
6067

61-
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
68+
ret = gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
69+
if (ret != 0)
70+
return ret;
6271

6372
/* Ask for a first random buffer to ensure CSPRNG is initialized.
6473
* Thus we control the memleak produced by gcrypt initialization and we can

src/omemo/omemo.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ omemo_init(void)
133133

134134
prof_add_shutdown_routine(_omemo_close);
135135

136-
if (omemo_crypto_init() != 0) {
137-
cons_show("Error initializing OMEMO crypto: gcry_check_version() failed");
136+
gcry_error_t crypt_res = omemo_crypto_init();
137+
if (crypt_res != 0) {
138+
cons_show("Error initializing OMEMO crypto: %s", gcry_strerror(crypt_res));
138139
}
139140

140141
pthread_mutexattr_init(&omemo_static_data.attr);

src/tools/aesgcm_download.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ aesgcm_file_get(void* userdata)
7777

7878
// Create a temporary file used for storing the ciphertext that is to be
7979
// retrieved from the https:// URL.
80-
auto_gchar char* tmpname = NULL;
80+
auto_gchar gchar* tmpname = NULL;
8181
auto_gfd gint tmpfd = 0;
8282
if ((tmpfd = g_file_open_tmp("profanity.XXXXXX", &tmpname, NULL)) == -1) {
8383
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
@@ -101,19 +101,25 @@ aesgcm_file_get(void* userdata)
101101

102102
// We wrap the HTTPDownload tool and use it for retrieving the ciphertext
103103
// and storing it in the temporary file previously opened.
104-
HTTPDownload* http_dl = malloc(sizeof(HTTPDownload));
104+
HTTPDownload* http_dl = calloc(1, sizeof(HTTPDownload));
105105
http_dl->window = aesgcm_dl->window;
106106
http_dl->worker = aesgcm_dl->worker;
107107
http_dl->id = strdup(aesgcm_dl->id);
108108
http_dl->url = strdup(https_url);
109109
http_dl->filename = strdup(tmpname);
110110
http_dl->cmd_template = NULL;
111111
http_dl->silent = FALSE;
112+
http_dl->return_bytes_received = TRUE;
112113
aesgcm_dl->http_dl = http_dl;
113114

114-
http_file_get(http_dl); // TODO(wstrm): Verify result.
115+
ssize_t* p_bytes_received = http_file_get(http_dl);
116+
if (!p_bytes_received) {
117+
return NULL;
118+
}
119+
ssize_t bytes_received = *p_bytes_received;
120+
free(p_bytes_received);
115121

116-
auto_FILE FILE* tmpfh = fopen(tmpname, "rb");
122+
FILE* tmpfh = fopen(tmpname, "rb");
117123
if (tmpfh == NULL) {
118124
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
119125
"Downloading '%s' failed: Unable to open "
@@ -125,10 +131,9 @@ aesgcm_file_get(void* userdata)
125131

126132
gcry_error_t crypt_res;
127133
crypt_res = omemo_decrypt_file(tmpfh, outfh,
128-
http_dl->bytes_received, fragment);
129-
134+
bytes_received, fragment);
135+
fclose(tmpfh);
130136
remove(tmpname);
131-
g_free(tmpname);
132137

133138
if (crypt_res != GPG_ERR_NO_ERROR) {
134139
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,

src/tools/http_download.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void*
105105
http_file_get(void* userdata)
106106
{
107107
HTTPDownload* download = (HTTPDownload*)userdata;
108+
ssize_t* ret = NULL;
108109

109110
char* err = NULL;
110111

@@ -208,6 +209,10 @@ http_file_get(void* userdata)
208209
"Downloading '%s': done\nSaved to '%s'",
209210
download->url, download->filename);
210211
win_mark_received(download->window, download->id);
212+
if (download->return_bytes_received) {
213+
ret = malloc(sizeof(*ret));
214+
*ret = download->bytes_received;
215+
}
211216
}
212217
}
213218

@@ -236,12 +241,12 @@ http_file_get(void* userdata)
236241
download_processes = g_slist_remove(download_processes, download);
237242
pthread_mutex_unlock(&lock);
238243

239-
free(download->id);
240-
free(download->url);
241244
free(download->filename);
245+
free(download->url);
246+
free(download->id);
242247
free(download);
243248

244-
return NULL;
249+
return ret;
245250
}
246251

247252
void

src/tools/http_download.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef struct http_download_t
5858
pthread_t worker;
5959
int cancel;
6060
gboolean silent;
61+
gboolean return_bytes_received;
6162
} HTTPDownload;
6263

6364
void* http_file_get(void* userdata);

src/tools/http_upload.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -298,39 +298,36 @@ http_file_put(void* userdata)
298298
pthread_mutex_lock(&lock);
299299

300300
if (err) {
301-
gchar* msg;
301+
auto_gchar gchar* err_msg = NULL;
302302
if (upload->cancel) {
303-
msg = g_strdup_printf("Uploading '%s' failed: Upload was canceled", upload->filename);
304-
if (!msg) {
305-
msg = g_strdup(FALLBACK_MSG);
303+
err_msg = g_strdup_printf("Uploading '%s' failed: Upload was canceled", upload->filename);
304+
if (!err_msg) {
305+
err_msg = g_strdup(FALLBACK_MSG);
306306
}
307307
} else {
308-
msg = g_strdup_printf("Uploading '%s' failed: %s", upload->filename, err);
309-
if (!msg) {
310-
msg = g_strdup(FALLBACK_MSG);
308+
err_msg = g_strdup_printf("Uploading '%s' failed: %s", upload->filename, err);
309+
if (!err_msg) {
310+
err_msg = g_strdup(FALLBACK_MSG);
311311
}
312-
win_update_entry_message(upload->window, upload->put_url, msg);
312+
win_update_entry_message(upload->window, upload->put_url, err_msg);
313313
}
314-
cons_show_error(msg);
315-
g_free(msg);
314+
cons_show_error(err_msg);
316315
} else {
317316
if (!upload->cancel) {
318-
msg = g_strdup_printf("Uploading '%s': 100%%", upload->filename);
319-
if (!msg) {
320-
msg = g_strdup(FALLBACK_MSG);
317+
auto_gchar gchar* status_msg = g_strdup_printf("Uploading '%s': 100%%", upload->filename);
318+
if (!status_msg) {
319+
status_msg = g_strdup(FALLBACK_MSG);
321320
}
322-
win_update_entry_message(upload->window, upload->put_url, msg);
321+
win_update_entry_message(upload->window, upload->put_url, status_msg);
323322
win_mark_received(upload->window, upload->put_url);
324-
g_free(msg);
325323

326324
char* url = NULL;
327325
if (format_alt_url(upload->get_url, upload->alt_scheme, upload->alt_fragment, &url) != 0) {
328-
gchar* msg = g_strdup_printf("Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url);
329-
if (!msg) {
330-
msg = g_strdup(FALLBACK_MSG);
326+
auto_gchar gchar* fail_msg = g_strdup_printf("Uploading '%s' failed: Bad URL ('%s')", upload->filename, upload->get_url);
327+
if (!fail_msg) {
328+
fail_msg = g_strdup(FALLBACK_MSG);
331329
}
332-
cons_show_error(msg);
333-
g_free(msg);
330+
cons_show_error(fail_msg);
334331
} else {
335332
switch (upload->window->type) {
336333
case WIN_CHAT:

src/tools/plugin_download.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ plugin_download_install(void* userdata)
6262
{
6363
HTTPDownload* plugin_dl = (HTTPDownload*)userdata;
6464

65+
/* We need to dup those, because they're free'd inside `http_file_get()` */
6566
auto_char char* path = strdup(plugin_dl->filename);
6667
auto_char char* https_url = strdup(plugin_dl->url);
67-
plugin_dl->silent = TRUE;
6868

6969
http_file_get(plugin_dl);
7070

0 commit comments

Comments
 (0)