[JDEV] FIX in io.c (TOM do DUMMY..;-P )

Thomas Charron tcharron at nermail.ups.com
Tue Feb 23 13:21:48 CST 1999


(Resent due to recessive stupid genes temporarily taking over control)
(ReRead the source snippet ;-P )

	I just stumbled across a comment in io.c in the lib/common directory..

/* There has got to be a better way to do this! */
old = c->buff;
c->buff = malloc(strlen(c->buff) + strlen(buffer) + 1);
c->buff[0] = '\0';
strcpy(c->buff, old);
free(old);
strcat(c->buff, buffer);

	There is a better way.. ;-P (Note the change in the first line over first
message)

c->buff = realloc(c->buff, strlen(c->buff) + strlen(buffer) + 1);
strcat(c->buff, buffer);

	MUCH prettier, isn't it??  ;-P  Under Unix do a man malloc, it explains
realloc fairly well..  realloc does exactly what you where doing, but in a
much less overhead way..  The only time that this would be REALLY BAD is if
realloc fails, c->buff is now NULL, but heck, your initial source didn't
check for the failure, so why should mine..  ;-P  It really should be
checked, though..  At least send a message of some sort of a bad error to
the client, and drop the connection..

	Actually, one COULD hypothetically crash the server this way..  On a
machine with 32 megs free, send 33 megs to the socket..  Eventually, becouse
of the above malloc or realloc routines, 'KABOOM!!'  ;-P

--
Thomas Charron
United Parcel Service
Northeast Region
IE Software Developer
"Moving at the speed of a T3 Trunk Line!"




More information about the JDev mailing list