This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] libio: Fix open_memstream flush (NULL)
- From: Florian Weimer <fw at deneb dot enyo dot de>
- To: Adhemerval Zanella <adhemerval dot zanella at linaro dot org>
- Cc: libc-alpha at sourceware dot org
- Date: Wed, 12 Jul 2017 01:40:48 +0200
- Subject: Re: [PATCH] libio: Fix open_memstream flush (NULL)
- Authentication-results: sourceware.org; auth=none
- References: <1499703585-17933-1-git-send-email-adhemerval.zanella@linaro.org>
* Adhemerval Zanella:
> POSIX specifies that the state and size after a fflush on a stream
> opened by open_memstream should be updated and current implementation
> does not act accordingly on a fflush (NULL) (meant to act on all opened
> streams).
>
> This patch fixes it for open_{w}memstream and also adjust the
> tst-memstream3 to use libsupport.
Subject is wrong, it should be fflush.
Please put the test case into a new file, and change it to call fflush
(NULL); it currently calls fflush (fp). As a result, it does not
actually test the change.
You also need to verify that this does not call malloc:
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
int
main (void)
{
char * buffer = NULL;
size_t length = 0;
FILE *fp = open_memstream (&buffer, &length);
if (fp == NULL)
err (1, "open_memstream");
abort ();
}
I think we don't want the malloc call for a call to exit, either.
Similarly for __stack_chk_fail, but that's something we need to fix
there.
> +static int
> +_IO_mem_overflow (_IO_FILE *fp, int c)
> +{
> + int ret = _IO_str_overflow (fp, c);
> +
> + struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
> + *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
> +
> + return ret;
> +}
_IO_str_overflow does not correctly process an EOF argument, but
_IO_flush_all_lockp calls the overflow function with EOF. This is the
reason why the test with fflush (NULL) fails.
The most direct fix appears to be to call _IO_mem_sync for c == EOF.
Let me suggest again that libio changes are not appropriate during the
freeze.