summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-09-04 21:53:20 -0400
committerRich Felker <dalias@aerifal.cx>2011-09-04 21:53:20 -0400
commitd2e061a2bd3f7674cfef2e2217e0695419041b5e (patch)
tree282e26dcafdfcae964a892f27bcba9e529b3da6b /src
parente72ee5786b1f328da131b87388333c2e3a09b7b3 (diff)
downloadmusl-d2e061a2bd3f7674cfef2e2217e0695419041b5e.tar.gz
more fmemopen null termination fixes
null termination is only added when current size grows. in update modes, null termination is not added if it does not fit (i.e. it is not allowed to clobber data). these rules make very little sense, but that's how it goes..
Diffstat (limited to 'src')
-rw-r--r--src/stdio/fmemopen.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/stdio/fmemopen.c b/src/stdio/fmemopen.c
index 260d2889..1b054a97 100644
--- a/src/stdio/fmemopen.c
+++ b/src/stdio/fmemopen.c
@@ -54,9 +54,10 @@ static size_t mwrite(FILE *f, const unsigned char *buf, size_t len)
if (len > rem) len = rem;
memcpy(c->buf+c->pos, buf, len);
c->pos += len;
- if (c->pos >= c->len) {
+ if (c->pos > c->len) {
c->len = c->pos;
- c->buf[c->len==c->size ? c->len-1 : c->len] = 0;
+ if (c->len < c->size) c->buf[c->len] = 0;
+ else if ((f->flags&F_NORD) && c->size) c->buf[c->size-1] = 0;
}
return len;
}