fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

stdlib.h (2029B)


      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 __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 #error PATH_MAX unset. A fortified realpath will not work.
     47 #else
     48 	if (__r && PATH_MAX > __bos(__r, 2)) {
     49 		char __buf[PATH_MAX], *__ret;
     50 		size_t __l;
     51 
     52 		__ret = __orig_realpath(__p, __buf);
     53 		if (!__ret)
     54 			return NULL;
     55 		__l = __builtin_strlen(__ret) + 1;
     56 		if (__l > __bos(__r, 0))
     57 			__builtin_trap();
     58 		__builtin_memcpy(__r, __ret, __l);
     59 		return __r;
     60 	}
     61 	return __orig_realpath(__p, __r);
     62 #endif
     63 }
     64 #endif
     65 
     66 #ifdef __cplusplus
     67 }
     68 #endif
     69 
     70 #endif
     71 
     72 #endif