diff mbox series

Remove memory leak in fdopen (bug 31840)

Message ID mvmv82pt52h.fsf@suse.de
State New
Headers show
Series Remove memory leak in fdopen (bug 31840) | expand

Commit Message

Andreas Schwab June 4, 2024, 9:44 a.m. UTC
Deallocate the memory for the FILE structure when seeking to the end fails
in append mode.

Fixes: ea33158c96 ("Fix offset caching for streams and use it for ftell (BZ #16680)")
---
 libio/iofdopen.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Florian Weimer June 4, 2024, 10:56 a.m. UTC | #1
* Andreas Schwab:

> Deallocate the memory for the FILE structure when seeking to the end fails
> in append mode.
>
> Fixes: ea33158c96 ("Fix offset caching for streams and use it for ftell (BZ #16680)")
> ---
>  libio/iofdopen.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/libio/iofdopen.c b/libio/iofdopen.c
> index 2583fb8255..14fbc7b257 100644
> --- a/libio/iofdopen.c
> +++ b/libio/iofdopen.c
> @@ -156,7 +156,11 @@ _IO_new_fdopen (int fd, const char *mode)
>      {
>        off64_t new_pos = _IO_SYSSEEK (&new_f->fp.file, 0, _IO_seek_end);
>        if (new_pos == _IO_pos_BAD && errno != ESPIPE)
> -	return NULL;
> +	{
> +	  _IO_un_link (&new_f->fp);
> +	  free (new_f);
> +	  return NULL;
> +	}
>      }
>    return &new_f->fp.file;
>  }

Patch looks okay.  Can we create a test case for this using mtrace?

Thanks,
Florian
Andreas Schwab June 4, 2024, 11:08 a.m. UTC | #2
On Jun 04 2024, Florian Weimer wrote:

> Patch looks okay.  Can we create a test case for this using mtrace?

It is difficult to create the failing situation as it requires a
seekable file that that does not support seeking to the end.
Florian Weimer June 4, 2024, 12:26 p.m. UTC | #3
* Andreas Schwab:

> On Jun 04 2024, Florian Weimer wrote:
>
>> Patch looks okay.  Can we create a test case for this using mtrace?
>
> It is difficult to create the failing situation as it requires a
> seekable file that that does not support seeking to the end.

We can use /proc/self/mem.  Please check in your fix, I'll post my test.

Reviewed-by: Florian Weimer <fweimer@redhat.com>

Thanks,
Florian
diff mbox series

Patch

diff --git a/libio/iofdopen.c b/libio/iofdopen.c
index 2583fb8255..14fbc7b257 100644
--- a/libio/iofdopen.c
+++ b/libio/iofdopen.c
@@ -156,7 +156,11 @@  _IO_new_fdopen (int fd, const char *mode)
     {
       off64_t new_pos = _IO_SYSSEEK (&new_f->fp.file, 0, _IO_seek_end);
       if (new_pos == _IO_pos_BAD && errno != ESPIPE)
-	return NULL;
+	{
+	  _IO_un_link (&new_f->fp);
+	  free (new_f);
+	  return NULL;
+	}
     }
   return &new_f->fp.file;
 }