bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add 'safe-alloc' module


From: David Lutterkort
Subject: [PATCH] Add 'safe-alloc' module
Date: Thu, 19 Feb 2009 14:00:55 -0800

Hi,

attached is a patch for a new module 'safe-alloc'. The module originated
with libvirt[1] and was originally written by Dan Berrange. I touched it
up for inclusion into gnulib. I've found them useful for some of my
projects, and thought that putting them into gnulib would be much better
than simply copying files around.

The module provides a few macros that help avoid common mistakes and
annoyances when using 'raw' malloc and friends. A fuller explanation
from the commit message for the patch:

These macros address a number of shortcomings in the usual C library
allocation functions:
  * Return value and error indication are conflated, making compile
    time checks for proper error checking (atribute __warn_unused_result__)
    useless
  * It is easy to allocate the wrong number of bytes, even though
    the compiler knows or could at least help in allocating the right
    number
  * Use of uninitialized memory (using malloc instead of calloc)
  * Double frees
  * Incorrect handling of the pointer for realloc, leading either to
    memory leaks or referencing invalid memory

A more complete explanation of the above can be found in the post
  "safer memory allocation APIs with compile time checking"
at
  http://www.redhat.com/archives/libvir-list/2008-April/msg00372.html

Using this module still allows using malloc/calloc/realloc directly, but
it offers an alternative that makes committing the above errors at least
harder, if not impossible.

David

[1] http://libvirt.org/

Attachment: safe-alloc.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]