Skip to content

Commit cb0b037

Browse files
committed
Use zend_long for resource ID
Currently, resource IDs are limited to 32-bits. As resource IDs are not reused, this means that resource ID overflow for long-running processes is very possible. This patch switched resource IDs to use zend_long instead, which means that on 64-bit systems, 64-bit resource IDs will be used. This makes resource ID overflow practically impossible. The tradeoff is an 8 byte increase in zend_resource size.
1 parent 5b2ddf5 commit cb0b037

File tree

6 files changed

+10
-9
lines changed

6 files changed

+10
-9
lines changed

Zend/zend_execute.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,9 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(vo
21662166

21672167
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_use_resource_as_offset(const zval *dim)
21682168
{
2169-
zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(dim), Z_RES_HANDLE_P(dim));
2169+
zend_error(E_WARNING,
2170+
"Resource ID#" ZEND_LONG_FMT " used as offset, casting to integer (" ZEND_LONG_FMT ")",
2171+
Z_RES_HANDLE_P(dim), Z_RES_HANDLE_P(dim));
21702172
}
21712173

21722174
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_new_element_for_string(void)

Zend/zend_list.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ static HashTable list_destructors;
3131

3232
ZEND_API zval* ZEND_FASTCALL zend_list_insert(void *ptr, int type)
3333
{
34-
int index;
3534
zval zv;
3635

37-
index = zend_hash_next_free_element(&EG(regular_list));
36+
zend_long index = zend_hash_next_free_element(&EG(regular_list));
3837
if (index == 0) {
3938
index = 1;
40-
} else if (index == INT_MAX) {
39+
} else if (index == ZEND_LONG_MAX) {
4140
zend_error_noreturn(E_ERROR, "Resource ID space overflow");
4241
}
4342
ZVAL_NEW_RES(&zv, index, ptr, type);

Zend/zend_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ struct _zend_object {
485485

486486
struct _zend_resource {
487487
zend_refcounted_h gc;
488-
int handle; // TODO: may be removed ???
488+
zend_long handle; // TODO: may be removed ???
489489
int type;
490490
void *ptr;
491491
};

ext/standard/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ PHPAPI PHP_FUNCTION(fclose)
908908
PHP_STREAM_TO_ZVAL(stream, res);
909909

910910
if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) {
911-
php_error_docref(NULL, E_WARNING, "%d is not a valid stream resource", stream->res->handle);
911+
php_error_docref(NULL, E_WARNING, ZEND_LONG_FMT " is not a valid stream resource", stream->res->handle);
912912
RETURN_FALSE;
913913
}
914914

ext/standard/var.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ PHPAPI void php_var_dump(zval *struc, int level) /* {{{ */
195195
}
196196
case IS_RESOURCE: {
197197
const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
198-
php_printf("%sresource(%d) of type (%s)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown");
198+
php_printf("%sresource(" ZEND_LONG_FMT ") of type (%s)\n", COMMON, Z_RES_P(struc)->handle, type_name ? type_name : "Unknown");
199199
break;
200200
}
201201
case IS_REFERENCE:
@@ -380,7 +380,7 @@ PHPAPI void php_debug_zval_dump(zval *struc, int level) /* {{{ */
380380
break;
381381
case IS_RESOURCE: {
382382
const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(struc));
383-
php_printf("resource(%d) of type (%s) refcount(%u)\n", Z_RES_P(struc)->handle, type_name ? type_name : "Unknown", Z_REFCOUNT_P(struc));
383+
php_printf("resource(" ZEND_LONG_FMT ") of type (%s) refcount(%u)\n", Z_RES_P(struc)->handle, type_name ? type_name : "Unknown", Z_REFCOUNT_P(struc));
384384
break;
385385
}
386386
case IS_REFERENCE:

sapi/phpdbg/phpdbg_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ char *phpdbg_short_zval_print(zval *zv, int maxlen) /* {{{ */
702702
zend_string_release(str);
703703
} break;
704704
case IS_RESOURCE:
705-
spprintf(&decode, 0, "Rsrc #%d", Z_RES_HANDLE_P(zv));
705+
spprintf(&decode, 0, "Rsrc #" ZEND_LONG_FMT, Z_RES_HANDLE_P(zv));
706706
break;
707707
case IS_ARRAY:
708708
spprintf(&decode, 0, "array(%d)", zend_hash_num_elements(Z_ARR_P(zv)));

0 commit comments

Comments
 (0)