From 7cc28327c1bebf85dcae988c12e6a5b19d01d973 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 19 Jul 2015 12:44:59 +0200
Subject: [PATCH 1/2] Add __malloclike (for __attribute__((malloc))) and mark
 functions.

---
 include/stdlib.h | 6 +++---
 include/string.h | 4 ++--
 include/wchar.h  | 2 +-
 sys/sys/cdefs.h  | 2 ++
 sys/sys/malloc.h | 6 +++---
 5 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/include/stdlib.h b/include/stdlib.h
index f7fd367..8420337 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -90,7 +90,7 @@ int	 atoi(const char *);
 long	 atol(const char *);
 void	*bsearch(const void *, const void *, size_t,
 		 size_t, int (*)(const void *, const void *));
-void	*calloc(size_t, size_t) __heedresult;
+void	*calloc(size_t, size_t) __heedresult __malloclike;
 div_t	 div(int, int) __pure2;
 void	 exit(int) __dead2;
 void	 free(void *);
@@ -99,7 +99,7 @@ char	*getenv(const char *);
 long	 labs(long) __pure2;
 #endif
 ldiv_t	 ldiv(long, long) __pure2;
-void	*malloc(size_t) __heedresult;
+void	*malloc(size_t) __heedresult __malloclike;
 int	 posix_memalign(void **, size_t, size_t);
 int	 mblen(const char *, size_t);
 size_t	 mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
@@ -316,7 +316,7 @@ extern char *suboptarg;			/* getsubopt(3) external variable */
 int 	at_quick_exit(void (*func)(void));
 _Noreturn void
 	quick_exit(int);
-void	*aligned_alloc(size_t, size_t) __heedresult;
+void	*aligned_alloc(size_t, size_t) __heedresult __malloclike;
 #endif /* __ISO_C_VISIBLE >= 2011 */
 
 __END_DECLS
diff --git a/include/string.h b/include/string.h
index b97f288..29b5f91 100644
--- a/include/string.h
+++ b/include/string.h
@@ -91,7 +91,7 @@ char	*strcpy(char * __restrict, const char * __restrict);
 #endif
 size_t	 strcspn(const char *, const char *) __pure;
 #if __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE
-char	*strdup(const char *);
+char	*strdup(const char *) __malloclike;
 #endif
 char	*strerror(int);
 #if __POSIX_VISIBLE >= 200112
@@ -113,7 +113,7 @@ int	 strncmp(const char *, const char *, size_t) __pure;
 char	*strncpy(char * __restrict, const char * __restrict, size_t);
 #endif
 #if __POSIX_VISIBLE >= 200809
-char	*strndup(const char *, size_t);
+char	*strndup(const char *, size_t) __malloclike;
 #if !defined(_KERNEL_VIRTUAL)
 size_t	 strnlen(const char *, size_t) __pure;
 #endif
diff --git a/include/wchar.h b/include/wchar.h
index 25c0fa5..550a5ca 100644
--- a/include/wchar.h
+++ b/include/wchar.h
@@ -249,7 +249,7 @@ size_t	mbsnrtowcs(wchar_t * __restrict, const char ** __restrict, size_t,
 FILE	*open_wmemstream(wchar_t **, size_t *);
 wchar_t	*wcpcpy(wchar_t * __restrict, const wchar_t * __restrict);
 wchar_t	*wcpncpy(wchar_t * __restrict, const wchar_t * __restrict, size_t);
-wchar_t	*wcsdup(const wchar_t *);
+wchar_t	*wcsdup(const wchar_t *) __malloclike;
 int	wcscasecmp(const wchar_t *, const wchar_t *);
 int	wcsncasecmp(const wchar_t *, const wchar_t *, size_t n);
 size_t	wcsnlen(const wchar_t *, size_t) __pure;
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index fc8ba1c..6773bd7 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -187,8 +187,10 @@
 #endif
 
 #if __GNUC_PREREQ__(2, 96)
+#define	__malloclike	__attribute__((__malloc__))
 #define	__pure		__attribute__((__pure__))
 #else
+#define	__malloclike
 #define	__pure		__pure2
 #endif
 
diff --git a/sys/sys/malloc.h b/sys/sys/malloc.h
index bb3a7e0..2f37109 100644
--- a/sys/sys/malloc.h
+++ b/sys/sys/malloc.h
@@ -169,7 +169,7 @@ void	contigfree(void *addr, unsigned long size, struct malloc_type *type)
 void	*contigmalloc (unsigned long size, struct malloc_type *type,
 			   int flags, vm_paddr_t low, vm_paddr_t high,
 			   unsigned long alignment, unsigned long boundary)
-	    __heedresult;
+	    __heedresult __malloclike;
 void	malloc_init (void *);
 void	malloc_uninit (void *);
 void	kmalloc_raise_limit(struct malloc_type *type, size_t bytes);
@@ -178,7 +178,7 @@ void	kmalloc_destroy(struct malloc_type **typep);
 
 #ifdef SLAB_DEBUG
 void	*kmalloc_debug (unsigned long size, struct malloc_type *type, int flags,
-			const char *file, int line) __heedresult;
+			const char *file, int line) __heedresult __malloclike;
 void	*krealloc_debug (void *addr, unsigned long size,
 			struct malloc_type *type, int flags,
 			const char *file, int line) __heedresult;
@@ -196,7 +196,7 @@ char	*kstrndup_debug (const char *, size_t maxlen, struct malloc_type *,
 	kstrndup_debug(str, maxlen, type, __FILE__, __LINE__)
 #else
 void	*kmalloc (unsigned long size, struct malloc_type *type, int flags)
-	    __heedresult;
+	    __heedresult __malloclike;
 void	*krealloc (void *addr, unsigned long size,
 		      struct malloc_type *type, int flags) __heedresult;
 char	*kstrdup (const char *, struct malloc_type *) __heedresult;
-- 
2.9.2

