Skip to content

Commit

Permalink
Plug memory leaks in main.c code.
Browse files Browse the repository at this point in the history
Resolves #239
Fixes #274 (smuggled)

Signed-off-by: James Noss <jnoss@stsci.edu>
  • Loading branch information
jamienoss committed Nov 14, 2017
1 parent 36f47d3 commit e07f1c9
Show file tree
Hide file tree
Showing 46 changed files with 467 additions and 376 deletions.
43 changes: 21 additions & 22 deletions pkg/acs/calacs/acs2d/main2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
int status = 0; /* zero is OK */

# include <c_iraf.h> /* for c_irafinit */
#include "hstcal_memory.h"
#include "hstcal.h"
# include "ximio.h"
# include "hstio.h"
Expand All @@ -20,7 +21,6 @@ int status = 0; /* zero is OK */
# include "hstcalversion.h"
#include "trlbuf.h"

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

/* Allocate space for file names. */
PtrRegister ptrReg;
initPtrRegister(&ptrReg);
inlist = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
addPtr(&ptrReg, inlist, &free);
outlist = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
addPtr(&ptrReg, outlist, &free);
input = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
addPtr(&ptrReg, input, &free);
output = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
if (inlist == NULL || outlist == NULL ||
input == NULL || output == NULL) {
addPtr(&ptrReg, output, &free);
if (!inlist || !outlist || !input || !output) {
printf ("Can't even begin; out of memory.\n");
freeOnExit(&ptrReg);
exit (ERROR_RETURN);
}

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

/* Initial values. */
initSwitch (&acs2d_sw);
Expand Down Expand Up @@ -148,16 +155,19 @@ int main (int argc, char **argv) {
if (!(strcmp(argv[i],"--version")))
{
printf("%s\n",ACS_CAL_VER);
freeOnExit(&ptrReg);
exit(0);
}
if (!(strcmp(argv[i],"--gitinfo")))
{
printGitInfo();
freeOnExit(&ptrReg);
exit(0);
}
if (!(strcmp(argv[i],"--help")))
{
printHelp();
freeOnExit(&ptrReg);
exit(0);
}
for (j = 1; argv[i][j] != '\0'; j++) {
Expand All @@ -170,7 +180,7 @@ int main (int argc, char **argv) {
} else {
printf ("Unrecognized option %s\n", argv[i]);
printSyntax();
FreeNames (inlist, outlist, input, output);
freeOnExit(&ptrReg);
exit (1);
}
}
Expand All @@ -189,12 +199,13 @@ int main (int argc, char **argv) {
printf (" -dqi -glin -lflg -dark\n");
printf (" -flash -flat -shad -phot\n");
*/
FreeNames (inlist, outlist, input, output);
freeOnExit(&ptrReg);
exit (ERROR_RETURN);
}

/* Initialize the structure for managing trailer file comments */
InitTrlBuf ();
addPtr(&ptrReg, &trlbuf , &CloseTrlBuf);
trlGitInfo();

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

/* Expand the templates. */
i_imt = c_imtopen (inlist);
addPtr(&ptrReg, i_imt, &c_imtclose);
o_imt = c_imtopen (outlist);
addPtr(&ptrReg, o_imt, &c_imtclose);
n_in = c_imtlen (i_imt);
n_out = c_imtlen (o_imt);

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

if (status) {
FreeNames (inlist, outlist, input, output);
CloseTrlBuf(&trlbuf);
freeOnExit(&ptrReg);
exit (ERROR_RETURN);
}

Expand Down Expand Up @@ -284,24 +296,11 @@ int main (int argc, char **argv) {
}
}

/* Close lists of file names, and free name buffers
and trailer file buffer.
*/
c_imtclose (i_imt);
c_imtclose (o_imt);
FreeRefFile (&refnames);
FreeNames (inlist, outlist, input, output);
CloseTrlBuf(&trlbuf);

freeOnExit(&ptrReg);

if (status)
exit (ERROR_RETURN);
else
exit (ACS_OK);
}

static void FreeNames (char *inlist, char *outlist, char *input, char *output) {
free (output);
free (input);
free (outlist);
free (inlist);
}
2 changes: 1 addition & 1 deletion pkg/acs/calacs/acs2d/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def build(bld):
name = 'acs2d.e',
source = 'main2d.c',
target = 'acs2d.e',
use = ['calacs', 'imphttab'] + bld.env.LOCAL_LIBS,
use = ['hstcallib', 'calacs', 'imphttab'] + bld.env.LOCAL_LIBS,
lib = bld.env.EXTERNAL_LIBS,
libpath = bld.env.LIBPATH,
install_path = '${PREFIX}/bin'
Expand Down
42 changes: 21 additions & 21 deletions pkg/acs/calacs/acsccd/mainccd.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
int status = 0; /* zero is OK */

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

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

c_irafinit (argc, argv);

PtrRegister ptrReg;
initPtrRegister(&ptrReg);
/* Allocate space for file names. */
inlist = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
addPtr(&ptrReg, inlist, &free);
outlist = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
addPtr(&ptrReg, outlist, &free);
input = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
addPtr(&ptrReg, input, &free);
output = calloc (CHAR_LINE_LENGTH+1, sizeof (char));
addPtr(&ptrReg, output, &free);

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

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

/* Initial values. */
initSwitch (&ccd_sw);
Expand All @@ -141,16 +148,19 @@ int main (int argc, char **argv) {
if (!(strcmp(argv[i],"--version")))
{
printf("%s\n",ACS_CAL_VER);
freeOnExit(&ptrReg);
exit(0);
}
if (!(strcmp(argv[i],"--gitinfo")))
{
printGitInfo();
freeOnExit(&ptrReg);
exit(0);
}
if (!(strcmp(argv[i],"--help")))
{
printHelp();
freeOnExit(&ptrReg);
exit(0);
}
for (j = 1; argv[i][j] != '\0'; j++) {
Expand All @@ -163,7 +173,7 @@ int main (int argc, char **argv) {
} else {
printf (MsgText, "Unrecognized option %s\n", argv[i]);
printSyntax();
FreeNames (inlist, outlist, input, output);
freeOnExit(&ptrReg);
exit (1);
}
}
Expand All @@ -181,11 +191,12 @@ int main (int argc, char **argv) {
printf (" command-line switches:\n");
printf (" -dqi -atod -blev -bias\n");
*/
FreeNames (inlist, outlist, input, output);
freeOnExit(&ptrReg);
exit (ERROR_RETURN);
}
/* Initialize the structure for managing trailer file comments */
InitTrlBuf ();
addPtr(&ptrReg, &trlbuf, &CloseTrlBuf);
trlGitInfo();

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

/* Expand the templates. */
i_imt = c_imtopen (inlist);
addPtr(&ptrReg, i_imt, &c_imtclose);
o_imt = c_imtopen (outlist);
addPtr(&ptrReg, o_imt, &c_imtclose);
n_in = c_imtlen (i_imt);
n_out = c_imtlen (o_imt);

/* The number of input and output files must be the same. */
if (CompareNumbers (n_in, n_out, "output"))
status = 1;
if (status) {
FreeNames (inlist, outlist, input, output);
CloseTrlBuf(&trlbuf);
freeOnExit(&ptrReg);
exit (ERROR_RETURN);
}

Expand Down Expand Up @@ -251,23 +263,11 @@ int main (int argc, char **argv) {
}
}

/* Close lists of file names, and free name buffers. */
c_imtclose (i_imt);
c_imtclose (o_imt);
FreeRefFile (&refnames);
FreeNames (inlist, outlist, input, output);
CloseTrlBuf(&trlbuf);

freeOnExit(&ptrReg);

if (status)
exit (ERROR_RETURN);
else
exit (0);
}


static void FreeNames (char *inlist, char *outlist, char *input, char *output) {
free (output);
free (input);
free (outlist);
free (inlist);
}
2 changes: 1 addition & 1 deletion pkg/acs/calacs/acsccd/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def build(bld):
t = bld.program(
source = 'mainccd.c',
target = 'acsccd.e',
use = ['calacs', 'imphttab'] + bld.env.LOCAL_LIBS,
use = ['hstcallib', 'calacs', 'imphttab'] + bld.env.LOCAL_LIBS,
lib = bld.env.EXTERNAL_LIBS,
libpath = bld.env.LIBPATH,
install_path = '${PREFIX}/bin'
Expand Down
Loading

0 comments on commit e07f1c9

Please sign in to comment.