fortify-headers

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

strings.h (2179B)


      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_STRINGS_H
     18 #define _FORTIFY_STRINGS_H
     19 
     20 #include_next <strings.h>
     21 
     22 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
     23 #include "fortify-headers.h"
     24 
     25 #ifdef __cplusplus
     26 extern "C" {
     27 #endif
     28 
     29 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_POSIX_SOURCE) \
     30  || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE+0 < 200809L) \
     31  || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700)
     32 #undef bcopy
     33 #undef bzero
     34 __fh_access(write_only, 2, 3)
     35 __fh_access(read_only, 1, 3)
     36 #if __has_builtin(__builtin_bcopy) && !defined(__clang__)
     37 // parameter 1 of function '__orig_bcopy' has type 'const void *', but parameter 1 of function '__builtin_bcopy' has type 'void *'
     38 __diagnose_as_builtin(__builtin_bcopy, 1, 2, 3)
     39 #endif
     40 _FORTIFY_FN(bcopy) void bcopy(const void * _FORTIFY_POS0 __s,
     41                               void * _FORTIFY_POS0 __d, size_t __n)
     42 {
     43 	__fh_size_t __bd = __fh_bos(__d, 0);
     44 	__fh_size_t __bs = __fh_bos(__s, 0);
     45 
     46 	if (__n > __bd || __n > __bs)
     47 		__builtin_trap();
     48 	return __orig_bcopy(__s, __d, __n);
     49 }
     50 
     51 __fh_access(write_only, 1, 2)
     52 #if __has_builtin(__builtin_bzero)
     53 __diagnose_as_builtin(__builtin_bzero, 1, 2)
     54 #endif
     55 _FORTIFY_FN(bzero) void bzero(void * _FORTIFY_POS0 __s, size_t __n)
     56 {
     57 	__fh_size_t __b = __fh_bos(__s, 0);
     58 
     59 	if (__n > __b)
     60 		__builtin_trap();
     61 	return __orig_bzero(__s, __n);
     62 }
     63 #endif
     64 
     65 #ifdef __cplusplus
     66 }
     67 #endif
     68 
     69 #endif
     70 
     71 #endif