fortify-headers

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

stdlib.h (1808B)


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