From 9f1fdbfe0795c846f880c386f922f93c7186441a Mon Sep 17 00:00:00 2001 From: kalibera Date: Thu, 23 Nov 2023 16:51:28 +0000 Subject: [PATCH] Simplify allocation to avoid compiler warnings. git-svn-id: https://svn.r-project.org/R/trunk@85618 00db46b3-68df-0310-9c12-caf00c1e9a41 --- src/library/tools/src/gramRd.c | 27 +++++++++++++++++---------- src/library/tools/src/gramRd.y | 27 +++++++++++++++++---------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/library/tools/src/gramRd.c b/src/library/tools/src/gramRd.c index cd3abe7f186..a1a9c912afa 100644 --- a/src/library/tools/src/gramRd.c +++ b/src/library/tools/src/gramRd.c @@ -3987,15 +3987,16 @@ static void yyerror(const char *s) } } -#define TEXT_PUSH(c) do { \ - size_t nc = bp - stext; \ +#define TEXT_PUSH(c) do { \ + size_t nc = bp - stext; \ if (nc >= nstext - 1) { \ char *old = stext; \ - nstext *= 2; \ + nstext *= 2; \ stext = malloc(nstext); \ if(!stext) error(_("unable to allocate buffer for long string at line %d"), parseState.xxlineno);\ memmove(stext, old, nc); \ - if(old != st0) free(old); \ + if(st1) free(st1); \ + st1 = stext; \ bp = stext+nc; } \ *bp++ = ((char) c); \ } while(0) @@ -4093,6 +4094,7 @@ static int token(void) static int mkText(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0, lookahead; @@ -4123,13 +4125,14 @@ static int mkText(int c) stop: if (c != '\n') xxungetc(c); /* newline causes a break, but we keep it */ PRESERVE_SV(yylval = mkString2(stext, bp - stext)); - if(stext != st0) free(stext); + if(st1) free(st1); return TEXT; } static int mkComment(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; @@ -4139,7 +4142,7 @@ static int mkComment(int c) xxungetc(c); PRESERVE_SV(yylval = mkString2(stext, bp - stext)); - if(stext != st0) free(stext); + if(st1) free(st1); return COMMENT; } @@ -4171,6 +4174,7 @@ static int closingRawStringDelim(int c) static int mkCode(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; @@ -4297,13 +4301,14 @@ static int mkCode(int c) } if (c != '\n') xxungetc(c); PRESERVE_SV(yylval = mkString2(stext, bp - stext)); - if(stext != st0) free(stext); + if(st1) free(st1); return RCODE; } static int mkMarkup(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; int retval = 0, attempt = 0; @@ -4336,7 +4341,7 @@ static int mkMarkup(int c) } } PRESERVE_SV(yylval = mkString2(stext, bp - stext - 1)); - if(stext != st0) free(stext); + if(st1) free(st1); xxungetc(c); return retval; } @@ -4344,6 +4349,7 @@ static int mkMarkup(int c) static int mkIfdef(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; int retval; @@ -4380,13 +4386,14 @@ static int mkIfdef(int c) } break; } - if(stext != st0) free(stext); + if(st1) free(st1); return retval; } static int mkVerb(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; @@ -4419,7 +4426,7 @@ static int mkVerb(int c) }; if (c != '\n') xxungetc(c); PRESERVE_SV(yylval = mkString2(stext, bp - stext)); - if(stext != st0) free(stext); + if(st1) free(st1); return VERB; } diff --git a/src/library/tools/src/gramRd.y b/src/library/tools/src/gramRd.y index 52f1b53334a..41aa16d2112 100644 --- a/src/library/tools/src/gramRd.y +++ b/src/library/tools/src/gramRd.y @@ -1421,15 +1421,16 @@ static void yyerror(const char *s) } } -#define TEXT_PUSH(c) do { \ - size_t nc = bp - stext; \ +#define TEXT_PUSH(c) do { \ + size_t nc = bp - stext; \ if (nc >= nstext - 1) { \ char *old = stext; \ - nstext *= 2; \ + nstext *= 2; \ stext = malloc(nstext); \ if(!stext) error(_("unable to allocate buffer for long string at line %d"), parseState.xxlineno);\ memmove(stext, old, nc); \ - if(old != st0) free(old); \ + if(st1) free(st1); \ + st1 = stext; \ bp = stext+nc; } \ *bp++ = ((char) c); \ } while(0) @@ -1527,6 +1528,7 @@ static int token(void) static int mkText(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0, lookahead; @@ -1557,13 +1559,14 @@ static int mkText(int c) stop: if (c != '\n') xxungetc(c); /* newline causes a break, but we keep it */ PRESERVE_SV(yylval = mkString2(stext, bp - stext)); - if(stext != st0) free(stext); + if(st1) free(st1); return TEXT; } static int mkComment(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; @@ -1573,7 +1576,7 @@ static int mkComment(int c) xxungetc(c); PRESERVE_SV(yylval = mkString2(stext, bp - stext)); - if(stext != st0) free(stext); + if(st1) free(st1); return COMMENT; } @@ -1605,6 +1608,7 @@ static int closingRawStringDelim(int c) static int mkCode(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; @@ -1731,13 +1735,14 @@ static int mkCode(int c) } if (c != '\n') xxungetc(c); PRESERVE_SV(yylval = mkString2(stext, bp - stext)); - if(stext != st0) free(stext); + if(st1) free(st1); return RCODE; } static int mkMarkup(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; int retval = 0, attempt = 0; @@ -1770,7 +1775,7 @@ static int mkMarkup(int c) } } PRESERVE_SV(yylval = mkString2(stext, bp - stext - 1)); - if(stext != st0) free(stext); + if(st1) free(st1); xxungetc(c); return retval; } @@ -1778,6 +1783,7 @@ static int mkMarkup(int c) static int mkIfdef(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; int retval; @@ -1814,13 +1820,14 @@ static int mkIfdef(int c) } break; } - if(stext != st0) free(stext); + if(st1) free(st1); return retval; } static int mkVerb(int c) { char st0[INITBUFSIZE]; + char *st1 = NULL; unsigned int nstext = INITBUFSIZE; char *stext = st0, *bp = st0; @@ -1853,7 +1860,7 @@ static int mkVerb(int c) }; if (c != '\n') xxungetc(c); PRESERVE_SV(yylval = mkString2(stext, bp - stext)); - if(stext != st0) free(stext); + if(st1) free(st1); return VERB; }