Skip to content

Commit e07f1c9

Browse files
committed
Plug memory leaks in main.c code.
Resolves spacetelescope#239 Fixes spacetelescope#274 (smuggled) Signed-off-by: James Noss <[email protected]>
1 parent 36f47d3 commit e07f1c9

File tree

46 files changed

+467
-376
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+467
-376
lines changed

pkg/acs/calacs/acs2d/main2d.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
int status = 0; /* zero is OK */
99

1010
# include <c_iraf.h> /* for c_irafinit */
11+
#include "hstcal_memory.h"
1112
#include "hstcal.h"
1213
# include "ximio.h"
1314
# include "hstio.h"
@@ -20,7 +21,6 @@ int status = 0; /* zero is OK */
2021
# include "hstcalversion.h"
2122
#include "trlbuf.h"
2223

23-
static void FreeNames (char *, char *, char *, char *);
2424
static void printSyntax(void)
2525
{
2626
printf("syntax: acs2d [--help] [-t] [-v] [-q] [--version] [--gitinfo] input output\n");
@@ -99,18 +99,25 @@ int main (int argc, char **argv) {
9999
c_irafinit (argc, argv);
100100

101101
/* Allocate space for file names. */
102+
PtrRegister ptrReg;
103+
initPtrRegister(&ptrReg);
102104
inlist = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
105+
addPtr(&ptrReg, inlist, &free);
103106
outlist = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
107+
addPtr(&ptrReg, outlist, &free);
104108
input = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
109+
addPtr(&ptrReg, input, &free);
105110
output = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
106-
if (inlist == NULL || outlist == NULL ||
107-
input == NULL || output == NULL) {
111+
addPtr(&ptrReg, output, &free);
112+
if (!inlist || !outlist || !input || !output) {
108113
printf ("Can't even begin; out of memory.\n");
114+
freeOnExit(&ptrReg);
109115
exit (ERROR_RETURN);
110116
}
111117

112118
/* Initialize the lists of reference file keywords and names. */
113119
InitRefFile (&refnames);
120+
addPtr(&ptrReg, &refnames, &FreeRefFile);
114121

115122
/* Initial values. */
116123
initSwitch (&acs2d_sw);
@@ -148,16 +155,19 @@ int main (int argc, char **argv) {
148155
if (!(strcmp(argv[i],"--version")))
149156
{
150157
printf("%s\n",ACS_CAL_VER);
158+
freeOnExit(&ptrReg);
151159
exit(0);
152160
}
153161
if (!(strcmp(argv[i],"--gitinfo")))
154162
{
155163
printGitInfo();
164+
freeOnExit(&ptrReg);
156165
exit(0);
157166
}
158167
if (!(strcmp(argv[i],"--help")))
159168
{
160169
printHelp();
170+
freeOnExit(&ptrReg);
161171
exit(0);
162172
}
163173
for (j = 1; argv[i][j] != '\0'; j++) {
@@ -170,7 +180,7 @@ int main (int argc, char **argv) {
170180
} else {
171181
printf ("Unrecognized option %s\n", argv[i]);
172182
printSyntax();
173-
FreeNames (inlist, outlist, input, output);
183+
freeOnExit(&ptrReg);
174184
exit (1);
175185
}
176186
}
@@ -189,12 +199,13 @@ int main (int argc, char **argv) {
189199
printf (" -dqi -glin -lflg -dark\n");
190200
printf (" -flash -flat -shad -phot\n");
191201
*/
192-
FreeNames (inlist, outlist, input, output);
202+
freeOnExit(&ptrReg);
193203
exit (ERROR_RETURN);
194204
}
195205

196206
/* Initialize the structure for managing trailer file comments */
197207
InitTrlBuf ();
208+
addPtr(&ptrReg, &trlbuf , &CloseTrlBuf);
198209
trlGitInfo();
199210

200211
/* Copy command-line value for QUIET to structure */
@@ -214,7 +225,9 @@ int main (int argc, char **argv) {
214225

215226
/* Expand the templates. */
216227
i_imt = c_imtopen (inlist);
228+
addPtr(&ptrReg, i_imt, &c_imtclose);
217229
o_imt = c_imtopen (outlist);
230+
addPtr(&ptrReg, o_imt, &c_imtclose);
218231
n_in = c_imtlen (i_imt);
219232
n_out = c_imtlen (o_imt);
220233

@@ -223,8 +236,7 @@ int main (int argc, char **argv) {
223236
status = 1;
224237

225238
if (status) {
226-
FreeNames (inlist, outlist, input, output);
227-
CloseTrlBuf(&trlbuf);
239+
freeOnExit(&ptrReg);
228240
exit (ERROR_RETURN);
229241
}
230242

@@ -284,24 +296,11 @@ int main (int argc, char **argv) {
284296
}
285297
}
286298

287-
/* Close lists of file names, and free name buffers
288-
and trailer file buffer.
289-
*/
290-
c_imtclose (i_imt);
291-
c_imtclose (o_imt);
292-
FreeRefFile (&refnames);
293-
FreeNames (inlist, outlist, input, output);
294-
CloseTrlBuf(&trlbuf);
299+
300+
freeOnExit(&ptrReg);
295301

296302
if (status)
297303
exit (ERROR_RETURN);
298304
else
299305
exit (ACS_OK);
300306
}
301-
302-
static void FreeNames (char *inlist, char *outlist, char *input, char *output) {
303-
free (output);
304-
free (input);
305-
free (outlist);
306-
free (inlist);
307-
}

pkg/acs/calacs/acs2d/wscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def build(bld):
55
name = 'acs2d.e',
66
source = 'main2d.c',
77
target = 'acs2d.e',
8-
use = ['calacs', 'imphttab'] + bld.env.LOCAL_LIBS,
8+
use = ['hstcallib', 'calacs', 'imphttab'] + bld.env.LOCAL_LIBS,
99
lib = bld.env.EXTERNAL_LIBS,
1010
libpath = bld.env.LIBPATH,
1111
install_path = '${PREFIX}/bin'

pkg/acs/calacs/acsccd/mainccd.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
int status = 0; /* zero is OK */
99

1010
# include <c_iraf.h> /* for c_irafinit */
11+
#include "hstcal_memory.h"
1112
#include "hstcal.h"
1213
# include "ximio.h"
1314
# include "hstio.h"
@@ -24,7 +25,6 @@ int status = 0; /* zero is OK */
2425
char MsgText[MSG_BUFF_LENGTH]; // Global char auto initialized to '\0'
2526
struct TrlBuf trlbuf = { 0 };
2627

27-
static void FreeNames (char *, char *, char *, char *);
2828
static void printSyntax(void)
2929
{
3030
printf ("syntax: acsccd [--help] [-t] [-v] [-q] [--version] [--gitinfo] input output\n");
@@ -98,15 +98,21 @@ int main (int argc, char **argv) {
9898

9999
c_irafinit (argc, argv);
100100

101+
PtrRegister ptrReg;
102+
initPtrRegister(&ptrReg);
101103
/* Allocate space for file names. */
102104
inlist = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
105+
addPtr(&ptrReg, inlist, &free);
103106
outlist = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
107+
addPtr(&ptrReg, outlist, &free);
104108
input = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
109+
addPtr(&ptrReg, input, &free);
105110
output = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
111+
addPtr(&ptrReg, output, &free);
106112

107-
if (inlist == NULL || outlist == NULL ||
108-
input == NULL || output == NULL) {
113+
if (!inlist || !outlist|| !input || !output) {
109114
printf ("Can't even begin; out of memory.\n");
115+
freeOnExit(&ptrReg);
110116
exit (ERROR_RETURN);
111117
}
112118
inlist[0] = '\0';
@@ -116,6 +122,7 @@ int main (int argc, char **argv) {
116122

117123
/* Initialize the lists of reference file keywords and names. */
118124
InitRefFile (&refnames);
125+
addPtr(&ptrReg, &refnames, &FreeRefFile);
119126

120127
/* Initial values. */
121128
initSwitch (&ccd_sw);
@@ -141,16 +148,19 @@ int main (int argc, char **argv) {
141148
if (!(strcmp(argv[i],"--version")))
142149
{
143150
printf("%s\n",ACS_CAL_VER);
151+
freeOnExit(&ptrReg);
144152
exit(0);
145153
}
146154
if (!(strcmp(argv[i],"--gitinfo")))
147155
{
148156
printGitInfo();
157+
freeOnExit(&ptrReg);
149158
exit(0);
150159
}
151160
if (!(strcmp(argv[i],"--help")))
152161
{
153162
printHelp();
163+
freeOnExit(&ptrReg);
154164
exit(0);
155165
}
156166
for (j = 1; argv[i][j] != '\0'; j++) {
@@ -163,7 +173,7 @@ int main (int argc, char **argv) {
163173
} else {
164174
printf (MsgText, "Unrecognized option %s\n", argv[i]);
165175
printSyntax();
166-
FreeNames (inlist, outlist, input, output);
176+
freeOnExit(&ptrReg);
167177
exit (1);
168178
}
169179
}
@@ -181,11 +191,12 @@ int main (int argc, char **argv) {
181191
printf (" command-line switches:\n");
182192
printf (" -dqi -atod -blev -bias\n");
183193
*/
184-
FreeNames (inlist, outlist, input, output);
194+
freeOnExit(&ptrReg);
185195
exit (ERROR_RETURN);
186196
}
187197
/* Initialize the structure for managing trailer file comments */
188198
InitTrlBuf ();
199+
addPtr(&ptrReg, &trlbuf, &CloseTrlBuf);
189200
trlGitInfo();
190201

191202
/* Copy command-line value for QUIET to structure */
@@ -202,16 +213,17 @@ int main (int argc, char **argv) {
202213

203214
/* Expand the templates. */
204215
i_imt = c_imtopen (inlist);
216+
addPtr(&ptrReg, i_imt, &c_imtclose);
205217
o_imt = c_imtopen (outlist);
218+
addPtr(&ptrReg, o_imt, &c_imtclose);
206219
n_in = c_imtlen (i_imt);
207220
n_out = c_imtlen (o_imt);
208221

209222
/* The number of input and output files must be the same. */
210223
if (CompareNumbers (n_in, n_out, "output"))
211224
status = 1;
212225
if (status) {
213-
FreeNames (inlist, outlist, input, output);
214-
CloseTrlBuf(&trlbuf);
226+
freeOnExit(&ptrReg);
215227
exit (ERROR_RETURN);
216228
}
217229

@@ -251,23 +263,11 @@ int main (int argc, char **argv) {
251263
}
252264
}
253265

254-
/* Close lists of file names, and free name buffers. */
255-
c_imtclose (i_imt);
256-
c_imtclose (o_imt);
257-
FreeRefFile (&refnames);
258-
FreeNames (inlist, outlist, input, output);
259-
CloseTrlBuf(&trlbuf);
266+
267+
freeOnExit(&ptrReg);
260268

261269
if (status)
262270
exit (ERROR_RETURN);
263271
else
264272
exit (0);
265273
}
266-
267-
268-
static void FreeNames (char *inlist, char *outlist, char *input, char *output) {
269-
free (output);
270-
free (input);
271-
free (outlist);
272-
free (inlist);
273-
}

pkg/acs/calacs/acsccd/wscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def build(bld):
44
t = bld.program(
55
source = 'mainccd.c',
66
target = 'acsccd.e',
7-
use = ['calacs', 'imphttab'] + bld.env.LOCAL_LIBS,
7+
use = ['hstcallib', 'calacs', 'imphttab'] + bld.env.LOCAL_LIBS,
88
lib = bld.env.EXTERNAL_LIBS,
99
libpath = bld.env.LIBPATH,
1010
install_path = '${PREFIX}/bin'

0 commit comments

Comments
 (0)