fortify-headers

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

stdio.h (3107B)


      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_STDIO_H
     17 #define _FORTIFY_STDIO_H
     18 
     19 #ifndef __cplusplus
     20 __extension__
     21 #endif
     22 #include_next <stdio.h>
     23 
     24 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
     25 #include "fortify-headers.h"
     26 
     27 #ifdef __cplusplus
     28 extern "C" {
     29 #endif
     30 
     31 #undef fgets
     32 #undef fread
     33 #undef fwrite
     34 #undef vsprintf
     35 #undef vsnprintf
     36 #undef snprintf
     37 #undef sprintf
     38 
     39 _FORTIFY_FN(fgets) char *fgets(char *__s, int __n, FILE *__f)
     40 {
     41 	size_t __b = __builtin_object_size(__s, 0);
     42 
     43 	if ((size_t)__n > __b)
     44 		__builtin_trap();
     45 	return __orig_fgets(__s, __n, __f);
     46 }
     47 
     48 _FORTIFY_FN(fread) size_t fread(void *__d, size_t __n, size_t __m, FILE *__f)
     49 {
     50 	size_t __b = __builtin_object_size(__d, 0);
     51 
     52 	if (__n != 0 && (__n * __m) / __n != __m)
     53 		__builtin_trap();
     54 	if (__n * __m > __b)
     55 		__builtin_trap();
     56 	return __orig_fread(__d, __n, __m, __f);
     57 }
     58 
     59 _FORTIFY_FN(fwrite) size_t fwrite(const void *__d, size_t __n, size_t __m, FILE *__f)
     60 {
     61 	size_t __b = __builtin_object_size(__d, 0);
     62 
     63 	if (__n != 0 && (__n * __m) / __n != __m)
     64 		__builtin_trap();
     65 	if (__n * __m > __b)
     66 		__builtin_trap();
     67 	return __orig_fwrite(__d, __n, __m, __f);
     68 }
     69 
     70 _FORTIFY_FN(vsnprintf) int vsnprintf(char *__s, size_t __n, const char *__f,
     71                                      __builtin_va_list __v)
     72 {
     73 	size_t __b = __builtin_object_size(__s, 0);
     74 
     75 	if (__n > __b)
     76 		__builtin_trap();
     77 	return __orig_vsnprintf(__s, __n, __f, __v);
     78 }
     79 
     80 _FORTIFY_FN(vsprintf) int vsprintf(char *__s, const char *__f, __builtin_va_list __v)
     81 {
     82 	size_t __b = __builtin_object_size(__s, 0);
     83 	int __r;
     84 
     85 	if (__b != (size_t)-1) {
     86 		__r = __orig_vsnprintf(__s, __b, __f, __v);
     87 		if (__r != -1 && (size_t)__r >= __b)
     88 			__builtin_trap();
     89 	} else {
     90 		__r = __orig_vsprintf(__s, __f, __v);
     91 	}
     92 	return __r;
     93 }
     94 
     95 _FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, const char *__f, ...)
     96 {
     97 	size_t __b = __builtin_object_size(__s, 0);
     98 
     99 	if (__n > __b)
    100 		__builtin_trap();
    101 	return __orig_snprintf(__s, __n, __f, __builtin_va_arg_pack());
    102 }
    103 
    104 _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...)
    105 {
    106 	size_t __b = __builtin_object_size(__s, 0);
    107 	int __r;
    108 
    109 	if (__b != (size_t)-1) {
    110 		__r = __orig_snprintf(__s, __b, __f, __builtin_va_arg_pack());
    111 		if (__r != -1 && (size_t)__r >= __b)
    112 			__builtin_trap();
    113 	} else {
    114 		__r = __orig_sprintf(__s, __f, __builtin_va_arg_pack());
    115 	}
    116 	return __r;
    117 }
    118 
    119 #ifdef __cplusplus
    120 }
    121 #endif
    122 
    123 #endif
    124 
    125 #endif