Site hosted by Build your free website today!
[^^mac]  [Main TECH page]  

Evolving Pains

For this next trick i wish to show that a program that had been used for YEARS had a hidden flaw. It was simple function that Input a string from the console. In order to make it "foolproof" a number of checks were put into it. And so it went. Then it started crashing - but only with a certain new proogram. The catch was that you would call the function: Input_string (Junk3); And it would return what the user had typed in via the return variable Junk3 (char *) and nicely terminated by a \0. Except of course you have to allocte Junk3 first; eg, Junk3 = (char *) malloc (STRING_SIZE); *Junk3 = '\0'; What happens if Junk3 isn't allocated? As it turns out, in most cases, it uses the NEXT allocated space. And that (in most cases was Junk4) - which was almost never used anyway - so who cared? (or more sinisterly, who knew? Murphy natch). So, in a new program the string wasn't allocated and it was a new variable (eg, Working_buffer). Of course, a new addition void Input_string (char *Xs) { if (Xs == NULL) { printf ("*** ERR: Un-allocated string passed to Input_string\n"); exit; } Of course some comilers don't initialise strings to NULL. And on it goes!