fortify-headers

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

stdlib.h (1927B)


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