From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/2193 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Possible file stream bug Date: Wed, 24 Oct 2012 16:59:05 -0400 Message-ID: <20121024205904.GJ254@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1351112872 30528 80.91.229.3 (24 Oct 2012 21:07:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 24 Oct 2012 21:07:52 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-2194-gllmg-musl=m.gmane.org@lists.openwall.com Wed Oct 24 23:08:01 2012 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1TR8B8-0002nC-UF for gllmg-musl@plane.gmane.org; Wed, 24 Oct 2012 23:07:55 +0200 Original-Received: (qmail 29808 invoked by uid 550); 24 Oct 2012 21:07:47 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 29800 invoked from network); 24 Oct 2012 21:07:46 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:2193 Archived-At: 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 > #include > > 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