Skip to content

Commit

Permalink
Bug 1529959, memory leaks in atob/btoa, r=jcj
Browse files Browse the repository at this point in the history
  • Loading branch information
kaie committed Feb 25, 2019
1 parent 094875f commit fa721ba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
36 changes: 25 additions & 11 deletions cmd/atob/atob.c
Expand Up @@ -105,20 +105,19 @@ Usage(char *progName)
"-i input");
fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
"-o output");
exit(-1);
}

int
main(int argc, char **argv)
{
char *progName;
SECStatus rv;
FILE *inFile, *outFile;
PLOptState *optstate;
FILE *inFile = NULL, *outFile = NULL;
PRBool closeIn = PR_TRUE, closeOut = PR_TRUE;
PLOptState *optstate = NULL;
PLOptStatus status;
int exitCode = -1;

inFile = 0;
outFile = 0;
progName = strrchr(argv[0], '/');
progName = progName ? progName + 1 : argv[0];

Expand All @@ -129,14 +128,15 @@ main(int argc, char **argv)
case '?':
case 'h':
Usage(progName);
goto loser;
break;

case 'i':
inFile = fopen(optstate->value, "r");
if (!inFile) {
fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
progName, optstate->value);
return -1;
goto loser;
}
break;

Expand All @@ -145,30 +145,44 @@ main(int argc, char **argv)
if (!outFile) {
fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
progName, optstate->value);
return -1;
goto loser;
}
break;
}
}
if (!inFile)
if (!inFile) {
inFile = stdin;
closeIn = PR_FALSE;
}
if (!outFile) {
#if defined(WIN32)
int smrv = _setmode(_fileno(stdout), _O_BINARY);
if (smrv == -1) {
fprintf(stderr,
"%s: Cannot change stdout to binary mode. Use -o option instead.\n",
progName);
return smrv;
goto loser;
}
#endif
outFile = stdout;
closeOut = PR_FALSE;
}
rv = decode_file(outFile, inFile);
if (rv != SECSuccess) {
fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
progName, PORT_GetError(), errno);
return -1;
goto loser;
}
exitCode = 0;
loser:
if (optstate) {
PL_DestroyOptState(optstate);
}
if (inFile && closeIn) {
fclose(inFile);
}
if (outFile && closeOut) {
fclose(outFile);
}
return 0;
return exitCode;
}
35 changes: 24 additions & 11 deletions cmd/btoa/btoa.c
Expand Up @@ -99,21 +99,20 @@ Usage(char *progName)
"-w suffix");
fprintf(stderr, "%-20s (use \"c\" as a shortcut for suffix CERTIFICATE)\n",
"");
exit(-1);
}

int
main(int argc, char **argv)
{
char *progName;
SECStatus rv;
FILE *inFile, *outFile;
PLOptState *optstate;
FILE *inFile = NULL, *outFile = NULL;
PRBool closeIn = PR_TRUE, closeOut = PR_TRUE;
PLOptState *optstate = NULL;
PLOptStatus status;
char *suffix = NULL;
int exitCode = -1;

inFile = 0;
outFile = 0;
progName = strrchr(argv[0], '/');
if (!progName)
progName = strrchr(argv[0], '\\');
Expand All @@ -125,14 +124,15 @@ main(int argc, char **argv)
switch (optstate->option) {
default:
Usage(progName);
goto loser;
break;

case 'i':
inFile = fopen(optstate->value, "rb");
if (!inFile) {
fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
progName, optstate->value);
return -1;
goto loser;
}
break;

Expand All @@ -141,7 +141,7 @@ main(int argc, char **argv)
if (!outFile) {
fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
progName, optstate->value);
return -1;
goto loser;
}
break;

Expand All @@ -166,10 +166,11 @@ main(int argc, char **argv)
fprintf(stderr,
"%s: Cannot change stdin to binary mode. Use -i option instead.\n",
progName);
return smrv;
goto loser;
}
#endif
inFile = stdin;
closeIn = PR_FALSE;
}
if (!outFile) {
#if defined(WIN32)
Expand All @@ -182,10 +183,11 @@ main(int argc, char **argv)
fprintf(stderr,
"%s: Cannot change stdout to binary mode. Use -o option instead.\n",
progName);
return smrv;
goto loser;
}
#endif
outFile = stdout;
closeOut = PR_FALSE;
}
if (suffix) {
fprintf(outFile, "-----BEGIN %s-----\n", suffix);
Expand All @@ -194,10 +196,21 @@ main(int argc, char **argv)
if (rv != SECSuccess) {
fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
progName, PORT_GetError(), errno);
return -1;
goto loser;
}
if (suffix) {
fprintf(outFile, "-----END %s-----\n", suffix);
}
return 0;
exitCode = 0;
loser:
if (optstate) {
PL_DestroyOptState(optstate);
}
if (inFile && closeIn) {
fclose(inFile);
}
if (outFile && closeOut) {
fclose(outFile);
}
return exitCode;
}

0 comments on commit fa721ba

Please sign in to comment.