commit e7c10a1b27be94a09bb03bf31b4b66233e3d1bdc
parent b333df29dcfabcd4ad0cf630fdb648eaa38ec1c7
Author: jvoisin <julien.voisin@dustri.org>
Date: Sun, 9 Jul 2023 19:14:44 +0200
Add some malloc annotations
Diffstat:
5 files changed, 62 insertions(+), 2 deletions(-)
diff --git a/include/fortify-headers.h b/include/fortify-headers.h
@@ -66,6 +66,12 @@
#define __format(...)
#endif
+#if defined __has_attribute && __has_attribute (malloc)
+#define __malloc(...) __attribute__ ((malloc, __VA_ARGS__))
+#else
+#define __malloc(...)
+#endif
+
/* TODO(jvoisin) Figure a nice way to make use of __builtin_mul_overflow while ignoring the result. */
/* TODO(jvoisin) Make use of C23's stdckdint header: https://gustedt.gitlabpages.inria.fr/c23-library/#stdckdint */
diff --git a/include/stdio.h b/include/stdio.h
@@ -29,13 +29,25 @@ __extension__
extern "C" {
#endif
+#undef fdopen
#undef fgets
+#undef fmemopen
+#undef fopen
#undef fread
#undef fwrite
-#undef vsprintf
-#undef vsnprintf
+#undef popen
+#undef tmpfile
#undef snprintf
#undef sprintf
+#undef vsnprintf
+#undef vsprintf
+
+__access(read_only, 2)
+__malloc(malloc (fclose, 1))
+_FORTIFY_FN(fdopen) FILE *fdopen(int __f, const char* __m)
+{
+ return __orig_fdopen(__f, __m);
+}
__access(write_only, 1, 2)
_FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f)
@@ -47,6 +59,20 @@ _FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f)
return __orig_fgets(__s, __n, __f);
}
+__malloc(malloc (fclose, 1))
+_FORTIFY_FN(fmemopen) FILE *fmemopen(void* __b, size_t __s, const char* __m)
+{
+ return __orig_fmemopen(__b, __s, __m);
+}
+
+__access(read_only, 1)
+__access(read_only, 2)
+__malloc(malloc (fclose, 1))
+_FORTIFY_FN(fopen) FILE *fopen(const char* __p, const char* __m)
+{
+ return __orig_fopen(__p, __m);
+}
+
__access(write_only, 1)
_FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n,
size_t __m, FILE *__f)
@@ -73,6 +99,18 @@ _FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n,
return __orig_fwrite(__d, __n, __m, __f);
}
+__malloc(malloc (pclose, 1))
+_FORTIFY_FN(popen) FILE *popen(const char* __c, const char* __t)
+{
+ return __orig_popen(__c, __t);
+}
+
+__malloc(malloc (fclose, 1))
+_FORTIFY_FN(tmpfile) FILE *tmpfile(void)
+{
+ return __orig_tmpfile();
+}
+
__access(read_write, 1, 2)
_FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n,
const char *__f, __builtin_va_list __v)
diff --git a/include/stdlib.h b/include/stdlib.h
@@ -36,6 +36,14 @@ __extension__
extern "C" {
#endif
+#undef malloc
+
+__malloc(malloc (free, 1))
+_FORTIFY_FN(malloc) void *malloc(size_t __s)
+{
+ return __orig_malloc(__s);
+}
+
/* FIXME clang */
#if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__)
#undef realpath
diff --git a/tests/Makefile b/tests/Makefile
@@ -57,6 +57,7 @@ TARGETS= \
test_fwrite_overwrite \
test_vsnprintf \
test_vsprintf \
+ test_malloc \
.SILENT:
diff --git a/tests/test_malloc.c b/tests/test_malloc.c
@@ -0,0 +1,7 @@
+#include "common.h"
+
+#include <stdlib.h>
+
+int main(int argc, char** argv) {
+ free(malloc(1));
+}