Skip to content

Commit 2808c0f

Browse files
committed
fmemopen: allow reading NUL bytes
Previously a memory stream opened in 'r' mode would return EOF once it encounters the first NUL byte but this is not correct according to the POSIX man page. Now that we track size (occurrence of first NUL byte in a R/W buffer) and bufsize separately we can return EOF once we hit the current size on reads rather than checking for NUL.
1 parent 0b2fa8f commit 2808c0f

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

newlib/libc/tinystdio/fmemopen.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,13 @@ static int
7373
__fmem_get(FILE *f)
7474
{
7575
struct __file_mem *mf = (struct __file_mem *)f;
76-
int c;
77-
if ((f->flags & __SRD) && mf->pos < mf->size) {
78-
c = (unsigned char)mf->buf[mf->pos++];
79-
if (c == '\0')
80-
c = _FDEV_EOF;
81-
} else
82-
c = _FDEV_ERR;
83-
return c;
76+
if ((f->flags & __SRD) == 0) {
77+
return _FDEV_ERR;
78+
} else if (mf->pos < mf->size) {
79+
return (unsigned char)mf->buf[mf->pos++];
80+
} else {
81+
return _FDEV_EOF;
82+
}
8483
}
8584

8685
static int

0 commit comments

Comments
 (0)