mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: Possible file stream bug
Date: Wed, 24 Oct 2012 16:59:05 -0400	[thread overview]
Message-ID: <20121024205904.GJ254@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAKHv7pj-WNCe5sY+krSYNbLnvZQonKTceyY2b3N=6U1HJ6PXxQ@mail.gmail.com>

On Wed, Oct 24, 2012 at 09:36:28PM +0200, Paul Schutte wrote:
> Hi
> 
> I compiled and linked libwebserver-0.5.3 against musl.
> 
> It would just strangely break halfway through a request. After hours of
> searching, I found the problem.
> 
> I can demonstrate it with the following code:
> 
> #include <unistd.h>
> #include <stdio.h>
> 
> int main() {
>     FILE *fstream;
> 
>     fclose(stdout);
> 
>     fstream=freopen("/dev/tty","w",stdout);
> 
>     if (fstream==NULL) {
>         fprintf(stderr,"freopen failed\n");
>     }
> 
>     printf("test this\n");
> 
> 
>     return 0;
> }
> 
> This snippet works fine when using glibc.

This code is invalid; you have invoked undefined behavior by accessing
stdout (passing it to freopen) after it was closed with fclose. Remove
the fclose and it works correctly. This code would certainly cause
memory corruption and/or crash if it were used on any FILE other than
one of the 3 builtin ones (since the memory would have been returned
to the heap when fclose was called) so there is no sense in trying to
support this invalid usage. You should file a bug report with the
application.

Rich


  parent reply	other threads:[~2012-10-24 20:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-24 19:36 Paul Schutte
2012-10-24 20:16 ` Paul Schutte
2012-10-24 20:51   ` Rich Felker
2012-10-24 20:59 ` Rich Felker [this message]
2012-10-24 21:22   ` Paul Schutte
2012-10-24 21:25     ` Rich Felker
2012-10-24 21:54       ` Paul Schutte
2012-10-24 21:53         ` Rich Felker
2012-10-26 21:57         ` Rich Felker
2012-10-24 21:41     ` Szabolcs Nagy
2012-10-24 21:52       ` Rich Felker

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=20121024205904.GJ254@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --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).