NEVER USE return str_dup(s); !!!

From: Katzlberger Thomas (cat@garfield.Vanderbilt.Edu)
Date: 09/15/95


Hi !

>Btw, I know I can 'return "string"' in order to return a string from a 
>char* function, but if I have a local buffer, how would I return a  
string
>in the same manner without returning the address of the local buffer?

> Maybe str_dup()? (its defined in utils.c)

Don't even think about doing that !!!!!!!!!!!!
This is the classical example of a memory leak !

Everytime you pass this point you will have some bytes of
memory less and some garbage more in memory until you crash.

Use this instead:

foo()
{
    static str[20];
    strcpy(str,"YEA!");
    return str;
}

But if you would do that in circle like static buf[8192];
Everytime you do it with gcc it makes your executeable 8KB
bigger and it will allocate e.g: 100 times 8KB instead of
3 times as in the current version of circle. Which really
doesn't seem to be a big deal since circle isn't too much
concerned about memory efficiency.

If you again use buffers that just fit, you are likely to
write out of bounds and crash.

Cat.
--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/_/                                                            _/_/
_/_/  Thomas Katzlberger                                        _/_/
_/_/  katzlbt@vuse.vanderbilt.edu                               _/_/
_/_/  @aBlackNeXT.called.garfield                               _/_/
_/_/  http://www.vuse.vanderbilt.edu/~katzlbt/                  _/_/
_/_/                                                            _/_/
_/_/  "You can tune a file system, but you can't tune a fish."  _/_/
_/_/                                UNIX man page for tunefs.   _/_/
_/_/                                                            _/_/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/



This archive was generated by hypermail 2b30 : 12/07/00 PST