From: Cristian Rodríguez Date: Wed, 29 Dec 2010 00:10:31 +0000 (-0300) Subject: Annotate dbus_malloc*() functions with __attribute__ malloc and __attribute__ alloc_size X-Git-Tag: dbus-1.4.4~47 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e2be7b92a6d46f02d8d682dd9e51bb494f41e92c;p=thirdparty%2Fdbus.git Annotate dbus_malloc*() functions with __attribute__ malloc and __attribute__ alloc_size This improves optimization as well catches buffer overflows at compile time. code like: foo = dbus_malloc(2); strcpy(foo, "epicfail"); will emit: "warning ... will always overflow the destination buffer..." Signed-off-by: Cristian Rodríguez Reviewed-by: Thiago Macieira Reviewed-by: Simon McVittie Bug: https://bugs.freedesktop.org/show_bug.cgi?id=32710 --- diff --git a/dbus/dbus-macros.h b/dbus/dbus-macros.h index d1e40ecba..dcd3eebd3 100644 --- a/dbus/dbus-macros.h +++ b/dbus/dbus-macros.h @@ -74,6 +74,20 @@ #define _DBUS_GNUC_NORETURN #endif /* !__GNUC__ */ +#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) +#define DBUS_MALLOC __attribute__((__malloc__)) +#else +#define DBUS_MALLOC +#endif + +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#define DBUS_ALLOC_SIZE(x) __attribute__((__alloc_size__(x))) +#define DBUS_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y))) +#else +#define DBUS_ALLOC_SIZE(x) +#define DBUS_ALLOC_SIZE2(x,y) +#endif + /** @def _DBUS_GNUC_PRINTF * used to tell gcc about printf format strings */ diff --git a/dbus/dbus-memory.h b/dbus/dbus-memory.h index ea28423c5..4fd56bd63 100644 --- a/dbus/dbus-memory.h +++ b/dbus/dbus-memory.h @@ -38,10 +38,18 @@ DBUS_BEGIN_DECLS */ DBUS_EXPORT +DBUS_MALLOC +DBUS_ALLOC_SIZE(1) void* dbus_malloc (size_t bytes); + DBUS_EXPORT +DBUS_MALLOC +DBUS_ALLOC_SIZE(1) void* dbus_malloc0 (size_t bytes); + DBUS_EXPORT +DBUS_MALLOC +DBUS_ALLOC_SIZE(2) void* dbus_realloc (void *memory, size_t bytes); DBUS_EXPORT