[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



Ya know, it's strange but when I wrote this code(last summer) I tried
using realloc but couldn't get it to work at all, but I was learning C at
the time and was probably doing something wrong which I'm not remembering.

The code below is going away anyway, but I'll try using realloc in other
places again :)

Jer

On Tue, 23 Feb 1999, Thomas Charron wrote:

> (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!"
>