Skip to content

Commit

Permalink
fgetll(): Work around spurious GCC warning on realloc()
Browse files Browse the repository at this point in the history
Claudio reports this:

gcc (GCC) 14.0.1 20240411 (Red Hat 14.0.1-0)
Fedora release 40 (Forty)

misc.c: In function 'fgetll':
misc.c:214:34: error: pointer 'cp' may be used after 'realloc' [-Werror=use-after-free]
  214 |                         new_cp = realloc(cp, len + increase);
      |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
misc.c:208:26: note: call to 'realloc' here
  208 |                 new_cp = realloc(cp, len + increase);

Completes b59ccbd
Fixes #5470
  • Loading branch information
solardiz committed Apr 30, 2024
1 parent f3b9c70 commit ccdcf21
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ char *fgetll(char *s, size_t size, FILE *stream)
void *new_cp;

new_cp = realloc(cp, len + increase);
/* Reference the relocated pointer to avoid GCC -Wuse-after-free */
if (new_cp)
cp = new_cp;

while (!new_cp) {
increase >>= 2;
Expand All @@ -217,6 +220,7 @@ char *fgetll(char *s, size_t size, FILE *stream)
cp = new_cp;
}

/* This became redundant after GCC warning workarounds above */
cp = new_cp;

/* We get an EOF if there is no trailing \n on the last line */
Expand Down

0 comments on commit ccdcf21

Please sign in to comment.