Skip to content

Commit 83a2da5

Browse files
committed
ensure disk_close to avoid mem-leak #40
1 parent 0482c92 commit 83a2da5

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

bins/unzzipcat-mem.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static FILE* create_fopen(char* name, char* mode, int subdirs)
8989

9090
static int unzzip_cat (int argc, char ** argv, int extract)
9191
{
92-
int done;
92+
int done = 0;
9393
int argn;
9494
ZZIP_MEM_DISK* disk;
9595

@@ -116,47 +116,50 @@ static int unzzip_cat (int argc, char ** argv, int extract)
116116
FILE* out = stdout;
117117
if (extract) out = create_fopen(name, "wb", 1);
118118
if (! out) {
119-
if (errno != EISDIR) done = EXIT_ERRORS;
119+
if (errno != EISDIR) {
120+
DBG3("can not open output file %i %s", errno, strerror(errno));
121+
done = EXIT_ERRORS;
122+
}
120123
continue;
121124
}
122125
unzzip_mem_disk_cat_file (disk, name, out);
123126
if (extract) fclose(out);
124127
}
125-
return done;
126-
}
127-
128-
if (argc == 3 && !extract)
128+
}
129+
else if (argc == 3 && !extract)
129130
{ /* list from one spec */
130131
ZZIP_MEM_ENTRY* entry = 0;
131132
while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0)))
132133
{
133134
unzzip_mem_entry_fprint (disk, entry, stdout);
134135
}
135-
136-
return 0;
137-
}
138-
139-
for (argn=1; argn < argc; argn++)
140-
{ /* list only the matching entries - each in order of commandline */
141-
ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
142-
for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
143-
{
144-
char* name = zzip_mem_entry_to_name (entry);
145-
if (! _zzip_fnmatch (argv[argn], name,
146-
_zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
136+
} else {
137+
for (argn=1; argn < argc; argn++)
138+
{ /* list only the matching entries - each in order of commandline */
139+
ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
140+
for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
147141
{
148-
FILE* out = stdout;
149-
if (extract) out = create_fopen(name, "wb", 1);
150-
if (! out) {
151-
if (errno != EISDIR) done = EXIT_ERRORS;
152-
continue;
153-
}
154-
unzzip_mem_disk_cat_file (disk, name, out);
155-
if (extract) fclose(out);
156-
break; /* match loop */
142+
char* name = zzip_mem_entry_to_name (entry);
143+
if (! _zzip_fnmatch (argv[argn], name,
144+
_zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
145+
{
146+
FILE* out = stdout;
147+
if (extract) out = create_fopen(name, "wb", 1);
148+
if (! out) {
149+
if (errno != EISDIR) {
150+
DBG3("can not open output file %i %s", errno, strerror(errno));
151+
done = EXIT_ERRORS;
152+
}
153+
continue;
154+
}
155+
unzzip_mem_disk_cat_file (disk, name, out);
156+
if (extract) fclose(out);
157+
break; /* match loop */
158+
}
157159
}
158160
}
159161
}
162+
zzip_mem_disk_close(disk);
160163
return done;
161164
}
162165

bins/unzzipdir-mem.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ unzzip_list (int argc, char ** argv, int verbose)
6868
printf ("%lli/%lli %s %s\n", csize, usize, defl, name);
6969
}
7070
}
71-
return 0;
7271
}
73-
74-
if (argc == 3)
72+
else if (argc == 3)
7573
{ /* list from one spec */
7674
ZZIP_MEM_ENTRY* entry = 0;
7775
while ((entry = zzip_mem_disk_findmatch(disk, argv[2], entry, 0, 0)))
@@ -89,9 +87,8 @@ unzzip_list (int argc, char ** argv, int verbose)
8987
printf ("%lli/%lli %s %s\n", csize, usize, defl, name);
9088
}
9189
}
92-
return 0;
9390
}
94-
91+
else
9592
{ /* list only the matching entries - in order of zip directory */
9693
ZZIP_MEM_ENTRY* entry = zzip_mem_disk_findfirst(disk);
9794
for (; entry ; entry = zzip_mem_disk_findnext(disk, entry))
@@ -118,8 +115,9 @@ unzzip_list (int argc, char ** argv, int verbose)
118115
}
119116
}
120117
}
121-
return 0;
122118
}
119+
zzip_mem_disk_close(disk);
120+
return EXIT_OK;
123121
}
124122

125123
int

0 commit comments

Comments
 (0)