Skip to content

Commit a097f5d

Browse files
committed
Use the gcc cleanup attribute in launcher
1 parent 6a901ea commit a097f5d

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

launcher.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
#define KITTY_LIB_DIR_NAME "lib"
3131
#endif
3232

33+
static inline void cleanup_free(void *p) { free(*(void**) p); }
34+
#define FREE_AFTER_FUNCTION __attribute__((cleanup(cleanup_free)))
35+
36+
3337
#ifndef __FreeBSD__
3438
static inline bool
3539
safe_realpath(const char* src, char *buf, size_t buf_sz) {
36-
char* ans = realpath(src, NULL);
40+
FREE_AFTER_FUNCTION char* ans = realpath(src, NULL);
3741
if (ans == NULL) return false;
3842
snprintf(buf, buf_sz, "%s", ans);
39-
free(ans);
4043
return true;
4144
}
4245
#endif
@@ -71,8 +74,8 @@ canonicalize_path(const char *srcpath, char *dstpath, size_t sz) {
7174
// remove . and .. path segments
7275
bool ok = false;
7376
size_t plen = strlen(srcpath) + 1, chk;
74-
char *wtmp = malloc(plen);
75-
char **tokv = malloc(sizeof(char*) * plen);
77+
FREE_AFTER_FUNCTION char *wtmp = malloc(plen);
78+
FREE_AFTER_FUNCTION char **tokv = malloc(sizeof(char*) * plen);
7679
if (!wtmp || !tokv) goto end;
7780
char *s, *tok, *sav;
7881
bool relpath = *srcpath != '/';
@@ -116,7 +119,6 @@ canonicalize_path(const char *srcpath, char *dstpath, size_t sz) {
116119
ok = true;
117120

118121
end:
119-
free(wtmp); free(tokv);
120122
return ok;
121123
}
122124

@@ -259,7 +261,7 @@ read_exe_path(char *exe, size_t buf_sz) {
259261
int main(int argc, char *argv[]) {
260262
char exe[PATH_MAX+1] = {0};
261263
char exe_dir_buf[PATH_MAX+1] = {0};
262-
const char *lc_ctype = NULL;
264+
FREE_AFTER_FUNCTION const char *lc_ctype = NULL;
263265
#ifdef __APPLE__
264266
lc_ctype = getenv("LC_CTYPE");
265267
#endif
@@ -278,6 +280,5 @@ int main(int argc, char *argv[]) {
278280
if (lc_ctype) lc_ctype = strdup(lc_ctype);
279281
RunData run_data = {.exe = exe, .exe_dir = exe_dir, .lib_dir = lib, .argc = argc, .argv = argv, .lc_ctype = lc_ctype};
280282
ret = run_embedded(run_data);
281-
if (lc_ctype) free((void*)lc_ctype);
282283
return ret;
283284
}

0 commit comments

Comments
 (0)