Skip to content

Commit

Permalink
Simplify allocation to avoid compiler warnings.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@85618 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
kalibera committed Nov 23, 2023
1 parent ba0e89f commit 9f1fdbf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
27 changes: 17 additions & 10 deletions src/library/tools/src/gramRd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -4336,14 +4341,15 @@ 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;
}

static int mkIfdef(int c)
{
char st0[INITBUFSIZE];
char *st1 = NULL;
unsigned int nstext = INITBUFSIZE;
char *stext = st0, *bp = st0;
int retval;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
27 changes: 17 additions & 10 deletions src/library/tools/src/gramRd.y
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1770,14 +1775,15 @@ 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;
}

static int mkIfdef(int c)
{
char st0[INITBUFSIZE];
char *st1 = NULL;
unsigned int nstext = INITBUFSIZE;
char *stext = st0, *bp = st0;
int retval;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 9f1fdbf

Please sign in to comment.