Skip to content

Commit 1ef7abf

Browse files
authored
zend_long: Remove ZEND_LTOA() (#20236)
* zend_long: Remove `ZEND_LTOA()` This macro is unsafe when the given buffer is too small, since `snprintf()` returns the *required* length of the string if it would fit. Thus unconditionally writing a NUL there might result in a out-of-bounds write. * zend_long: Remove `ZEND_LTOA_BUF_LEN`
1 parent e6c52bc commit 1ef7abf

File tree

3 files changed

+5
-17
lines changed

3 files changed

+5
-17
lines changed

UPGRADING.INTERNALS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ PHP 8.6 INTERNALS UPGRADE NOTES
2929
. CHECK_ZVAL_NULL_PATH() and CHECK_NULL_PATH() have been removed, use
3030
zend_str_has_nul_byte(Z_STR_P(...)) and zend_char_has_nul_byte()
3131
respectively.
32+
. ZEND_LTOA() (and ZEND_LTOA_BUF_LEN) has been removed, as it was
33+
unsafe. Directly use ZEND_LONG_FMT with a function from the
34+
printf family.
3235

3336
========================
3437
2. Build system changes

Zend/zend_long.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,20 @@ typedef int32_t zend_off_t;
5151
#endif
5252

5353

54-
/* Conversion macros. */
55-
#define ZEND_LTOA_BUF_LEN 65
56-
5754
#ifdef ZEND_ENABLE_ZVAL_LONG64
5855
# define ZEND_LONG_FMT "%" PRId64
5956
# define ZEND_ULONG_FMT "%" PRIu64
6057
# define ZEND_XLONG_FMT "%" PRIx64
6158
# define ZEND_LONG_FMT_SPEC PRId64
6259
# define ZEND_ULONG_FMT_SPEC PRIu64
6360
# ifdef ZEND_WIN32
64-
# define ZEND_LTOA(i, s, len) _i64toa_s((i), (s), (len), 10)
6561
# define ZEND_ATOL(s) _atoi64((s))
6662
# define ZEND_STRTOL(s0, s1, base) _strtoi64((s0), (s1), (base))
6763
# define ZEND_STRTOUL(s0, s1, base) _strtoui64((s0), (s1), (base))
6864
# define ZEND_STRTOL_PTR _strtoi64
6965
# define ZEND_STRTOUL_PTR _strtoui64
7066
# define ZEND_ABS _abs64
7167
# else
72-
# define ZEND_LTOA(i, s, len) \
73-
do { \
74-
int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
75-
(s)[st] = '\0'; \
76-
} while (0)
7768
# define ZEND_ATOL(s) atoll((s))
7869
# define ZEND_STRTOL(s0, s1, base) strtoll((s0), (s1), (base))
7970
# define ZEND_STRTOUL(s0, s1, base) strtoull((s0), (s1), (base))
@@ -90,14 +81,8 @@ typedef int32_t zend_off_t;
9081
# define ZEND_LONG_FMT_SPEC PRId32
9182
# define ZEND_ULONG_FMT_SPEC PRIu32
9283
# ifdef ZEND_WIN32
93-
# define ZEND_LTOA(i, s, len) _ltoa_s((i), (s), (len), 10)
9484
# define ZEND_ATOL(s) atol((s))
9585
# else
96-
# define ZEND_LTOA(i, s, len) \
97-
do { \
98-
int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
99-
(s)[st] = '\0'; \
100-
} while (0)
10186
# define ZEND_ATOL(s) atol((s))
10287
# endif
10388
# define ZEND_STRTOL_PTR strtol

ext/standard/hrtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
} while (0)
3232
#endif
3333
#define PHP_RETURN_HRTIME(t) do { \
34-
char _a[ZEND_LTOA_BUF_LEN]; \
34+
char _a[65]; \
3535
double _d; \
36-
HRTIME_U64A(t, _a, ZEND_LTOA_BUF_LEN); \
36+
HRTIME_U64A(t, _a, sizeof(_a)); \
3737
_d = zend_strtod(_a, NULL); \
3838
RETURN_DOUBLE(_d); \
3939
} while (0)

0 commit comments

Comments
 (0)