[PATCH] xkb: when copying the keymap, make sure the structs default to 0/NULL.

Daniel Stone daniel at fooishbar.org
Thu Feb 7 02:09:45 PST 2008


On Thu, Feb 07, 2008 at 08:29:48PM +1030, Peter Hutterer wrote:
> struct *foo = xalloc(just the right size);
> foo->num_bar = 0;
>
> // blah blah
>
> if (foo->num_bar)
> {
> 	// happy happy picnic
> }
> xfree(foo->bar); // Yay! Nirvana! Finally!

So, geometry has those semantics (having very obviously been written by
a completely different person than the rest of XKB), but most of it has
these semantics:

foo->bar = xalloc(6 * sizeof(despair));
foo->sz_bar = 6;
foo->num_bar = 0;

if (foo->num_bar) {
    /* picnics ensue, bunnies and koalas live in happy unison */
}

[...]

if (foo->sz_bar)
    tmp = xrealloc(failure * sizeof(despair)); /* obviously a valid
                                                  pointer if sz_bar is set */
else
    tmp = xalloc(failure * sizeof(despair));
if (!tmp)
    /* i've fallen and i can't get up */

foo->bar = tmp;
foo->sz_bar = failure;

[...]

xfree(foo->bar); /* OH GOD IT BURNS WHY DO YOU HURT ME SO */
foo->sz_bar = 0; /* i'm so glad we have sz_bar around, not superfluous
                    at all */
foo->num_bar = 0;

HTH,
Daniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.x.org/archives/xorg/attachments/20080207/4121b996/attachment.pgp>


More information about the xorg mailing list