]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Annotate dbus_malloc*() functions with __attribute__ malloc and __attribute__ alloc_size
authorCristian Rodríguez <cristian.rodriguez@opensuse.org>
Wed, 29 Dec 2010 00:10:31 +0000 (21:10 -0300)
committerSimon McVittie <smcv@debian.org>
Wed, 5 Jan 2011 13:13:48 +0000 (13:13 +0000)
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 <cristian.rodriguez@opensuse.org>
Reviewed-by: Thiago Macieira <thiago@kde.org>
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=32710

dbus/dbus-macros.h
dbus/dbus-memory.h

index d1e40ecba51e61d6f09d7b80f1bb1ebde5e87237..dcd3eebd3073123eba8523a402736ad12c71bb52 100644 (file)
 #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
  */
index ea28423c5be3e637e8277966b915827d24156b6f..4fd56bd633b4527666ea4bdc92b98fd280ac774d 100644 (file)
@@ -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