xref: /PHP-8.3/Zend/zend_range_check.h (revision 92ac598a)
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 
19 #ifndef ZEND_RANGE_CHECK_H
20 #define ZEND_RANGE_CHECK_H
21 
22 #include "zend_long.h"
23 
24 /* Flag macros for basic range recognition. Notable is that
25    always sizeof(signed) == sizeof(unsigned), so no need to
26    overcomplicate things. */
27 #if SIZEOF_INT < SIZEOF_ZEND_LONG
28 # define ZEND_LONG_CAN_OVFL_INT 1
29 # define ZEND_LONG_CAN_OVFL_UINT 1
30 #endif
31 
32 #if SIZEOF_INT < SIZEOF_SIZE_T
33 /* size_t can always overflow signed int on the same platform.
34    Furthermore, by the current design, size_t can always
35    overflow zend_long. */
36 # define ZEND_SIZE_T_CAN_OVFL_UINT 1
37 #endif
38 
39 
40 /* zend_long vs. (unsigned) int checks. */
41 #ifdef ZEND_LONG_CAN_OVFL_INT
42 # define ZEND_LONG_INT_OVFL(zlong) UNEXPECTED((zlong) > (zend_long)INT_MAX)
43 # define ZEND_LONG_INT_UDFL(zlong) UNEXPECTED((zlong) < (zend_long)INT_MIN)
44 # define ZEND_LONG_EXCEEDS_INT(zlong) UNEXPECTED(ZEND_LONG_INT_OVFL(zlong) || ZEND_LONG_INT_UDFL(zlong))
45 # define ZEND_LONG_UINT_OVFL(zlong) UNEXPECTED((zlong) < 0 || (zlong) > (zend_long)UINT_MAX)
46 #else
47 # define ZEND_LONG_INT_OVFL(zl) (0)
48 # define ZEND_LONG_INT_UDFL(zl) (0)
49 # define ZEND_LONG_EXCEEDS_INT(zlong) (0)
50 # define ZEND_LONG_UINT_OVFL(zl) (0)
51 #endif
52 
53 /* size_t vs (unsigned) int checks. */
54 #define ZEND_SIZE_T_INT_OVFL(size) 	UNEXPECTED((size) > (size_t)INT_MAX)
55 #ifdef ZEND_SIZE_T_CAN_OVFL_UINT
56 # define ZEND_SIZE_T_UINT_OVFL(size) UNEXPECTED((size) > (size_t)UINT_MAX)
57 #else
58 # define ZEND_SIZE_T_UINT_OVFL(size) (0)
59 #endif
60 
61 /* Comparison zend_long vs size_t */
62 #define ZEND_SIZE_T_GT_ZEND_LONG(size, zlong) ((zlong) < 0 || (size) > (size_t)(zlong))
63 #define ZEND_SIZE_T_GTE_ZEND_LONG(size, zlong) ((zlong) < 0 || (size) >= (size_t)(zlong))
64 #define ZEND_SIZE_T_LT_ZEND_LONG(size, zlong) ((zlong) >= 0 && (size) < (size_t)(zlong))
65 #define ZEND_SIZE_T_LTE_ZEND_LONG(size, zlong) ((zlong) >= 0 && (size) <= (size_t)(zlong))
66 
67 #endif /* ZEND_RANGE_CHECK_H */
68