Add the ability to bail before EOF to get_line_alloc();

If we stop reading a file before getting to the end, get_line_alloc()
will not clean up the memory it allocated. Give get_line_alloc()
the ability to do a forced cleanup by calling it with a NULL file,
and use that ability where we stop reading a file early.

Also clear the state variables when we free the memory, to
pre-empt use-after-free.
This commit is contained in:
Simon Kelley committed 2026-06-02 00:22:05 +01:00
1 parent 7d42376a3a
commit 4bf46ceb37
2 files changed
+11 -6

No files matched your search

+5 -1
View File
@@ -6270,7 +6270,11 @@ void read_opts(int argc, char **argv, char *compile_opts)
if ((token = strtok(NULL, " \t\n\r")) &&
(daemon->domain_suffix = canonicalise_opt(token)))
break;
{
/* We don't call get_line_alloc() until it returns false and frees the automatically */
get_line_alloc(NULL, &line, &linesz);
break;
}
}
fclose(f);
+6 -5
View File
@@ -1038,16 +1038,15 @@ void free_real(const char *func, unsigned int line, void *ptr)
}
/* get lines from f, expand buffer as required. Buffer is freed when
EOF reached and we return zero. */
EOF reached and we return zero. Buffer also freed if f == NULL. */
int get_line_alloc(FILE *f, char **buffp, size_t *sizep)
{
int c;
size_t cnt = 0;
char *buff = *buffp;
while (1) {
c = getc(f);
while (1) {
int c = f ? getc(f) : EOF;
if (cnt == *sizep)
{
void *new;
@@ -1081,6 +1080,8 @@ int get_line_alloc(FILE *f, char **buffp, size_t *sizep)
return 1;
free(buff);
*buffp = NULL;
*sizep = 0;
return 0;
}