Reserving Space on Disk

Conor P. Cahill cpcahil at virtech.uucp
Mon Jul 16 07:16:08 AEST 1990


In article <563 at hhb.UUCP> istvan at hhb.UUCP (Istvan Mohos) writes:
>    if (write (fd, malloc(FOURMEG), FOURMEG) != FOURMEG)
>        perror("malloc or write error"), exit (1);

While I know this is just an example & is not intended to be a 
suggestion of what to do, I just have to comment on it to ensure 
that some impressionable engineer doesn't think this is a 
good thing to do.

Anyway, my comment is never (not even in an example (unless it is
an example of what not to do)) ever use the return of any function
that may return NULL before you check it to see if it returned NULL.

Your perror() seems to indicated that if the malloc had failed, the write
would return -1 and set errno accordingly.  In fact, the malloc failure
would return a NULL pointer and then write would try to write out 4MB
from that location.  On most machines this would result in a
core dump, either because NULL was not a valid address, or because
there was not 4MB in the user's address space following that 
pointer.

I'm not harping on you.  I just feel that I must emphasize this
point.  Too often I see code that does something like the above, 
although getenv() is more often the culprit.


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 



More information about the Comp.unix.wizards mailing list