Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ PHP NEWS
. Fixed bug GH-23958 (truncated encoding names resolving to full names). (Kamil Tekiela)
. Fixed stack overflow in mb_check_encoding() and mb_convert_encoding() with
deeply nested arrays. (Eyüp Can Akman)
. Fixed mbstring.regex_retry_limit above UINT_MAX being ignored.
(Marc Bennewitz)

- MySQLi:
. Fix GH-22854: Fixed failed assertion when accessing mysqli property after
Expand Down
8 changes: 8 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ PHP 8.7 INTERNALS UPGRADE NOTES
scheduled restart has been carried out and the SHM lock is released, on
the thread that carried it out, with the same reason as the
zend_accel_schedule_restart_hook callback.
. ZEND_IN_RANGE_OF(x, T) was added to check whether the value of an integer
x is representable in the integer type T.
. ZEND_TYPE_MIN(T) and ZEND_TYPE_MAX(T) were added.

- Deprecated:
. ZEND_LONG_INT_OVFL(), ZEND_LONG_INT_UDFL(), ZEND_LONG_EXCEEDS_INT(),
ZEND_LONG_UINT_OVFL(), ZEND_SIZE_T_INT_OVFL(), ZEND_SIZE_T_UINT_OVFL() and
the *_CAN_OVFL_* flags were deprecated. Use ZEND_IN_RANGE_OF() instead.

========================
2. Build system changes
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_multiply.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

#define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \
int64_t __result = (int64_t) (a) * (int64_t) (b); \
if (__result > ZEND_LONG_MAX || __result < ZEND_LONG_MIN) { \
if (!ZEND_IN_RANGE_OF(__result, zend_long)) { \
(dval) = (double) __result; \
(usedval) = 1; \
} else { \
Expand Down
5 changes: 3 additions & 2 deletions Zend/zend_portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ using std::memory_order_acquire;
#include <intrin.h>
#endif

#include "zend_range_check.h"

/* GCC x.y.z supplies __GNUC__ = x and __GNUC_MINOR__ = y */
#ifdef __GNUC__
# define ZEND_GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
Expand Down Expand Up @@ -956,4 +954,7 @@ static zend_always_inline uint64_t ZEND_BYTES_SWAP64(uint64_t u)
# define ZEND_OPCACHE_SHM_REATTACHMENT 1
#endif

/* Last, as its declarations use ZEND_ATTRIBUTE_DEPRECATED. */
#include "zend_range_check.h"

#endif /* ZEND_PORTABILITY_H */
153 changes: 122 additions & 31 deletions Zend/zend_range_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
| SPDX-License-Identifier: BSD-3-Clause |
+----------------------------------------------------------------------+
| Authors: Anatol Belski <ab@php.net> |
| Marc Bennewitz <marc@mabe.berlin> |
+----------------------------------------------------------------------+
*/

Expand All @@ -20,47 +21,137 @@

#include "zend_long.h"

/* Flag macros for basic range recognition. Notable is that
always sizeof(signed) == sizeof(unsigned), so no need to
overcomplicate things. */
#if SIZEOF_INT < SIZEOF_ZEND_LONG
# define ZEND_LONG_CAN_OVFL_INT 1
# define ZEND_LONG_CAN_OVFL_UINT 1
#endif
/* _ZEND_INT_TYPE_CHECK(T): the integer constant 0,
* or a compile error unless T is an integer type not wider than intmax_t. */
#ifndef __cplusplus
/* C: a static_assert() cannot appear in an expression itself,
* but it can as a member of a struct of which only the size is taken.
* The "% 1" only compiles for an integer operand. */
# include <assert.h>

#if SIZEOF_INT < SIZEOF_SIZE_T
/* size_t can always overflow signed int on the same platform.
Furthermore, by the current design, size_t can always
overflow zend_long. */
# define ZEND_SIZE_T_CAN_OVFL_UINT 1
# define _ZEND_INT_TYPE_CHECK(T) \
((int)(0 * sizeof(struct { \
static_assert( \
sizeof((T)0 % 1) && sizeof(T) <= sizeof(intmax_t), \
"T must be an integer type not wider than intmax_t" \
); \
char zend_dummy; \
})))
#else
/* C++ does not allow to define a struct within sizeof(),
* so the static_assert() lives in a class template instead,
* which taking the size of instantiates. */
extern "C++" {
# include <cstdint>
# include <type_traits>
template<typename T> struct _zend_int_type_check {
static_assert(std::is_integral<T>::value && sizeof(T) <= sizeof(intmax_t),
"T must be an integer type not wider than intmax_t");
};
}

# define _ZEND_INT_TYPE_CHECK(T) ((int)(0 * sizeof(_zend_int_type_check<T>)))
#endif

/* _ZEND_TYPE_IS_SIGNED(T): whether the integer type T is signed, as an integer constant expression. */
#define _ZEND_TYPE_IS_SIGNED(T) (!((T)-1 > (T)0))

/* zend_long vs. (unsigned) int checks. */
#ifdef ZEND_LONG_CAN_OVFL_INT
# define ZEND_LONG_INT_OVFL(zlong) UNEXPECTED((zlong) > (zend_long)INT_MAX)
# define ZEND_LONG_INT_UDFL(zlong) UNEXPECTED((zlong) < (zend_long)INT_MIN)
# define ZEND_LONG_EXCEEDS_INT(zlong) UNEXPECTED(ZEND_LONG_INT_OVFL(zlong) || ZEND_LONG_INT_UDFL(zlong))
# define ZEND_LONG_UINT_OVFL(zlong) UNEXPECTED((zlong) < 0 || (zlong) > (zend_long)UINT_MAX)
#else
# define ZEND_LONG_INT_OVFL(zl) (0)
# define ZEND_LONG_INT_UDFL(zl) (0)
# define ZEND_LONG_EXCEEDS_INT(zlong) (0)
# define ZEND_LONG_UINT_OVFL(zl) UNEXPECTED((zl) < 0)
#endif
/* ZEND_TYPE_MAX(T): the largest value of the integer type T, as an integer constant expression,
* for types without a constant of their own such as time_t.
* T may be any signed or unsigned integer type not wider than intmax_t. */
#define ZEND_TYPE_MAX(T) (_ZEND_INT_TYPE_CHECK(T) + (_ZEND_TYPE_IS_SIGNED(T) \
? (((((T)1 << (sizeof(T) * CHAR_BIT - 2)) - 1) * 2) + 1) \
: (T)-1 \
))

/* ZEND_TYPE_MIN(T): the smallest value of the integer type T, as an integer constant expression.
* T may be any signed or unsigned integer type not wider than intmax_t. */
#define ZEND_TYPE_MIN(T) (_ZEND_TYPE_IS_SIGNED(T) ? -ZEND_TYPE_MAX(T) - 1 : (T)0)

/* ZEND_IN_RANGE_OF */

static inline bool _zend_in_range_signed(intmax_t v, intmax_t min, uintmax_t max)
{
if (max <= INTMAX_MAX) {
return v >= min && v <= (intmax_t)max;
}
return v >= 0; /* T is a uintmax_t-wide unsigned type */
}

static inline bool _zend_in_range_unsigned(uintmax_t v, uintmax_t max)
{
return v <= max;
}

#define _ZEND_IN_RANGE_SIGNED(x, T) _zend_in_range_signed((intmax_t)(x), ZEND_TYPE_MIN(T), ZEND_TYPE_MAX(T))
#define _ZEND_IN_RANGE_UNSIGNED(x, T) _zend_in_range_unsigned((uintmax_t)(x), ZEND_TYPE_MAX(T))

#ifndef __cplusplus
/* C: _Generic dispatches on the promoted type of x, without evaluating it. */

/* size_t vs (unsigned) int checks. */
#define ZEND_SIZE_T_INT_OVFL(size) UNEXPECTED((size) > (size_t)INT_MAX)
#ifdef ZEND_SIZE_T_CAN_OVFL_UINT
# define ZEND_SIZE_T_UINT_OVFL(size) UNEXPECTED((size) > (size_t)UINT_MAX)
/* ZEND_IN_RANGE_OF(x, T): whether the value of the integer expression x is representable in the integer type T.
* x and T may be any signed or unsigned integer types not wider than intmax_t.
* x is evaluated exactly once. */
# define ZEND_IN_RANGE_OF(x, T) EXPECTED(_Generic((x) + 0, \
int: _ZEND_IN_RANGE_SIGNED(x, T), \
long: _ZEND_IN_RANGE_SIGNED(x, T), \
long long: _ZEND_IN_RANGE_SIGNED(x, T), \
unsigned int: _ZEND_IN_RANGE_UNSIGNED(x, T), \
unsigned long: _ZEND_IN_RANGE_UNSIGNED(x, T), \
unsigned long long: _ZEND_IN_RANGE_UNSIGNED(x, T) \
))
#else
# define ZEND_SIZE_T_UINT_OVFL(size) (0)
/* C++ has no _Generic (g++ and MSVC reject it).
* A function template deduces the type of x instead.
* The unary + applies the integer promotions, as (x) + 0 does for C. */
extern "C++" {
template<typename T, typename X>
inline bool _zend_in_range_of(X x) {
static_assert(std::is_integral<X>::value && sizeof(X) <= sizeof(intmax_t), "x must be an integer not wider than intmax_t");

return std::is_signed<X>::value
? _ZEND_IN_RANGE_SIGNED(x, T)
: _ZEND_IN_RANGE_UNSIGNED(x, T);
}
}

/* ZEND_IN_RANGE_OF(x, T): whether the value of the integer expression x is representable in the integer type T.
* x and T may be any signed or unsigned integer types not wider than intmax_t.
* x is evaluated exactly once. */
# define ZEND_IN_RANGE_OF(x, T) EXPECTED(_zend_in_range_of<T>(+(x)))
#endif

/* Comparison zend_long vs size_t */
#define ZEND_SIZE_T_GT_ZEND_LONG(size, zlong) ((zlong) < 0 || (size) > (size_t)(zlong))
#define ZEND_SIZE_T_GT_ZEND_LONG(size, zlong) ((zlong) < 0 || (size) > (size_t)(zlong))
#define ZEND_SIZE_T_GTE_ZEND_LONG(size, zlong) ((zlong) < 0 || (size) >= (size_t)(zlong))
#define ZEND_SIZE_T_LT_ZEND_LONG(size, zlong) ((zlong) >= 0 && (size) < (size_t)(zlong))
#define ZEND_SIZE_T_LT_ZEND_LONG(size, zlong) ((zlong) >= 0 && (size) < (size_t)(zlong))
#define ZEND_SIZE_T_LTE_ZEND_LONG(size, zlong) ((zlong) >= 0 && (size) <= (size_t)(zlong))

/* -- DEPRECATED -- */

/* The zend_long and size_t vs. (unsigned) int checks below are deprecated, use ZEND_IN_RANGE_OF() instead. */
ZEND_ATTRIBUTE_DEPRECATED static inline void _zend_deprecated_use_ZEND_IN_RANGE_OF_instead(void) {}

#if SIZEOF_INT < SIZEOF_ZEND_LONG
# define ZEND_LONG_CAN_OVFL_INT 1 /* Deprecated */
# define ZEND_LONG_CAN_OVFL_UINT 1 /* Deprecated */
# define ZEND_LONG_INT_OVFL(zlong) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), UNEXPECTED((zlong) > (zend_long)INT_MAX))
# define ZEND_LONG_INT_UDFL(zlong) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), UNEXPECTED((zlong) < (zend_long)INT_MIN))
# define ZEND_LONG_EXCEEDS_INT(zlong) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), UNEXPECTED((zlong) > (zend_long)INT_MAX || (zlong) < (zend_long)INT_MIN))
# define ZEND_LONG_UINT_OVFL(zlong) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), UNEXPECTED((zlong) < 0 || (zlong) > (zend_long)UINT_MAX))
#else
# define ZEND_LONG_INT_OVFL(zl) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), 0)
# define ZEND_LONG_INT_UDFL(zl) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), 0)
# define ZEND_LONG_EXCEEDS_INT(zlong) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), 0)
# define ZEND_LONG_UINT_OVFL(zl) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), UNEXPECTED((zl) < 0))
#endif

#define ZEND_SIZE_T_INT_OVFL(size) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), UNEXPECTED((size) > (size_t)INT_MAX))
#if SIZEOF_INT < SIZEOF_SIZE_T
# define ZEND_SIZE_T_CAN_OVFL_UINT 1 /* Deprecated */
# define ZEND_SIZE_T_UINT_OVFL(size) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), UNEXPECTED((size) > (size_t)UINT_MAX))
#else
# define ZEND_SIZE_T_UINT_OVFL(size) (_zend_deprecated_use_ZEND_IN_RANGE_OF_instead(), 0)
#endif

#endif /* ZEND_RANGE_CHECK_H */
2 changes: 1 addition & 1 deletion ext/bcmath/bcmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static zend_always_inline zend_result bcmath_check_scale(zend_long scale, uint32

static zend_result bcmath_check_precision(zend_long precision, uint32_t arg_num)
{
if (ZEND_LONG_INT_OVFL(precision)) {
if (precision > 0 && !ZEND_IN_RANGE_OF(precision, int)) {
zend_argument_value_error(arg_num, "must be between " ZEND_LONG_FMT " and %d",
(zend_long) ZEND_LONG_MIN, INT_MAX);
return FAILURE;
Expand Down
4 changes: 2 additions & 2 deletions ext/calendar/calendar.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ PHP_FUNCTION(cal_to_jd)
RETURN_THROWS();
}

if (UNEXPECTED(ZEND_LONG_EXCEEDS_INT(day))) {
if (!ZEND_IN_RANGE_OF(day, int32_t)) {
zend_argument_value_error(3, "must be between %d and %d", INT32_MIN, INT32_MAX);
RETURN_THROWS();
}
Expand Down Expand Up @@ -516,7 +516,7 @@ PHP_FUNCTION(jewishtojd)
RETURN_THROWS();
}

if (ZEND_LONG_EXCEEDS_INT(year)) {
if (!ZEND_IN_RANGE_OF(year, int)) {
zend_argument_value_error(3, "must be between %d and %d", INT_MIN, INT_MAX);
RETURN_THROWS();
}
Expand Down
2 changes: 1 addition & 1 deletion ext/calendar/julian.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void SdnToJulian(
/* Calculate the year and day of year (1 <= dayOfYear <= 366). */
{
long yearl = temp / DAYS_PER_4_YEARS;
if (yearl > INT_MAX || yearl < INT_MIN) {
if (!ZEND_IN_RANGE_OF(yearl, int)) {
goto fail;
}
year = (int) yearl;
Expand Down
4 changes: 2 additions & 2 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -3071,7 +3071,7 @@ PHP_FUNCTION(curl_escape)

ch = Z_CURL_P(zid);

if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(str))) {
if (!ZEND_IN_RANGE_OF(ZSTR_LEN(str), int)) {
RETURN_FALSE;
}

Expand Down Expand Up @@ -3100,7 +3100,7 @@ PHP_FUNCTION(curl_unescape)

ch = Z_CURL_P(zid);

if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(str))) {
if (!ZEND_IN_RANGE_OF(ZSTR_LEN(str), int)) {
RETURN_FALSE;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/dba/dba.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ PHP_FUNCTION(dba_fetch)
ZEND_PARSE_PARAMETERS_END();
}

if (ZEND_LONG_EXCEEDS_INT(skip)) {
if (!ZEND_IN_RANGE_OF(skip, int)) {
zend_argument_value_error(3, "must be between %d and %d", INT_MIN, INT_MAX);
RETURN_THROWS();
}
Expand Down
8 changes: 4 additions & 4 deletions ext/dom/characterdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ PHP_METHOD(DOMCharacterData, substringData)
const xmlChar *cur = php_dom_get_content_or_empty(node);

length = xmlUTF8Strlen(cur);
if (ZEND_LONG_INT_OVFL(offset_input) || ZEND_LONG_INT_OVFL(count_input)) {
if ((offset_input > 0 && !ZEND_IN_RANGE_OF(offset_input, int)) || (count_input > 0 && !ZEND_IN_RANGE_OF(count_input, int))) {
php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document));
RETURN_FALSE;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ static void dom_character_data_insert_data(INTERNAL_FUNCTION_PARAMETERS, bool re

length = xmlUTF8Strlen(cur);

if (ZEND_LONG_INT_OVFL(offset_input)) {
if (offset_input > 0 && !ZEND_IN_RANGE_OF(offset_input, int)) {
php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document));
RETURN_FALSE;
}
Expand Down Expand Up @@ -271,7 +271,7 @@ static void dom_character_data_delete_data(INTERNAL_FUNCTION_PARAMETERS, bool re

length = xmlUTF8Strlen(cur);

if (offset < 0 || ZEND_LONG_INT_OVFL(offset) || ZEND_LONG_INT_OVFL(count_input) || offset > length) {
if (offset < 0 || !ZEND_IN_RANGE_OF(offset, int) || (count_input > 0 && !ZEND_IN_RANGE_OF(count_input, int)) || offset > length) {
php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document));
RETURN_FALSE;
}
Expand Down Expand Up @@ -341,7 +341,7 @@ static void dom_character_data_replace_data(INTERNAL_FUNCTION_PARAMETERS, bool r

length = xmlUTF8Strlen(cur);

if (offset < 0 || ZEND_LONG_INT_OVFL(offset) || ZEND_LONG_INT_OVFL(count_input) || offset > length) {
if (offset < 0 || !ZEND_IN_RANGE_OF(offset, int) || (count_input > 0 && !ZEND_IN_RANGE_OF(count_input, int)) || offset > length) {
php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document));
RETURN_FALSE;
}
Expand Down
12 changes: 6 additions & 6 deletions ext/dom/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -1532,11 +1532,11 @@ static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode)
zend_argument_must_not_be_empty_error(1);
RETURN_THROWS();
}
if (ZEND_SIZE_T_INT_OVFL(source_len)) {
if (!ZEND_IN_RANGE_OF(source_len, int)) {
php_error_docref(NULL, E_WARNING, "Input string is too long");
RETURN_FALSE;
}
if (ZEND_LONG_EXCEEDS_INT(options)) {
if (!ZEND_IN_RANGE_OF(options, int)) {
php_error_docref(NULL, E_WARNING, "Invalid options");
RETURN_FALSE;
}
Expand Down Expand Up @@ -1794,7 +1794,7 @@ PHP_METHOD(DOMDocument, xinclude)
RETURN_THROWS();
}

if (ZEND_LONG_EXCEEDS_INT(flags)) {
if (!ZEND_IN_RANGE_OF(flags, int)) {
php_error_docref(NULL, E_WARNING, "Invalid flags");
RETURN_FALSE;
}
Expand All @@ -1820,7 +1820,7 @@ PHP_METHOD(Dom_XMLDocument, xinclude)
RETURN_THROWS();
}

if (ZEND_LONG_EXCEEDS_INT(flags)) {
if (!ZEND_IN_RANGE_OF(flags, int)) {
zend_argument_value_error(1, "is too large");
RETURN_THROWS();
}
Expand Down Expand Up @@ -2105,7 +2105,7 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
RETURN_THROWS();
}

if (ZEND_LONG_EXCEEDS_INT(options)) {
if (!ZEND_IN_RANGE_OF(options, int)) {
php_error_docref(NULL, E_WARNING, "Invalid options");
RETURN_FALSE;
}
Expand All @@ -2117,7 +2117,7 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
}
ctxt = htmlCreateFileParserCtxt(source, NULL);
} else {
if (ZEND_SIZE_T_INT_OVFL(source_len)) {
if (!ZEND_IN_RANGE_OF(source_len, int)) {
php_error_docref(NULL, E_WARNING, "Input string is too long");
RETURN_FALSE;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/dom_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ xmlNodePtr php_dom_libxml_hash_iter(xmlHashTable *ht, zend_long index)
if (index < 0) {
return NULL;
}
if (UNEXPECTED(ZEND_LONG_INT_OVFL(index))) {
if (!ZEND_IN_RANGE_OF(index, int)) {
zend_value_error("must be between 0 and %d", INT_MAX);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ static void dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAMETERS, zend_

DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);

if (UNEXPECTED(ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(data)))) {
if (!ZEND_IN_RANGE_OF(ZSTR_LEN(data), int)) {
zend_argument_value_error(2, "is too long");
RETURN_THROWS();
}
Expand Down
Loading
Loading