Don't assume libc can realloc() a NULL string.

If malloc/realloc fails, make sure we just return; yyerror() is not terminal.
This commit is contained in:
Todd C. Miller
2003-03-13 20:27:09 +00:00
parent 1725de8772
commit a01f25b5f1

View File

@@ -365,8 +365,10 @@ fill(s, len)
int i, j; int i, j;
yylval.string = (char *) malloc(len + 1); yylval.string = (char *) malloc(len + 1);
if (yylval.string == NULL) if (yylval.string == NULL) {
yyerror("unable to allocate memory"); yyerror("unable to allocate memory");
return;
}
/* Copy the string and collapse any escaped characters. */ /* Copy the string and collapse any escaped characters. */
for (i = 0, j = 0; i < len; i++, j++) { for (i = 0, j = 0; i < len; i++, j++) {
@@ -386,8 +388,10 @@ fill_cmnd(s, len)
arg_len = arg_size = 0; arg_len = arg_size = 0;
yylval.command.cmnd = (char *) malloc(len + 1); yylval.command.cmnd = (char *) malloc(len + 1);
if (yylval.command.cmnd == NULL) if (yylval.command.cmnd == NULL) {
yyerror("unable to allocate memory"); yyerror("unable to allocate memory");
return;
}
/* copy the string and NULL-terminate it (escapes handled by fnmatch) */ /* copy the string and NULL-terminate it (escapes handled by fnmatch) */
(void) strncpy(yylval.command.cmnd, s, len); (void) strncpy(yylval.command.cmnd, s, len);
@@ -416,10 +420,14 @@ fill_args(s, len, addspace)
while (new_len >= (arg_size += COMMANDARGINC)) while (new_len >= (arg_size += COMMANDARGINC))
; ;
if ((p = (char *) realloc(yylval.command.args, arg_size)) == NULL) { p = yylval.command.args ?
(char *) realloc(yylval.command.args, arg_size) :
(char *) malloc(arg_size);
if (p == NULL) {
if (yylval.command.args != NULL) if (yylval.command.args != NULL)
free(yylval.command.args); free(yylval.command.args);
yyerror("unable to allocate memory"); yyerror("unable to allocate memory");
return;
} else } else
yylval.command.args = p; yylval.command.args = p;
} }