David Laight wrote on Wed, Nov 16, 2022 at 09:51:50AM +0000:
> > +if (fclose(G.nonstdout)) {
> > +xfunc_error_retval = 4; /* It's what gnu sed exits with... */
> > +bb_simple_error_msg_and_die(bb_msg_write_error);
> > +}
> > G.nonstdout = stdout;
>
> Does that report an error if there is nothing currently buffered?
hmm, I never use the file interface so not quite sure, looking a bit I
guess that's libc dependant?
On glibc it looks like fclose will catch any error flag, but on musl
from what I'm following the fflush implied in fclose will only catch the
error if there haven't been another error before (because wpos=wbase=0
already so it won't try to write), and the close call itself doesn't
care so it just happened to work because there was data buffered as
you've pointed out.
> I think the best sequence is:
>
> fflush(G.nonstdout);
> if (ferror(G.nonstdout)) {
> xfunc_error_retval = 4; /* It's what gnu sed exits with... */
> bb_simple_error_msg_and_die(bb_msg_write_error);
> }
> fclose(G.nonstdout);
> G.nonstdout = stdout;
This looked safe to me, yes.
I'll send a v2 with this in a minute.
--
Dominique
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox