diff options
| author | jvoisin | 2023-07-09 19:14:44 +0200 |
|---|---|---|
| committer | jvoisin | 2023-07-09 19:14:44 +0200 |
| commit | e7c10a1b27be94a09bb03bf31b4b66233e3d1bdc (patch) | |
| tree | 74eaf63f04fa673d81fe900f38dbae1b1241fefe /include | |
| parent | b333df29dcfabcd4ad0cf630fdb648eaa38ec1c7 (diff) | |
Add some malloc annotations
Diffstat (limited to 'include')
| -rw-r--r-- | include/fortify-headers.h | 6 | ||||
| -rw-r--r-- | include/stdio.h | 42 | ||||
| -rw-r--r-- | include/stdlib.h | 8 |
3 files changed, 54 insertions, 2 deletions
diff --git a/include/fortify-headers.h b/include/fortify-headers.h index 24af881..42a283f 100644 --- a/include/fortify-headers.h +++ b/include/fortify-headers.h | |||
| @@ -66,6 +66,12 @@ | |||
| 66 | #define __format(...) | 66 | #define __format(...) |
| 67 | #endif | 67 | #endif |
| 68 | 68 | ||
| 69 | #if defined __has_attribute && __has_attribute (malloc) | ||
| 70 | #define __malloc(...) __attribute__ ((malloc, __VA_ARGS__)) | ||
| 71 | #else | ||
| 72 | #define __malloc(...) | ||
| 73 | #endif | ||
| 74 | |||
| 69 | 75 | ||
| 70 | /* TODO(jvoisin) Figure a nice way to make use of __builtin_mul_overflow while ignoring the result. */ | 76 | /* TODO(jvoisin) Figure a nice way to make use of __builtin_mul_overflow while ignoring the result. */ |
| 71 | /* TODO(jvoisin) Make use of C23's stdckdint header: https://gustedt.gitlabpages.inria.fr/c23-library/#stdckdint */ | 77 | /* 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 index 5d589f0..8cdd826 100644 --- a/include/stdio.h +++ b/include/stdio.h | |||
| @@ -29,13 +29,25 @@ __extension__ | |||
| 29 | extern "C" { | 29 | extern "C" { |
| 30 | #endif | 30 | #endif |
| 31 | 31 | ||
| 32 | #undef fdopen | ||
| 32 | #undef fgets | 33 | #undef fgets |
| 34 | #undef fmemopen | ||
| 35 | #undef fopen | ||
| 33 | #undef fread | 36 | #undef fread |
| 34 | #undef fwrite | 37 | #undef fwrite |
| 35 | #undef vsprintf | 38 | #undef popen |
| 36 | #undef vsnprintf | 39 | #undef tmpfile |
| 37 | #undef snprintf | 40 | #undef snprintf |
| 38 | #undef sprintf | 41 | #undef sprintf |
| 42 | #undef vsnprintf | ||
| 43 | #undef vsprintf | ||
| 44 | |||
| 45 | __access(read_only, 2) | ||
| 46 | __malloc(malloc (fclose, 1)) | ||
| 47 | _FORTIFY_FN(fdopen) FILE *fdopen(int __f, const char* __m) | ||
| 48 | { | ||
| 49 | return __orig_fdopen(__f, __m); | ||
| 50 | } | ||
| 39 | 51 | ||
| 40 | __access(write_only, 1, 2) | 52 | __access(write_only, 1, 2) |
| 41 | _FORTIFY_FN(fgets) char *fgets(char * _FORTIFY_POS0 __s, int __n, FILE *__f) | 53 | _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) | |||
| 47 | return __orig_fgets(__s, __n, __f); | 59 | return __orig_fgets(__s, __n, __f); |
| 48 | } | 60 | } |
| 49 | 61 | ||
| 62 | __malloc(malloc (fclose, 1)) | ||
| 63 | _FORTIFY_FN(fmemopen) FILE *fmemopen(void* __b, size_t __s, const char* __m) | ||
| 64 | { | ||
| 65 | return __orig_fmemopen(__b, __s, __m); | ||
| 66 | } | ||
| 67 | |||
| 68 | __access(read_only, 1) | ||
| 69 | __access(read_only, 2) | ||
| 70 | __malloc(malloc (fclose, 1)) | ||
| 71 | _FORTIFY_FN(fopen) FILE *fopen(const char* __p, const char* __m) | ||
| 72 | { | ||
| 73 | return __orig_fopen(__p, __m); | ||
| 74 | } | ||
| 75 | |||
| 50 | __access(write_only, 1) | 76 | __access(write_only, 1) |
| 51 | _FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n, | 77 | _FORTIFY_FN(fread) size_t fread(void * _FORTIFY_POS0 __d, size_t __n, |
| 52 | size_t __m, FILE *__f) | 78 | size_t __m, FILE *__f) |
| @@ -73,6 +99,18 @@ _FORTIFY_FN(fwrite) size_t fwrite(const void * _FORTIFY_POS0 __d, size_t __n, | |||
| 73 | return __orig_fwrite(__d, __n, __m, __f); | 99 | return __orig_fwrite(__d, __n, __m, __f); |
| 74 | } | 100 | } |
| 75 | 101 | ||
| 102 | __malloc(malloc (pclose, 1)) | ||
| 103 | _FORTIFY_FN(popen) FILE *popen(const char* __c, const char* __t) | ||
| 104 | { | ||
| 105 | return __orig_popen(__c, __t); | ||
| 106 | } | ||
| 107 | |||
| 108 | __malloc(malloc (fclose, 1)) | ||
| 109 | _FORTIFY_FN(tmpfile) FILE *tmpfile(void) | ||
| 110 | { | ||
| 111 | return __orig_tmpfile(); | ||
| 112 | } | ||
| 113 | |||
| 76 | __access(read_write, 1, 2) | 114 | __access(read_write, 1, 2) |
| 77 | _FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n, | 115 | _FORTIFY_FN(vsnprintf) int vsnprintf(char * _FORTIFY_POS0 __s, size_t __n, |
| 78 | const char *__f, __builtin_va_list __v) | 116 | const char *__f, __builtin_va_list __v) |
diff --git a/include/stdlib.h b/include/stdlib.h index 38015d4..b661862 100644 --- a/include/stdlib.h +++ b/include/stdlib.h | |||
| @@ -36,6 +36,14 @@ __extension__ | |||
| 36 | extern "C" { | 36 | extern "C" { |
| 37 | #endif | 37 | #endif |
| 38 | 38 | ||
| 39 | #undef malloc | ||
| 40 | |||
| 41 | __malloc(malloc (free, 1)) | ||
| 42 | _FORTIFY_FN(malloc) void *malloc(size_t __s) | ||
| 43 | { | ||
| 44 | return __orig_malloc(__s); | ||
| 45 | } | ||
| 46 | |||
| 39 | /* FIXME clang */ | 47 | /* FIXME clang */ |
| 40 | #if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__) | 48 | #if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__) |
| 41 | #undef realpath | 49 | #undef realpath |
