files: check the result of fdopen(), to avoid a possible crash

When safe_tempfile() returns a valid filename, it should also
return a valid open stream.

This fixes https://savannah.gnu.org/bugs/?61064.

Bug existed since version 1.3.8, commit 5e068c60.
master
Benno Schulenberg 2021-08-20 10:28:42 +02:00
parent 51eb938fea
commit eaff5ec9e5
1 changed files with 5 additions and 3 deletions

View File

@ -1469,13 +1469,15 @@ char *safe_tempfile(FILE **stream)
fd = mkstemp(tempfile_name); fd = mkstemp(tempfile_name);
if (fd == -1) { *stream = (fd > 0) ? fdopen(fd, "r+b") : NULL;
if (*stream == NULL) {
if (fd > 0)
close(fd);
free(tempfile_name); free(tempfile_name);
return NULL; return NULL;
} }
*stream = fdopen(fd, "r+b");
return tempfile_name; return tempfile_name;
} }