From 7e2369456c8c1f11acd8aa0e33d45e6139797883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?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 this improves optimization as well catches buffer overflows at compile time. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit code like: foo = dbus_malloc(2); strcpy(foo, "epicfail"); will emit: "warning ... will always overflow the destination buffer..." Signed-off-by: Cristian Rodríguez --- dbus/dbus-macros.h | 14 ++++++++++++++ dbus/dbus-memory.h | 8 ++++++++ 2 files changed, 22 insertions(+), 0 deletions(-) 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 -- 1.7.3.4