stdlib.h (2137B)
1 /* 2 * Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org> 3 * Copyright (C) 2022 q66 <q66@chimera-linux.org> 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef _FORTIFY_STDLIB_H 18 #define _FORTIFY_STDLIB_H 19 20 #if !defined(__cplusplus) && !defined(__clang__) 21 __extension__ 22 #endif 23 #include_next <stdlib.h> 24 25 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 26 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 27 #if !defined(__cplusplus) && !defined(__clang__) 28 __extension__ 29 #endif 30 #include_next <limits.h> 31 #endif 32 33 #include "fortify-headers.h" 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /* FIXME clang */ 40 #if (defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)) && !defined(__clang__) 41 #undef realpath 42 __fortify_warning_if(__p == NULL, "'realpath' called with path set to `NULL`; did you invert the arguments?") 43 _FORTIFY_FN(realpath) char *realpath(const char *__p, char *__r) 44 { 45 #ifndef PATH_MAX 46 /* see man realpath(3) */ 47 #define PATH_MAX 4096 48 #define _FORTIFY_UNDEF_PATH_MAX 49 #endif 50 if (__r && PATH_MAX > __bos(__r, 2)) { 51 char __buf[PATH_MAX], *__ret; 52 size_t __l; 53 54 __ret = __orig_realpath(__p, __buf); 55 if (!__ret) 56 return NULL; 57 __l = __builtin_strlen(__ret) + 1; 58 if (__l > __bos(__r, 0)) 59 __builtin_trap(); 60 __builtin_memcpy(__r, __ret, __l); 61 return __r; 62 } 63 return __orig_realpath(__p, __r); 64 } 65 #ifdef _FORTIFY_UNDEF_PATH_MAX 66 #undef PATH_MAX 67 #undef _FORTIFY_UNDEF_PATH_MAX 68 #endif 69 #endif 70 71 #ifdef __cplusplus 72 } 73 #endif 74 75 #endif 76 77 #endif