Skip to content

Commit 3f13507

Browse files
committed
Use one place to define max length of double
Introduce new constant PHP_DOUBLE_MAX_LENGTH for that purpose
1 parent 158b537 commit 3f13507

File tree

5 files changed

+14
-37
lines changed

5 files changed

+14
-37
lines changed

ext/json/json.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@
3333
#include "php_json_parser.h"
3434
#include <zend_exceptions.h>
3535

36-
#include <float.h>
37-
#if defined(DBL_MANT_DIG) && defined(DBL_MIN_EXP)
38-
#define NUM_BUF_SIZE (3 + DBL_MANT_DIG - DBL_MIN_EXP)
39-
#else
40-
#define NUM_BUF_SIZE 1080
41-
#endif
42-
43-
4436
static PHP_MINFO_FUNCTION(json);
4537
static PHP_FUNCTION(json_encode);
4638
static PHP_FUNCTION(json_decode);

ext/json/json_encoder.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@
3131
#include "php_json.h"
3232
#include <zend_exceptions.h>
3333

34-
/* double limits */
35-
#include <float.h>
36-
#if defined(DBL_MANT_DIG) && defined(DBL_MIN_EXP)
37-
#define PHP_JSON_DOUBLE_MAX_LENGTH (3 + DBL_MANT_DIG - DBL_MIN_EXP)
38-
#else
39-
#define PHP_JSON_DOUBLE_MAX_LENGTH 1080
40-
#endif
41-
4234
static const char digits[] = "0123456789abcdef";
4335

4436
static void php_json_escape_string(smart_str *buf, char *s, size_t len, int options);
@@ -103,11 +95,11 @@ static inline int php_json_is_valid_double(double d) /* {{{ */
10395
static inline void php_json_encode_double(smart_str *buf, double d, int options) /* {{{ */
10496
{
10597
size_t len;
106-
char num[PHP_JSON_DOUBLE_MAX_LENGTH];
98+
char num[PHP_DOUBLE_MAX_LENGTH];
10799

108100
php_gcvt(d, (int)PG(serialize_precision), '.', 'e', num);
109101
len = strlen(num);
110-
if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_JSON_DOUBLE_MAX_LENGTH - 2) {
102+
if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < PHP_DOUBLE_MAX_LENGTH - 2) {
111103
num[len++] = '.';
112104
num[len++] = '0';
113105
num[len] = '\0';

ext/standard/var.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@
3535

3636
#define COMMON (is_ref ? "&" : "")
3737

38-
/* Copied from main/spprintf.c and use the same buffer size
39-
*
40-
* NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
41-
*
42-
* XXX: this is a magic number; do not decrease it
43-
* Emax = 1023
44-
* NDIG = 320
45-
* NUM_BUF_SIZE >= strlen("-") + Emax + strlrn(".") + NDIG + strlen("E+1023") + 1;
46-
*/
47-
#define NUM_BUF_SIZE 2048
48-
4938
static void php_array_element_dump(zval *zv, zend_ulong index, zend_string *key, int level) /* {{{ */
5039
{
5140
if (key == NULL) { /* numeric key */
@@ -447,7 +436,7 @@ static void php_object_element_export(zval *zv, zend_ulong index, zend_string *k
447436
PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
448437
{
449438
HashTable *myht;
450-
char tmp_str[NUM_BUF_SIZE];
439+
char tmp_str[PHP_DOUBLE_MAX_LENGTH];
451440
zend_string *ztmp, *ztmp2;
452441
zend_ulong index;
453442
zend_string *key;
@@ -853,7 +842,7 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
853842
return;
854843

855844
case IS_DOUBLE: {
856-
char tmp_str[NUM_BUF_SIZE];
845+
char tmp_str[PHP_DOUBLE_MAX_LENGTH];
857846
smart_str_appendl(buf, "d:", 2);
858847
php_gcvt(Z_DVAL_P(struc), (int)PG(serialize_precision), '.', 'E', tmp_str);
859848
smart_str_appends(buf, tmp_str);

main/php.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ char *strerror(int);
231231
#define INT_MIN (- INT_MAX - 1)
232232
#endif
233233

234+
/* double limits */
235+
#include <float.h>
236+
#if defined(DBL_MANT_DIG) && defined(DBL_MIN_EXP)
237+
#define PHP_DOUBLE_MAX_LENGTH (3 + DBL_MANT_DIG - DBL_MIN_EXP)
238+
#else
239+
#define PHP_DOUBLE_MAX_LENGTH 1080
240+
#endif
241+
234242
#define PHP_GCC_VERSION ZEND_GCC_VERSION
235243
#define PHP_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_MALLOC
236244
#define PHP_ATTRIBUTE_FORMAT ZEND_ATTRIBUTE_FORMAT

main/spprintf.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,9 @@
152152

153153
/*
154154
* NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
155-
*
156-
* XXX: this is a magic number; do not decrease it
157-
* Emax = 1023
158-
* NDIG = 320
159-
* NUM_BUF_SIZE >= strlen("-") + Emax + strlrn(".") + NDIG + strlen("E+1023") + 1;
155+
* which can be at most max length of double
160156
*/
161-
#define NUM_BUF_SIZE 2048
157+
#define NUM_BUF_SIZE PHP_DOUBLE_MAX_LENGTH
162158

163159
#define NUM(c) (c - '0')
164160

0 commit comments

Comments
 (0)