On Thu, Dec 24, 2015 at 12:41:28PM -0500, Michael McConville wrote: > 1. realloc acts like malloc when ptr == NULL
Why not "#endif", - " if (newsize && YY_SIZE_MAX / newsize < sizeof *newss)", - " goto bail;", - " newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :", - " (short *)malloc(newsize * sizeof *newss); /* overflow check above */", + " newss = reallocarray(yyss, newsize, sizeof(*newss)); instead? Note however that the commit message in which the overflow checks were introduced says revision 1.28 date: 2007/09/03 21:14:58; author: deraadt; state: Exp; lines: +13 -4; move back to using malloc() instead of calloc(), because the yacc skeleton really should only call malloc/realloc/free, no other external APIs at all. theefore, add a pre-check for the overflow case, thus protecting realloc too; tested mblamer, ok millert, help from kettenis but that was long before reallocarray existed, so I'm not sure. > 2. no need to check for NULL before free > > ok? > > > ? cscope.out > Index: skeleton.c > =================================================================== > RCS file: /cvs/src/usr.bin/yacc/skeleton.c,v > retrieving revision 1.35 > diff -u -p -r1.35 skeleton.c > --- skeleton.c 16 Mar 2014 18:38:30 -0000 1.35 > +++ skeleton.c 24 Dec 2015 17:34:02 -0000 > @@ -137,16 +137,14 @@ char *body[] = > "#endif", > " if (newsize && YY_SIZE_MAX / newsize < sizeof *newss)", > " goto bail;", > - " newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :", > - " (short *)malloc(newsize * sizeof *newss); /* overflow check > above */", > + " newss = realloc(yyss, newsize * sizeof(*newss)); /* overflow check > above */", > " if (newss == NULL)", > " goto bail;", > " yyss = newss;", > " yyssp = newss + sslen;", > " if (newsize && YY_SIZE_MAX / newsize < sizeof *newvs)", > " goto bail;", > - " newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) > :", > - " (YYSTYPE *)malloc(newsize * sizeof *newvs); /* overflow check > above */", > + " newvs = realloc(yyvs, newsize * sizeof(*newvs)); /* overflow check > above */", > " if (newvs == NULL)", > " goto bail;", > " yyvs = newvs;", > @@ -155,10 +153,8 @@ char *body[] = > " yysslim = yyss + newsize - 1;", > " return 0;", > "bail:", > - " if (yyss)", > - " free(yyss);", > - " if (yyvs)", > - " free(yyvs);", > + " free(yyss);", > + " free(yyvs);", > " yyss = yyssp = NULL;", > " yyvs = yyvsp = NULL;", > " yystacksize = 0;", > @@ -368,19 +364,15 @@ char *trailer[] = > "yyoverflow:", > " yyerror(\"yacc stack overflow\");", > "yyabort:", > - " if (yyss)", > - " free(yyss);", > - " if (yyvs)", > - " free(yyvs);", > + " free(yyss);", > + " free(yyvs);", > " yyss = yyssp = NULL;", > " yyvs = yyvsp = NULL;", > " yystacksize = 0;", > " return (1);", > "yyaccept:", > - " if (yyss)", > - " free(yyss);", > - " if (yyvs)", > - " free(yyvs);", > + " free(yyss);", > + " free(yyvs);", > " yyss = yyssp = NULL;", > " yyvs = yyvsp = NULL;", > " yystacksize = 0;", >