Subject: Re: [PATCH 1/2] MSVC: Do not close stdout to prevent a crash

From: Vincent van Ravesteijn <[email-masked]>
Date: Sat, 19 Nov 2011 21:52:29 +0100

Op 19-11-2011 20:11, Junio C Hamano schreef:
> ... This happens for 'format-patch' which closes stdout after a call to
> freopen which directs stdout to the format patch file.
>> It shouldn't do that in the first place.  This is an error on any
>> platform.
> Correct. The clean-up codepath is for built-in command implementations
> that write out their result and return 0 to signal success. If we let the
> crt0 to run its usual clean-ups like closing the standard output stream,
> we wouldn't be able to catch errors from there.
>
> For built-ins that perform their own clean-ups, it is their responsibility
> to be careful, hence we skip this part of the code.
>
> We have relied on fstat(-1,&st) to correctly error out, and if MSVC build
> crashes, it is a bug in its fstat() emulation, I would think.

This doesn't work because fileno(stdout) returns 1 no matter whether we 
closed stdout or not.

My reasoning for the proposed patch is that we call freopen(..., stdout) 
for each patch file to write, but we only call fclose(stdout) once. Sp, 
I concluded that it is not necessary to close stdout and that we just 
need to redirect any new output to stdout.

Vincent