make fmemopen's w+ mode truncate the buffer
authorRich Felker <dalias@aerifal.cx>
Tue, 28 Aug 2018 23:16:40 +0000 (19:16 -0400)
committerRich Felker <dalias@aerifal.cx>
Tue, 28 Aug 2018 23:16:40 +0000 (19:16 -0400)
the w+ mode is specified to "truncate the buffer contents". like most
of fmemopen, exactly what this means is underspecified. mode w and w+
of course implicitly 'truncate' the buffer if a write from the initial
position is flushed, so in order for this part of the text about w+
not to be spurious, it should be interpreted as requiring something
else, and the obvious reasonable interpretation is that the truncation
is immediately visible if you attempt to read from the stream or the
buffer before writing/flushing.

this interpretation agrees with reported conformance test failures.

src/stdio/fmemopen.c

index fb2656e3ad19d16d623b9be3f1e5b49a1ef2533b..5e0eeb50d44865398f90f39cf5416282d3b8b0fe 100644 (file)
@@ -112,6 +112,7 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
        if (!plus) f->f.flags = (*mode == 'r') ? F_NOWR : F_NORD;
        if (*mode == 'r') f->c.len = size;
        else if (*mode == 'a') f->c.len = f->c.pos = strnlen(buf, size);
+       else if (plus) *f->c.buf = 0;
 
        f->f.read = mread;
        f->f.write = mwrite;