OSEC

Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
 
Subject: Re: Here's another glibc env. var.
From: David Luyer (david_luyerpacific.net.au)
Date: Thu May 25 2000 - 10:13:54 CDT


> PS A reference on netstrings: <http://cr.yp.to/proto/netstrings.txt>.

The example in that document calls malloc(len+1) and then writes len bytes
into it. It would be much nicer if there was a buf[len] = '\0' because
people may assume that code fragment produces a null-terminated string
based on running it a few times. It doesn't. Apart from that there
could be a null in the middle - which isn't really important if you know
the locale and are willing to accept a null to indicate termination, and
much more importantly, malloc() isn't guaranteed to return memory which is
zero even though it quite often will.

If the intent of malloc(len+1) is as the comment reads then malloc(len?:1)
would do the same without the trap for inexperienced coders.

[or for -Wall -ansi -pedantic to be happy: len?len:1]

David.

-- 
----------------------------------------------
David Luyer
Senior Network Engineer
Pacific Internet (Aust) Pty Ltd
Phone:  +61 3 9674 7525
Fax:    +61 3 9699 8693
Mobile: +61 4 1064 2258, +61 4 1114 2258
http://www.pacific.net.au        NASDAQ: PCNTF
<< fast 'n easy >>
----------------------------------------------