From e2be7b92a6d46f02d8d682dd9e51bb494f41e92c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Cristian=20Rodr=C3=ADguez?= Date: Tue, 28 Dec 2010 21:10:31 -0300 Subject: [PATCH] Annotate dbus_malloc*() functions with __attribute__ malloc and __attribute__ alloc_size MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- dbus/dbus-macros.h | 14 ++++++++++++++ dbus/dbus-memory.h | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/dbus/dbus-macros.h b/dbus/dbus-macros.h index d1e40ec..dcd3eeb 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 ea28423..4fd56bd 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 -- 2.7.4