Skip to content

Commit 208f19d

Browse files
committed
fmemopen: add a NUL byte when writing beyond current buffer end
This behaviour is not currently mandated by the POSIX spec, but is what glibc does and will be part of the next POSIX standard according to https://austingroupbugs.net/view.php?id=657#c6535
1 parent 3dae86e commit 208f19d

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

newlib/libc/tinystdio/fmemopen.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ __fmem_put(char c, FILE *f)
6262
mf->buf[mf->pos++] = c;
6363
if (mf->pos > mf->size) {
6464
mf->size = mf->pos;
65+
/* When a stream open for update (the mode argument includes '+') or
66+
* for writing only is successfully written and the write advances
67+
* the current buffer end position, a null byte shall be written at
68+
* the new buffer end position if it fits. */
69+
if (mf->size < mf->bufsize) {
70+
mf->buf[mf->size] = '\0';
71+
}
6572
}
6673
return (unsigned char)c;
6774
} else {

0 commit comments

Comments
 (0)