From 88e7fbf14bef0688b10b1f50b9c72846a8ecbc91 Mon Sep 17 00:00:00 2001
From: Marcus Meissner <meissner@grape.suse.de>
Date: Mon, 1 Sep 2008 10:27:25 +0200
Subject: [PATCH] attribute alloc_size for newer gccs.
This patch marks up the allocator functions with the
GCC 4.3 (and later) attribute alloc_size.
This will pass the size of allocations of known size
(at compile time) to the overflow checking memcpy/strcpy
and related functions.
Ciao, Marcus
---
avahi-common/gccmacro.h | 9 +++++++++
avahi-common/malloc.h | 20 ++++++++++----------
2 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/avahi-common/gccmacro.h b/avahi-common/gccmacro.h
index 78a09a7..dd49502 100644
|
a
|
b
|
|
| 28 | 28 | |
| 29 | 29 | AVAHI_C_DECL_BEGIN |
| 30 | 30 | |
| | 31 | #if defined(__GNUC__) && (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 3) |
| | 32 | #define AVAHI_GCC_ALLOC_SIZE(x) __attribute__ ((__alloc_size__(x))) |
| | 33 | #define AVAHI_GCC_ALLOC_SIZE2(x,y) __attribute__ ((__alloc_size__(x,y))) |
| | 34 | #else |
| | 35 | /** Macro for usage of GCC's alloc_size attribute */ |
| | 36 | #define AVAHI_GCC_ALLOC_SIZE(x) |
| | 37 | #define AVAHI_GCC_ALLOC_SIZE2(x,y) |
| | 38 | #endif |
| | 39 | |
| 31 | 40 | #if defined(__GNUC__) && (__GNUC__ >= 4) |
| 32 | 41 | #define AVAHI_GCC_SENTINEL __attribute__ ((sentinel)) |
| 33 | 42 | #else |
diff --git a/avahi-common/malloc.h b/avahi-common/malloc.h
index 2430199..511b51c 100644
|
a
|
b
|
|
| 35 | 35 | AVAHI_C_DECL_BEGIN |
| 36 | 36 | |
| 37 | 37 | /** Allocate some memory, just like the libc malloc() */ |
| 38 | | void *avahi_malloc(size_t size); |
| | 38 | void *avahi_malloc(size_t size) AVAHI_GCC_ALLOC_SIZE(1); |
| 39 | 39 | |
| 40 | 40 | /** Similar to avahi_malloc() but set the memory to zero */ |
| 41 | | void *avahi_malloc0(size_t size); |
| | 41 | void *avahi_malloc0(size_t size) AVAHI_GCC_ALLOC_SIZE(1); |
| 42 | 42 | |
| 43 | 43 | /** Free some memory */ |
| 44 | 44 | void avahi_free(void *p); |
| 45 | 45 | |
| 46 | 46 | /** Similar to libc's realloc() */ |
| 47 | | void *avahi_realloc(void *p, size_t size); |
| | 47 | void *avahi_realloc(void *p, size_t size) AVAHI_GCC_ALLOC_SIZE(2); |
| 48 | 48 | |
| 49 | 49 | /** Internal helper for avahi_new() */ |
| 50 | | static inline void* avahi_new_internal(unsigned n, size_t k) { |
| | 50 | static inline void* AVAHI_GCC_ALLOC_SIZE2(1,2) avahi_new_internal(unsigned n, size_t k) { |
| 51 | 51 | assert(n < INT_MAX/k); |
| 52 | 52 | return avahi_malloc(n*k); |
| 53 | 53 | } |
| … |
… |
|
| 56 | 56 | #define avahi_new(type, n) ((type*) avahi_new_internal((n), sizeof(type))) |
| 57 | 57 | |
| 58 | 58 | /** Internal helper for avahi_new0() */ |
| 59 | | static inline void* avahi_new0_internal(unsigned n, size_t k) { |
| | 59 | static inline void* AVAHI_GCC_ALLOC_SIZE2(1,2) avahi_new0_internal(unsigned n, size_t k) { |
| 60 | 60 | assert(n < INT_MAX/k); |
| 61 | 61 | return avahi_malloc0(n*k); |
| 62 | 62 | } |
| … |
… |
|
| 71 | 71 | char *avahi_strndup(const char *s, size_t l); |
| 72 | 72 | |
| 73 | 73 | /** Duplicate the given memory block into a new one allocated with avahi_malloc() */ |
| 74 | | void *avahi_memdup(const void *s, size_t l); |
| | 74 | void *avahi_memdup(const void *s, size_t l) AVAHI_GCC_ALLOC_SIZE(2); |
| 75 | 75 | |
| 76 | 76 | /** Wraps allocator functions */ |
| 77 | 77 | typedef struct AvahiAllocator { |
| 78 | | void* (*malloc)(size_t size); |
| 79 | | void (*free)(void *p); |
| 80 | | void* (*realloc)(void *p, size_t size); |
| 81 | | void* (*calloc)(size_t nmemb, size_t size); /**< May be NULL */ |
| | 78 | void* (*malloc)(size_t size) AVAHI_GCC_ALLOC_SIZE(1); |
| | 79 | void (*free)(void *p); |
| | 80 | void* (*realloc)(void *p, size_t size) AVAHI_GCC_ALLOC_SIZE(2); |
| | 81 | void* (*calloc)(size_t nmemb, size_t size) AVAHI_GCC_ALLOC_SIZE2(1,2); /**< May be NULL */ |
| 82 | 82 | } AvahiAllocator; |
| 83 | 83 | |
| 84 | 84 | /** Change the allocator. May be NULL to return to default (libc) |