zend_range_check.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Anatol Belski <ab@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifndef ZEND_RANGE_CHECK_H
  19. #define ZEND_RANGE_CHECK_H
  20. #include "zend_long.h"
  21. /* Flag macros for basic range recognition. Notable is that
  22. always sizeof(signed) == sizeof(unsigned), so no need to
  23. overcomplicate things. */
  24. #if SIZEOF_INT < SIZEOF_ZEND_LONG
  25. # define ZEND_LONG_CAN_OVFL_INT 1
  26. # define ZEND_LONG_CAN_OVFL_UINT 1
  27. #endif
  28. #if SIZEOF_INT < SIZEOF_SIZE_T
  29. /* size_t can always overflow signed int on the same platform.
  30. Furthermore, by the current design, size_t can always
  31. overflow zend_long. */
  32. # define ZEND_SIZE_T_CAN_OVFL_UINT 1
  33. #endif
  34. /* zend_long vs. (unsigned) int checks. */
  35. #ifdef ZEND_LONG_CAN_OVFL_INT
  36. # define ZEND_LONG_INT_OVFL(zlong) UNEXPECTED((zlong) > (zend_long)INT_MAX)
  37. # define ZEND_LONG_INT_UDFL(zlong) UNEXPECTED((zlong) < (zend_long)INT_MIN)
  38. # define ZEND_LONG_EXCEEDS_INT(zlong) UNEXPECTED(ZEND_LONG_INT_OVFL(zlong) || ZEND_LONG_INT_UDFL(zlong))
  39. # define ZEND_LONG_UINT_OVFL(zlong) UNEXPECTED((zlong) < 0 || (zlong) > (zend_long)UINT_MAX)
  40. #else
  41. # define ZEND_LONG_INT_OVFL(zl) (0)
  42. # define ZEND_LONG_INT_UDFL(zl) (0)
  43. # define ZEND_LONG_EXCEEDS_INT(zlong) (0)
  44. # define ZEND_LONG_UINT_OVFL(zl) (0)
  45. #endif
  46. /* size_t vs (unsigned) int checks. */
  47. #define ZEND_SIZE_T_INT_OVFL(size) UNEXPECTED((size) > (size_t)INT_MAX)
  48. #ifdef ZEND_SIZE_T_CAN_OVFL_UINT
  49. # define ZEND_SIZE_T_UINT_OVFL(size) UNEXPECTED((size) > (size_t)UINT_MAX)
  50. #else
  51. # define ZEND_SIZE_T_UINT_OVFL(size) (0)
  52. #endif
  53. /* Comparison zend_long vs size_t */
  54. #define ZEND_SIZE_T_GT_ZEND_LONG(size, zlong) ((zlong) < 0 || (size) > (size_t)(zlong))
  55. #define ZEND_SIZE_T_GTE_ZEND_LONG(size, zlong) ((zlong) < 0 || (size) >= (size_t)(zlong))
  56. #define ZEND_SIZE_T_LT_ZEND_LONG(size, zlong) ((zlong) >= 0 && (size) < (size_t)(zlong))
  57. #define ZEND_SIZE_T_LTE_ZEND_LONG(size, zlong) ((zlong) >= 0 && (size) <= (size_t)(zlong))
  58. #endif /* ZEND_RANGE_CHECK_H */