mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Maxim Storchak <m.storchak@gmail.com>
To: musl@lists.openwall.com
Subject: open_memstream corner case
Date: Tue, 6 Oct 2015 16:52:58 +0300	[thread overview]
Message-ID: <5613D23A.3010707@gmail.com> (raw)

Hi,

I discovered something strange with memstream subsystem in musl: if a
stream is opened for writing with open_memstream(3) but then closed
without writing anything, neither buffer pointer, nor buffer size gets
updated. I compared source code of glibc, uClibc and musl and discovered
that both uClibc and musl update buffer size on write, while glibc does
that on fclose or fsync, both of which is fine, according to
memstream(3) man page. While both uClibc and musl behave the same if
something is written to the buffer, they differ if no write happens. On
initialization uClibc sets buffer size to 0, and musl leaves it intact.
In case of no write it doesn't get updated and contains garbage. Here is
my test case:

#include <stdio.h>
#include <stdlib.h>
int main() {
        FILE *f;
        size_t size=-42; /* garbage */
        char *buf="garbage";
        printf("Initially: size=%ld, buf=%s\n", size, buf);
        if ( (f=open_memstream(&buf, &size)) == NULL ) {
                perror("open_memstream");
                exit(0);
        }
        fclose(f);
        printf("After writing nothing to buffer: size=%ld, buf=%s\n",
size, buf);
        if ( (f=open_memstream(&buf, &size)) == NULL ) {
                perror("open_memstream");
                exit(0);
        }
        fprintf(f, "something completely different");
        fclose(f);
        printf("After writing something to buffer: size=%ld, buf=%s\n",
size, buf);
        exit(0);
}

musl:
Initially: size=-42, buf=garbage
After writing nothing to buffer: size=-42, buf=garbage
After writing something to buffer: size=30, buf=something completely
different

glibc:
Initially: size=-42, buf=garbage
After writing nothing to buffer: size=0, buf=
After writing something to buffer: size=30, buf=something completely
different

(I'm sorry for providing no example for uClibc, but the program where I
found that use case used to work with uClibc but not musl).

Could anyone please comment if where's a bug in musl or in my test case?

-- 
Best regards,
Maxim Storchak
mailto:m.storchak@gmail.com


             reply	other threads:[~2015-10-06 13:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-06 13:52 Maxim Storchak [this message]
2015-10-08 22:12 ` Rich Felker
2015-10-13  8:43   ` Maxim Storchak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5613D23A.3010707@gmail.com \
    --to=m.storchak@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).