Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Internal data and routines for "Safer C", or bounds-checking
  28. *               library extension.
  29. *
  30. ****************************************************************************/
  31.  
  32.  
  33. #ifndef _SAFERLIB_H_INCLUDED
  34. #define _SAFERLIB_H_INCLUDED
  35.  
  36. #define __STDC_WANT_LIB_EXT1__  1
  37. #include <stdlib.h>
  38. #include <stdint.h>
  39.  
  40. // Maximum length of runtime-constraint error message
  41. #define RTCT_MSG_MAX            128
  42.  
  43. #define _RWD_rtcthandler        __runtime_constraint_handler
  44.  
  45. extern  constraint_handler_t    __runtime_constraint_handler;
  46.  
  47. extern  void    __rtct_fail( const char *fn, const char *reason, void *reserved );
  48.  
  49. // Runtime-constraint validation macros. Call the handler and return zero if check
  50. // failed, return non-zero value if check succeeded.
  51.  
  52. #define __check_constraint_nullptr( arg )   \
  53.     ((arg == NULL) ? __rtct_fail( __func__, #arg " == NULL", NULL ), 0 : 1)
  54.  
  55. #define __check_constraint_maxsize( arg )   \
  56.     ((arg > RSIZE_MAX) ? __rtct_fail( __func__, #arg " > RSIZE_MAX", NULL ), 0 : 1)
  57.  
  58. #define __check_constraint_zero( arg )   \
  59.     ((arg == 0) ? __rtct_fail( __func__, #arg " == 0", NULL ), 0 : 1)
  60.  
  61. #define __check_constraint_toosmall( name, left )   \
  62.     ((left == 0) ? __rtct_fail( __func__, #name " is too small to hold data", NULL ), 0 : 1)
  63.  
  64.  
  65. // Runtime-constraint validation macros. Construct the message and return
  66. // zero if check failed, return non-zero value if check succeeded.
  67. // __rtct_fail has to be explicitly called later.
  68.  
  69. #define __check_constraint_nullptr_msg( msg, arg )   \
  70.     ((arg == NULL) ? ( msg = #arg " == NULL" ), 0 : 1)
  71.  
  72. #define __check_constraint_maxsize_msg( msg, arg )   \
  73.     ((arg > RSIZE_MAX) ? ( msg = #arg " > RSIZE_MAX" ), 0 : 1)
  74.  
  75. #define __check_constraint_zero_msg( msg, arg )   \
  76.     ((arg == 0) ? ( msg = #arg " == 0" ), 0 : 1)
  77.  
  78. #define __check_constraint_toosmall_msg( msg, name, left )   \
  79.     ((left == 0) ? ( msg = #name " is too small to hold data" ), 0 : 1)
  80.  
  81. #define __check_constraint_a_gt_b_msg( msg, a, b )   \
  82.     ((a > b) ? ( msg = #a " > " #b ), 0 : 1)
  83.  
  84. #define __check_constraint_overlap_msg( msg, p1, len1, p2, len2 )       \
  85.     (((p1 == p2) || ( (p1 > p2) && ( p1 < (CHAR_TYPE *)p2 + len2 * sizeof( CHAR_TYPE )))    \
  86.         || ( (p2 > p1) && ( p2 < (CHAR_TYPE *)p1 + len1 * sizeof( CHAR_TYPE ))))            \
  87.      ? ( msg = #p1 " overlap " #p2 ), 0 : 1)
  88.  
  89.  
  90. // For 16-bit targets, the RSIZE_MAX check is effectively no-op. Object sizes
  91. // up to SIZE_MAX are legal and not uncommon.
  92. #if RSIZE_MAX == SIZE_MAX
  93.     #undef  __check_constraint_maxsize
  94.     #define __check_constraint_maxsize( arg )   1
  95.     #undef  __check_constraint_maxsize_msg
  96.     #define __check_constraint_maxsize_msg( msg, arg )  1
  97.  
  98.     #define __lte_rsizmax( arg )    1
  99. #else
  100.     #define __lte_rsizmax( arg )    (arg <= RSIZE_MAX)
  101. #endif
  102.  
  103. #endif // _SAFERLIB_H_INCLUDED
  104.