Expert C Programming

(Jeff_L) #1

Sins of Omission


The "sins of omission" category covers things that the language doesn't do that it should. This includes
missing features like standard argument processing and the mistake of extracting lint checking from
the compiler.


Mail Won't Go to Users with an "f" in Their User names


The bug report was very puzzling. It just said "mail isn't getting delivered to users who have an 'f' as
the second character of their username." It sounded so unlikely. What could possibly cause mail to fail
because of a character in the username? After all, there's no connection between the characters in a
username and the mail delivery processing. Nonetheless, the problem was reported at multiple sites.


After some urgent testing, we found that mail was indeed falling into the void when an addressee had
an "f" as the second character of the username! Thus, mail would go to Fred and Muffy, but not to
Effie. An examination of the source code quickly located the trouble.


Many people are surprised to learn that ANSI C mandates the argc, argv convention of passing


arguments to a C program, but it does. The UNIX convention has been elevated to the level of a
standard, and it was partly to blame for the mail bug here. The mail program had been amended in the
previous release to:


if ( argv[argc-1][0] == '-' || (argv[argc-2][1] == 'f' ) )


readmail(argc, argv);


else


sendmail(argc, argv);


The "mail" program can be executed either to send mail, or to read your incoming mail. We won't
enquire too closely into the merits of making one program responsible for two such different tasks.
This code was supposed to look at the arguments and use the information to decide if we are reading
mail or sending mail. The way to distinguish is somewhat heuristic: look for switches that are unique
to either reading or sending. In this case, if the final argument is a switch (i.e., starts with a hyphen),
we are definitely reading mail. We are also reading mail if the last argument is not an option but is a


filename, that is, the next-to-last argument was "-f".


And this is where the programmer went wrong, aided by lack of support in the language. The
programmer merely looked at the second character of the next-to-last option. If it was an "f", he
assumed that mail was invoked with a line like:


mail -h -d -f /usr/linden/mymailbox


In most cases this was correct, and mail would be read from mymailbox. But it could also happen that
the invocation was:


mail effie robert

Free download pdf