Skip to content

Commit 09970db

Browse files
committed
cfs-fuse: Fix memory leaks
Caught by ASAN. Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 6f7d643 commit 09970db

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

tools/cfs-fuse.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ uint32_t erofs_build_time_nsec;
5151
int basedir_fd;
5252

5353
struct cfs_data {
54-
const char *source;
55-
const char *basedir;
54+
char *source;
55+
char *basedir;
5656
bool noacl;
5757
};
5858

@@ -1116,22 +1116,33 @@ static const struct fuse_lowlevel_ops cfs_oper = {
11161116
.lseek = cfs_lseek,
11171117
};
11181118

1119+
static void cleanup_cfs_data(struct cfs_data *data)
1120+
{
1121+
free(data->source);
1122+
free(data->basedir);
1123+
}
1124+
11191125
int main(int argc, char *argv[])
11201126
{
1121-
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1127+
__attribute__((cleanup(fuse_opt_free_args))) struct fuse_args args =
1128+
FUSE_ARGS_INIT(argc, argv);
11221129
struct fuse_session *se;
11231130
struct fuse_cmdline_opts opts;
11241131
struct fuse_loop_config config;
1125-
struct cfs_data data = { .source = NULL };
1132+
__attribute__((cleanup(cleanup_cfs_data))) struct cfs_data data = {
1133+
.source = NULL, .basedir = NULL
1134+
};
11261135
int fd;
11271136
struct stat s;
11281137
int r;
11291138
uint32_t cfs_flags;
11301139

11311140
int ret = -1;
11321141

1133-
if (fuse_parse_cmdline(&args, &opts) != 0)
1134-
return 1;
1142+
if (fuse_parse_cmdline(&args, &opts) != 0) {
1143+
ret = 1;
1144+
goto err_out1;
1145+
}
11351146
if (opts.show_help) {
11361147
printf("usage: %s [options] <file> <mountpoint>\n\n", argv[0]);
11371148
fuse_cmdline_help();
@@ -1156,8 +1167,10 @@ int main(int argc, char *argv[])
11561167
fuse_opt_add_arg(&args, "-o");
11571168
fuse_opt_add_arg(&args, "ro,default_permissions");
11581169

1159-
if (fuse_opt_parse(&args, &data, cfs_opts, NULL) == -1)
1160-
return 1;
1170+
if (fuse_opt_parse(&args, &data, cfs_opts, NULL) == -1) {
1171+
ret = 1;
1172+
goto err_out1;
1173+
}
11611174

11621175
fd = open(data.source, O_RDONLY | O_CLOEXEC);
11631176
if (fd < 0) {
@@ -1239,7 +1252,6 @@ int main(int argc, char *argv[])
12391252
fuse_session_destroy(se);
12401253
err_out1:
12411254
free(opts.mountpoint);
1242-
fuse_opt_free_args(&args);
12431255

12441256
return ret ? 1 : 0;
12451257
}

0 commit comments

Comments
 (0)