Re: bglibs-1.022/str/alloc.c comment

Bruce Guenter <[email protected]>
Newsgroups gmane.comp.sysutils.bgware
Message-ID <[email protected]>
On Tue, Jun 21, 2005 at 04:58:19PM +0200, Uffe Jakobsen wrote:
> bglibs-1.022/str/alloc.c (line 32-36) states:
> ---
> 32: int str_alloc(str* s, unsigned size, int copy)
> 33: {
> 34:   char* news;
> 35:   unsigned newsize;
> 36:  if ((newsize = size + 1) < size) return 0;
> ---
> 
> I wonder if line 36 should have been this instead or ???
> 
> 36:  if ((newsize = size + 1) < s->size) return 0;

This comparison is done to protect against additive overflow.  That is,
trying to allocate a string whose size would overflow an unsigned
integer will fail instead of producing bugs.

> Anyway with the above construct it doesn't seem possible to resize a string 
> to something less than the actual size. It that correct ?

The str library is set up to always grow, never shrink, yes.  The only
way to shrink would be to free the string (which resets the size
parameter) and then reallocate it.
-- 
Bruce Guenter <[email protected]> http://em.ca/~bruceg/ http://untroubled.org/
OpenPGP key: 699980E8 / D0B7 C8DD 365D A395 29DA  2E2A E96F B2DC 6999 80E8
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFCuDHF6W+y3GmZgOgRArktAJ9tEeAfvMQbQZj/8bI/IMwi6HDQFQCgkaJN
VaxDRTkn4RpflC9ynJ5zlsA=
=/RDL
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.