fortify-headers

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

strings.h (1909B)


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