This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] abort: Only flush file-based stdio streams before termination


On 08/18/2017 03:45 PM, Carlos O'Donell wrote:

> Do we have a technical objection to flushing the open memory streams?

abort needs to be async-signal-safe and our current implementation of
open_memstream allocates on flush, for example if exactly 8192 bytes
have been written:

#include <assert.h>
#include <dlfcn.h>
#include <err.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static volatile int to_write;
static volatile bool pending_flush;
static volatile bool malloc_called;

void *
malloc (size_t sz)
{
  malloc_called = true;
  if (pending_flush)
    {
      pending_flush = false;
      printf ("malloc called for size %d\n", to_write);
    }
  void *(*next) (size_t) = dlsym (RTLD_NEXT, "malloc");
  return next (sz);
}

int
main (int argc, char **argv)
{
  to_write = atoi (argv[1]);
  char *data = malloc (to_write);
  assert (malloc_called);
  if (data == NULL)
    err (1, "malloc");
  memset (data, 'X', to_write);

  char *buffer = NULL;
  size_t length = 0;
  FILE *fp = open_memstream (&buffer, &length);
  if (fwrite (data, to_write, 1, fp) != 1)
    err (1, "fwrite");

  pending_flush = true;
  fflush (fp);
}

$ ./memstream-alloc 8192
malloc called for size 8192

Thanks,
Florian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]