Firewall Wizards mailing list archives

Re: Code reviews [Was: FWTK and smap/smapd]


From: Joseph S D Yao <jsdy () center osis gov>
Date: Tue, 23 Jul 2002 12:52:58 -0400

On Wed, Jul 24, 2002 at 02:35:24AM +1000, Darren Reed wrote:
In some email I received from Joseph S D Yao, sie wrote:
Oh, geez, shouldn't everyone have written [something like] this?

No...

Your parameters are wrong, for a start :-)
They were "char *buf" and "unsigned int buflen".

register char *cp = buf;

Look a little harder.  "buf" is the same as "buf".  I don't know about
"buflen", but I will plead nolo contendere to "buflen" if you plead
guilty to nitpicking.  ;-)

register int i, count = n;

for (;;) {
    i = write(fd, cp, count);
    if (i < 0) {
            perror("...");
            fprintf(stderr, ...);
            break;
    }
    count -= i;
    if (count <= 0) /* yes, should never be <, optionally fuss */
            break;
    cp += i;
    if (i == 0 {
            sleep(1);
            /* should probably count and break if too many */
    }
}

You're using a "for (;;)" where it isn't required - should be
"while (count != 0)", the "if (count <= 0) ..." should be nuked from orbit.

Hmmm.  I should have started the whole thing with "if (n == 0) return;"
as we are not guaranteed that it is non-zero.  Otherwise, I would argue
that if "Say What You Mean" [Joe's First Law of Software Engineering]
is uppermost in your mind, then the "for (;;)" and "if (count <= 0)"
are exactly where they should be.  Once you have written all the bytes,
why execute the rest of the code?

Hanlding write() returning 0 bytes, is, umm, bizarre ?  Carson never
said anything about non-blocking I/O but apparently it was implied.
That's a failure in the specification, I say :-)

No.  It is always possible for a write() to return 0.  If it seems
bizarre, it is because PEOPLE DON'T HANDLE UNEXPECTED CASES PROPERLY.

Carson wanted specific handling of things like EINTR...

And I did that off the top of my head without the specs in front of me,
so I missed that.

You missed the parenthesis where I didn't press the key hard enough so
it was too faint for you to see.  ;-)

-- 
Joe Yao                         jsdy () center osis gov - Joseph S. D. Yao
OSIS Center Systems Support                                     EMT-B
-----------------------------------------------------------------------
   This message is not an official statement of OSIS Center policies.
_______________________________________________
firewall-wizards mailing list
firewall-wizards () honor icsalabs com
http://honor.icsalabs.com/mailman/listinfo/firewall-wizards


Current thread: