Skip to content

Commit 5b9516b

Browse files
authored
Merge pull request #2022 from profanity-im/fixes
Memory fixes
2 parents 0da4706 + bf24d79 commit 5b9516b

File tree

7 files changed

+49
-26
lines changed

7 files changed

+49
-26
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ jobs:
3535
# if this check fails, you have to update the number of auto types known and the list of auto types in the check below
3636
- name: Check auto types are up-to-date
3737
run: |
38-
[[ "$(find src -type f -name '*.[ch]' -exec awk '/^#define auto_[\W]*/ {print $2}' '{}' \; | sort -u | wc -l)" == "6" ]] || exit -1
38+
[[ "$(find src -type f -name '*.[ch]' -exec awk '/^#define auto_[\W]*/ {print $2}' '{}' \; | sort -u | wc -l)" == "8" ]] || exit -1
3939
- name: Check auto types are initialized
4040
run: |
41-
grep -P 'auto_(char|gchar|gcharv|guchar|jid|sqlite)[\w *]*;$' -r src && exit -1 || true
41+
grep -P 'auto_(char|gchar|gcharv|guchar|jid|sqlite|gfd|FILE)[\w *]*;$' -r src && exit -1 || true
4242
- name: Run clang-format
4343
uses: jidicula/clang-format-action@v4.11.0
4444
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ compile_commands.json
1717
.project
1818
.settings/
1919
.vscode/
20+
*.plist/
2021

2122
# autotools
2223
.libs/

src/command/cmd_funcs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4917,8 +4917,6 @@ cmd_sendfile(ProfWin* window, const char* const command, gchar** args)
49174917
goto out;
49184918
}
49194919

4920-
FILE* fh = fdopen(fd, "rb");
4921-
49224920
gboolean omemo_enabled = FALSE;
49234921
gboolean sendfile_enabled = TRUE;
49244922

@@ -4952,6 +4950,8 @@ cmd_sendfile(ProfWin* window, const char* const command, gchar** args)
49524950
goto out;
49534951
}
49544952

4953+
FILE* fh = fdopen(fd, "rb");
4954+
49554955
if (omemo_enabled) {
49564956
#ifdef HAVE_OMEMO
49574957
char* err = NULL;

src/common.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,36 @@ auto_free_char(char** str)
130130
free(*str);
131131
}
132132

133+
/**
134+
* Closes the file descriptor.
135+
*
136+
* @param fd file descriptor handle. If NULL, no action is taken.
137+
*/
138+
void
139+
auto_close_gfd(gint* fd)
140+
{
141+
if (fd == NULL)
142+
return;
143+
144+
if (close(*fd) == EOF)
145+
log_error(g_strerror(errno));
146+
}
147+
148+
/**
149+
* Closes file stream
150+
*
151+
* @param fd file descriptor handle opened with fopen. If NULL, no action is taken.
152+
*/
153+
void
154+
auto_close_FILE(FILE** fd)
155+
{
156+
if (fd == NULL)
157+
return;
158+
159+
if (fclose(*fd) == EOF)
160+
log_error(g_strerror(errno));
161+
}
162+
133163
static gboolean
134164
_load_keyfile(prof_keyfile_t* keyfile)
135165
{

src/common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ void auto_free_guchar(guchar** str);
104104
*/
105105
#define auto_guchar __attribute__((__cleanup__(auto_free_guchar)))
106106

107+
#define auto_gfd __attribute__((__cleanup__(auto_close_gfd)))
108+
109+
void auto_close_gfd(gint* fd);
110+
111+
#define auto_FILE __attribute__((__cleanup__(auto_close_FILE)))
112+
113+
void auto_close_FILE(FILE** fd);
114+
107115
#if defined(__OpenBSD__)
108116
#define STR_MAYBE_NULL(p) (p) ?: "(null)"
109117
#else

src/tools/aesgcm_download.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@
5757
#include "ui/window.h"
5858
#include "common.h"
5959

60-
#define FALLBACK_MSG ""
61-
6260
void*
6361
aesgcm_file_get(void* userdata)
6462
{
6563
AESGCMDownload* aesgcm_dl = (AESGCMDownload*)userdata;
6664

67-
char* https_url = NULL;
68-
char* fragment = NULL;
65+
auto_char char* https_url = NULL;
66+
auto_char char* fragment = NULL;
6967

7068
// Convert the aesgcm:// URL to a https:// URL and extract the encoded key
7169
// and tag stored in the URL fragment.
@@ -79,8 +77,8 @@ aesgcm_file_get(void* userdata)
7977

8078
// Create a temporary file used for storing the ciphertext that is to be
8179
// retrieved from the https:// URL.
82-
gchar* tmpname = NULL;
83-
gint tmpfd;
80+
auto_gchar char* tmpname = NULL;
81+
auto_gfd gint tmpfd = 0;
8482
if ((tmpfd = g_file_open_tmp("profanity.XXXXXX", &tmpname, NULL)) == -1) {
8583
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
8684
"Downloading '%s' failed: Unable to create "
@@ -91,7 +89,7 @@ aesgcm_file_get(void* userdata)
9189
}
9290

9391
// Open the target file for storing the cleartext.
94-
FILE* outfh = fopen(aesgcm_dl->filename, "wb");
92+
auto_FILE FILE* outfh = fopen(aesgcm_dl->filename, "wb");
9593
if (outfh == NULL) {
9694
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
9795
"Downloading '%s' failed: Unable to open "
@@ -115,7 +113,7 @@ aesgcm_file_get(void* userdata)
115113

116114
http_file_get(http_dl); // TODO(wstrm): Verify result.
117115

118-
FILE* tmpfh = fopen(tmpname, "rb");
116+
auto_FILE FILE* tmpfh = fopen(tmpname, "rb");
119117
if (tmpfh == NULL) {
120118
http_print_transfer_update(aesgcm_dl->window, aesgcm_dl->id,
121119
"Downloading '%s' failed: Unable to open "
@@ -129,11 +127,6 @@ aesgcm_file_get(void* userdata)
129127
crypt_res = omemo_decrypt_file(tmpfh, outfh,
130128
http_dl->bytes_received, fragment);
131129

132-
if (fclose(tmpfh) == EOF) {
133-
cons_show_error(g_strerror(errno));
134-
}
135-
136-
close(tmpfd);
137130
remove(tmpname);
138131
g_free(tmpname);
139132

@@ -144,13 +137,6 @@ aesgcm_file_get(void* userdata)
144137
https_url, gcry_strerror(crypt_res));
145138
}
146139

147-
if (fclose(outfh) == EOF) {
148-
cons_show_error(g_strerror(errno));
149-
}
150-
151-
free(https_url);
152-
free(fragment);
153-
154140
if (aesgcm_dl->cmd_template != NULL) {
155141
gchar** argv = format_call_external_argv(aesgcm_dl->cmd_template,
156142
aesgcm_dl->filename,

src/tools/plugin_download.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
#include "ui/window.h"
5858
#include "common.h"
5959

60-
#define FALLBACK_MSG ""
61-
6260
void*
6361
plugin_download_install(void* userdata)
6462
{

0 commit comments

Comments
 (0)