Another nice piece of code made by
ESR you know, the guy who writes books about "The art of UNIX programming" for example
From sink.c:
if (strcmp(ctl->bsmtp, "-") == 0)
sinkfp = stdout;
else
sinkfp = fopen(ctl->bsmtp, "a");
/* see the ap computation under the SMTP branch */
need_anglebrs = (msg->return_path[0] != '<');
fprintf(sinkfp,
"MAIL FROM:%s%s%s",
need_anglebrs ? "<" : "",
(msg->return_path[0]) ? msg->return_path : user,
need_anglebrs ? ">" : "");
/* DO SOME UNIMPORTANT STUFF HERE aka ...... CODE STRIPPED */
fputs("DATA\r\n", sinkfp);
if (ferror(sinkfp))
{
report(stderr, GT_("BSMTP file open or preamble write failed\n"));
return(PS_BSMTP);
}
This code will
segfault if the fopen call in line 4 fails, sinkfp will become NULL then which leads
to a segfault when writing more than once into it with fprintf/fputs.
What I find funny now is the check in line 17 to test the error indicator for the stream pointed to by sinkfp AFTER actually writing into it.
But what is even more funny is that also the check would segfault in this case since ferror will if it gets a NULL pointer.