Closed
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
25 changes: 25 additions & 0 deletions Zend/tests/const_deprecation.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
--TEST--
Internal constant deprecation
--SKIPIF--
<?php
if (!extension_loaded('zend-test')) die('skip requires zend-test');
?>
--FILE--
<?php

var_dump(ZEND_TEST_DEPRECATED);
var_dump(constant('ZEND_TEST_DEPRECATED'));

const X = ZEND_TEST_DEPRECATED;
var_dump(X);

?>
--EXPECTF--
Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)
29 changes: 20 additions & 9 deletions Zend/zend_compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1409,15 +1409,27 @@ ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char
}
/* }}} */

static zend_bool can_ct_eval_const(zend_constant *c) {
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
return 0;
}
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
&& (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
return 1;
}
if (Z_TYPE(c->value) < IS_OBJECT
&& !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
return 1;
}
return 0;
}

static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c && (
((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
|| (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
)) {
if (c && can_ct_eval_const(c)) {
ZVAL_COPY_OR_DUP(zv, &c->value);
return 1;
}
Expand All@@ -1426,14 +1438,13 @@ static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool i
/* Substitute true, false and null (including unqualified usage in namespaces) */
const char *lookup_name = ZSTR_VAL(name);
size_t lookup_len = ZSTR_LEN(name);
zval *val;

if (!is_fully_qualified) {
zend_get_unqualified_name(name, &lookup_name, &lookup_len);
}

if ((val = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, val);
if ((c = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, &c->value);
return 1;
}

Expand Down
77 changes: 47 additions & 30 deletions Zend/zend_constants.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@
#define RESET_CONSTANT_VISITED(zv) Z_ACCESS_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK

/* Use for special null/true/false constants. */
static zval null_value, true_value, false_value;
static zend_constant *null_const, *true_const, *false_const;

void free_zend_constant(zval *zv)
{
Expand DownExpand Up@@ -138,9 +138,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

ZVAL_NULL(&null_value);
ZVAL_TRUE(&true_value);
ZVAL_FALSE(&false_value);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}


Expand DownExpand Up@@ -235,22 +235,22 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
}
}

ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
{
if (len == 4) {
if ((name[0] == 'n' || name[0] == 'N') &&
(name[1] == 'u' || name[1] == 'U') &&
(name[2] == 'l' || name[2] == 'L') &&
(name[3] == 'l' || name[3] == 'L')
) {
return &null_value;
return null_const;
}
if ((name[0] == 't' || name[0] == 'T') &&
(name[1] == 'r' || name[1] == 'R') &&
(name[2] == 'u' || name[2] == 'U') &&
(name[3] == 'e' || name[3] == 'E')
) {
return &true_value;
return true_const;
}
} else {
if ((name[0] == 'f' || name[0] == 'F') &&
Expand All@@ -259,10 +259,10 @@ ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
(name[3] == 's' || name[3] == 'S') &&
(name[4] == 'e' || name[4] == 'E')
) {
return &false_value;
return false_const;
}
}
return 0;
return NULL;
}
/* }}} */

Expand All@@ -279,36 +279,54 @@ ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *
}
/* }}} */

ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
if (c) {
return &c->value;
return c;
}

c = zend_get_halt_offset_constant(name, name_len);
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(name, name_len);
}

ZEND_API zval *zend_get_constant(zend_string *name)
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
zend_constant *c = zend_get_constant_str_impl(name, name_len);
if (c) {
return &c->value;
}
return NULL;
}

static zend_constant *zend_get_constant_impl(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
return c;
}

c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
}

ZEND_API zval *zend_get_constant(zend_string *name)
{
zend_constant *c = zend_get_constant_impl(name);
if (c) {
return &c->value;
}
return NULL;
}

ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
{
zend_constant *c;
Expand DownExpand Up@@ -402,7 +420,6 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}

/* non-class constant */
zval *value;
if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
/* compound constant name */
int prefix_len = colon - name;
Expand All@@ -423,28 +440,28 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
free_alloca(lcname, use_heap);

if (c) {
return &c->value;
}

if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
value = zend_get_constant_str(constant_name, const_name_len);
} else {
value = NULL;
if (!c) {
if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
c = zend_get_constant_str_impl(constant_name, const_name_len);
}
}
} else {
if (cname) {
value = zend_get_constant(cname);
c = zend_get_constant_impl(cname);
} else {
value = zend_get_constant_str(name, name_len);
c = zend_get_constant_str_impl(name, name_len);
}
}

if (!value && !(flags & ZEND_FETCH_CLASS_SILENT)) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
if (!c) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
} else if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
}
}
return value;
return &c->value;
}

static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
#define CONST_CS 0 /* No longer used -- always case sensitive */
#define CONST_PERSISTENT (1<<0) /* Persistent */
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand DownExpand Up@@ -86,9 +87,10 @@ ZEND_API int zend_register_constant(zend_constant *c);
void zend_copy_constants(HashTable *target, HashTable *sourc);
#endif

ZEND_API zval *_zend_get_special_const(const char *name, size_t name_len);
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t name_len);

static zend_always_inline zval *zend_get_special_const(const char *name, size_t name_len) {
static zend_always_inline zend_constant *zend_get_special_const(
const char *name, size_t name_len) {
if (name_len == 4 || name_len == 5) {
return _zend_get_special_const(name, name_len);
}
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -4301,6 +4301,10 @@ static zend_always_inline int _zend_quick_get_constant(

if (!check_defined_only) {
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
return SUCCESS;
}
}

CACHE_PTR(opline->extended_value, c);
Expand Down
8 changes: 4 additions & 4 deletions ext/opcache/Optimizer/block_pass.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,10 +33,10 @@
/* Checks if a constant (like "true") may be replaced by its value */
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
{
zval *zv;
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)
&& (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
|| !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
ZVAL_COPY_VALUE(result, &c->value);
Expand All@@ -50,9 +50,9 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
}

/* Special constants null/true/false can always be substituted. */
zv = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (zv) {
ZVAL_COPY_VALUE(result, zv);
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
ZVAL_COPY_VALUE(result, &c->value);
return 1;
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,8 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC);

zend_register_class_alias("_ZendTestClassAlias", zend_test_class);

REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
return SUCCESS;
}

Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
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
25 changes: 25 additions & 0 deletions Zend/tests/const_deprecation.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
--TEST--
Internal constant deprecation
--SKIPIF--
<?php
if (!extension_loaded('zend-test')) die('skip requires zend-test');
?>
--FILE--
<?php

var_dump(ZEND_TEST_DEPRECATED);
var_dump(constant('ZEND_TEST_DEPRECATED'));

const X = ZEND_TEST_DEPRECATED;
var_dump(X);

?>
--EXPECTF--
Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)
29 changes: 20 additions & 9 deletions Zend/zend_compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1409,15 +1409,27 @@ ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char
}
/* }}} */

static zend_bool can_ct_eval_const(zend_constant *c) {
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
return 0;
}
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
&& (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
return 1;
}
if (Z_TYPE(c->value) < IS_OBJECT
&& !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
return 1;
}
return 0;
}

static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c && (
((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
|| (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
)) {
if (c && can_ct_eval_const(c)) {
ZVAL_COPY_OR_DUP(zv, &c->value);
return 1;
}
Expand All@@ -1426,14 +1438,13 @@ static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool i
/* Substitute true, false and null (including unqualified usage in namespaces) */
const char *lookup_name = ZSTR_VAL(name);
size_t lookup_len = ZSTR_LEN(name);
zval *val;

if (!is_fully_qualified) {
zend_get_unqualified_name(name, &lookup_name, &lookup_len);
}

if ((val = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, val);
if ((c = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, &c->value);
return 1;
}

Expand Down
77 changes: 47 additions & 30 deletions Zend/zend_constants.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@
#define RESET_CONSTANT_VISITED(zv) Z_ACCESS_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK

/* Use for special null/true/false constants. */
static zval null_value, true_value, false_value;
static zend_constant *null_const, *true_const, *false_const;

void free_zend_constant(zval *zv)
{
Expand DownExpand Up@@ -138,9 +138,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

ZVAL_NULL(&null_value);
ZVAL_TRUE(&true_value);
ZVAL_FALSE(&false_value);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}


Expand DownExpand Up@@ -235,22 +235,22 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
}
}

ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
{
if (len == 4) {
if ((name[0] == 'n' || name[0] == 'N') &&
(name[1] == 'u' || name[1] == 'U') &&
(name[2] == 'l' || name[2] == 'L') &&
(name[3] == 'l' || name[3] == 'L')
) {
return &null_value;
return null_const;
}
if ((name[0] == 't' || name[0] == 'T') &&
(name[1] == 'r' || name[1] == 'R') &&
(name[2] == 'u' || name[2] == 'U') &&
(name[3] == 'e' || name[3] == 'E')
) {
return &true_value;
return true_const;
}
} else {
if ((name[0] == 'f' || name[0] == 'F') &&
Expand All@@ -259,10 +259,10 @@ ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
(name[3] == 's' || name[3] == 'S') &&
(name[4] == 'e' || name[4] == 'E')
) {
return &false_value;
return false_const;
}
}
return 0;
return NULL;
}
/* }}} */

Expand All@@ -279,36 +279,54 @@ ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *
}
/* }}} */

ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
if (c) {
return &c->value;
return c;
}

c = zend_get_halt_offset_constant(name, name_len);
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(name, name_len);
}

ZEND_API zval *zend_get_constant(zend_string *name)
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
zend_constant *c = zend_get_constant_str_impl(name, name_len);
if (c) {
return &c->value;
}
return NULL;
}

static zend_constant *zend_get_constant_impl(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
return c;
}

c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
}

ZEND_API zval *zend_get_constant(zend_string *name)
{
zend_constant *c = zend_get_constant_impl(name);
if (c) {
return &c->value;
}
return NULL;
}

ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
{
zend_constant *c;
Expand DownExpand Up@@ -402,7 +420,6 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}

/* non-class constant */
zval *value;
if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
/* compound constant name */
int prefix_len = colon - name;
Expand All@@ -423,28 +440,28 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
free_alloca(lcname, use_heap);

if (c) {
return &c->value;
}

if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
value = zend_get_constant_str(constant_name, const_name_len);
} else {
value = NULL;
if (!c) {
if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
c = zend_get_constant_str_impl(constant_name, const_name_len);
}
}
} else {
if (cname) {
value = zend_get_constant(cname);
c = zend_get_constant_impl(cname);
} else {
value = zend_get_constant_str(name, name_len);
c = zend_get_constant_str_impl(name, name_len);
}
}

if (!value && !(flags & ZEND_FETCH_CLASS_SILENT)) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
if (!c) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
} else if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
}
}
return value;
return &c->value;
}

static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
#define CONST_CS 0 /* No longer used -- always case sensitive */
#define CONST_PERSISTENT (1<<0) /* Persistent */
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand DownExpand Up@@ -86,9 +87,10 @@ ZEND_API int zend_register_constant(zend_constant *c);
void zend_copy_constants(HashTable *target, HashTable *sourc);
#endif

ZEND_API zval *_zend_get_special_const(const char *name, size_t name_len);
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t name_len);

static zend_always_inline zval *zend_get_special_const(const char *name, size_t name_len) {
static zend_always_inline zend_constant *zend_get_special_const(
const char *name, size_t name_len) {
if (name_len == 4 || name_len == 5) {
return _zend_get_special_const(name, name_len);
}
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -4301,6 +4301,10 @@ static zend_always_inline int _zend_quick_get_constant(

if (!check_defined_only) {
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
return SUCCESS;
}
}

CACHE_PTR(opline->extended_value, c);
Expand Down
8 changes: 4 additions & 4 deletions ext/opcache/Optimizer/block_pass.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,10 +33,10 @@
/* Checks if a constant (like "true") may be replaced by its value */
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
{
zval *zv;
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)
&& (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
|| !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
ZVAL_COPY_VALUE(result, &c->value);
Expand All@@ -50,9 +50,9 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
}

/* Special constants null/true/false can always be substituted. */
zv = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (zv) {
ZVAL_COPY_VALUE(result, zv);
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
ZVAL_COPY_VALUE(result, &c->value);
return 1;
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,8 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC);

zend_register_class_alias("_ZendTestClassAlias", zend_test_class);

REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
return SUCCESS;
}

Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
25 changes: 25 additions & 0 deletions Zend/tests/const_deprecation.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
--TEST--
Internal constant deprecation
--SKIPIF--
<?php
if (!extension_loaded('zend-test')) die('skip requires zend-test');
?>
--FILE--
<?php

var_dump(ZEND_TEST_DEPRECATED);
var_dump(constant('ZEND_TEST_DEPRECATED'));

const X = ZEND_TEST_DEPRECATED;
var_dump(X);

?>
--EXPECTF--
Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)
29 changes: 20 additions & 9 deletions Zend/zend_compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1409,15 +1409,27 @@ ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char
}
/* }}} */

static zend_bool can_ct_eval_const(zend_constant *c) {
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
return 0;
}
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
&& (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
return 1;
}
if (Z_TYPE(c->value) < IS_OBJECT
&& !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
return 1;
}
return 0;
}

static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c && (
((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
|| (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
)) {
if (c && can_ct_eval_const(c)) {
ZVAL_COPY_OR_DUP(zv, &c->value);
return 1;
}
Expand All@@ -1426,14 +1438,13 @@ static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool i
/* Substitute true, false and null (including unqualified usage in namespaces) */
const char *lookup_name = ZSTR_VAL(name);
size_t lookup_len = ZSTR_LEN(name);
zval *val;

if (!is_fully_qualified) {
zend_get_unqualified_name(name, &lookup_name, &lookup_len);
}

if ((val = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, val);
if ((c = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, &c->value);
return 1;
}

Expand Down
77 changes: 47 additions & 30 deletions Zend/zend_constants.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@
#define RESET_CONSTANT_VISITED(zv) Z_ACCESS_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK

/* Use for special null/true/false constants. */
static zval null_value, true_value, false_value;
static zend_constant *null_const, *true_const, *false_const;

void free_zend_constant(zval *zv)
{
Expand DownExpand Up@@ -138,9 +138,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

ZVAL_NULL(&null_value);
ZVAL_TRUE(&true_value);
ZVAL_FALSE(&false_value);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}


Expand DownExpand Up@@ -235,22 +235,22 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
}
}

ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
{
if (len == 4) {
if ((name[0] == 'n' || name[0] == 'N') &&
(name[1] == 'u' || name[1] == 'U') &&
(name[2] == 'l' || name[2] == 'L') &&
(name[3] == 'l' || name[3] == 'L')
) {
return &null_value;
return null_const;
}
if ((name[0] == 't' || name[0] == 'T') &&
(name[1] == 'r' || name[1] == 'R') &&
(name[2] == 'u' || name[2] == 'U') &&
(name[3] == 'e' || name[3] == 'E')
) {
return &true_value;
return true_const;
}
} else {
if ((name[0] == 'f' || name[0] == 'F') &&
Expand All@@ -259,10 +259,10 @@ ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
(name[3] == 's' || name[3] == 'S') &&
(name[4] == 'e' || name[4] == 'E')
) {
return &false_value;
return false_const;
}
}
return 0;
return NULL;
}
/* }}} */

Expand All@@ -279,36 +279,54 @@ ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *
}
/* }}} */

ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
if (c) {
return &c->value;
return c;
}

c = zend_get_halt_offset_constant(name, name_len);
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(name, name_len);
}

ZEND_API zval *zend_get_constant(zend_string *name)
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
zend_constant *c = zend_get_constant_str_impl(name, name_len);
if (c) {
return &c->value;
}
return NULL;
}

static zend_constant *zend_get_constant_impl(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
return c;
}

c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
}

ZEND_API zval *zend_get_constant(zend_string *name)
{
zend_constant *c = zend_get_constant_impl(name);
if (c) {
return &c->value;
}
return NULL;
}

ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
{
zend_constant *c;
Expand DownExpand Up@@ -402,7 +420,6 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}

/* non-class constant */
zval *value;
if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
/* compound constant name */
int prefix_len = colon - name;
Expand All@@ -423,28 +440,28 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
free_alloca(lcname, use_heap);

if (c) {
return &c->value;
}

if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
value = zend_get_constant_str(constant_name, const_name_len);
} else {
value = NULL;
if (!c) {
if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
c = zend_get_constant_str_impl(constant_name, const_name_len);
}
}
} else {
if (cname) {
value = zend_get_constant(cname);
c = zend_get_constant_impl(cname);
} else {
value = zend_get_constant_str(name, name_len);
c = zend_get_constant_str_impl(name, name_len);
}
}

if (!value && !(flags & ZEND_FETCH_CLASS_SILENT)) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
if (!c) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
} else if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
}
}
return value;
return &c->value;
}

static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
#define CONST_CS 0 /* No longer used -- always case sensitive */
#define CONST_PERSISTENT (1<<0) /* Persistent */
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand DownExpand Up@@ -86,9 +87,10 @@ ZEND_API int zend_register_constant(zend_constant *c);
void zend_copy_constants(HashTable *target, HashTable *sourc);
#endif

ZEND_API zval *_zend_get_special_const(const char *name, size_t name_len);
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t name_len);

static zend_always_inline zval *zend_get_special_const(const char *name, size_t name_len) {
static zend_always_inline zend_constant *zend_get_special_const(
const char *name, size_t name_len) {
if (name_len == 4 || name_len == 5) {
return _zend_get_special_const(name, name_len);
}
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -4301,6 +4301,10 @@ static zend_always_inline int _zend_quick_get_constant(

if (!check_defined_only) {
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
return SUCCESS;
}
}

CACHE_PTR(opline->extended_value, c);
Expand Down
8 changes: 4 additions & 4 deletions ext/opcache/Optimizer/block_pass.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,10 +33,10 @@
/* Checks if a constant (like "true") may be replaced by its value */
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
{
zval *zv;
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)
&& (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
|| !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
ZVAL_COPY_VALUE(result, &c->value);
Expand All@@ -50,9 +50,9 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
}

/* Special constants null/true/false can always be substituted. */
zv = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (zv) {
ZVAL_COPY_VALUE(result, zv);
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
ZVAL_COPY_VALUE(result, &c->value);
return 1;
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,8 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC);

zend_register_class_alias("_ZendTestClassAlias", zend_test_class);

REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
return SUCCESS;
}

Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
25 changes: 25 additions & 0 deletions Zend/tests/const_deprecation.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
--TEST--
Internal constant deprecation
--SKIPIF--
<?php
if (!extension_loaded('zend-test')) die('skip requires zend-test');
?>
--FILE--
<?php

var_dump(ZEND_TEST_DEPRECATED);
var_dump(constant('ZEND_TEST_DEPRECATED'));

const X = ZEND_TEST_DEPRECATED;
var_dump(X);

?>
--EXPECTF--
Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)
29 changes: 20 additions & 9 deletions Zend/zend_compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1409,15 +1409,27 @@ ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char
}
/* }}} */

static zend_bool can_ct_eval_const(zend_constant *c) {
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
return 0;
}
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
&& (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
return 1;
}
if (Z_TYPE(c->value) < IS_OBJECT
&& !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
return 1;
}
return 0;
}

static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c && (
((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
|| (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
)) {
if (c && can_ct_eval_const(c)) {
ZVAL_COPY_OR_DUP(zv, &c->value);
return 1;
}
Expand All@@ -1426,14 +1438,13 @@ static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool i
/* Substitute true, false and null (including unqualified usage in namespaces) */
const char *lookup_name = ZSTR_VAL(name);
size_t lookup_len = ZSTR_LEN(name);
zval *val;

if (!is_fully_qualified) {
zend_get_unqualified_name(name, &lookup_name, &lookup_len);
}

if ((val = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, val);
if ((c = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, &c->value);
return 1;
}

Expand Down
77 changes: 47 additions & 30 deletions Zend/zend_constants.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@
#define RESET_CONSTANT_VISITED(zv) Z_ACCESS_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK

/* Use for special null/true/false constants. */
static zval null_value, true_value, false_value;
static zend_constant *null_const, *true_const, *false_const;

void free_zend_constant(zval *zv)
{
Expand DownExpand Up@@ -138,9 +138,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

ZVAL_NULL(&null_value);
ZVAL_TRUE(&true_value);
ZVAL_FALSE(&false_value);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}


Expand DownExpand Up@@ -235,22 +235,22 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
}
}

ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
{
if (len == 4) {
if ((name[0] == 'n' || name[0] == 'N') &&
(name[1] == 'u' || name[1] == 'U') &&
(name[2] == 'l' || name[2] == 'L') &&
(name[3] == 'l' || name[3] == 'L')
) {
return &null_value;
return null_const;
}
if ((name[0] == 't' || name[0] == 'T') &&
(name[1] == 'r' || name[1] == 'R') &&
(name[2] == 'u' || name[2] == 'U') &&
(name[3] == 'e' || name[3] == 'E')
) {
return &true_value;
return true_const;
}
} else {
if ((name[0] == 'f' || name[0] == 'F') &&
Expand All@@ -259,10 +259,10 @@ ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
(name[3] == 's' || name[3] == 'S') &&
(name[4] == 'e' || name[4] == 'E')
) {
return &false_value;
return false_const;
}
}
return 0;
return NULL;
}
/* }}} */

Expand All@@ -279,36 +279,54 @@ ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *
}
/* }}} */

ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
if (c) {
return &c->value;
return c;
}

c = zend_get_halt_offset_constant(name, name_len);
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(name, name_len);
}

ZEND_API zval *zend_get_constant(zend_string *name)
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
zend_constant *c = zend_get_constant_str_impl(name, name_len);
if (c) {
return &c->value;
}
return NULL;
}

static zend_constant *zend_get_constant_impl(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
return c;
}

c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
}

ZEND_API zval *zend_get_constant(zend_string *name)
{
zend_constant *c = zend_get_constant_impl(name);
if (c) {
return &c->value;
}
return NULL;
}

ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
{
zend_constant *c;
Expand DownExpand Up@@ -402,7 +420,6 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}

/* non-class constant */
zval *value;
if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
/* compound constant name */
int prefix_len = colon - name;
Expand All@@ -423,28 +440,28 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
free_alloca(lcname, use_heap);

if (c) {
return &c->value;
}

if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
value = zend_get_constant_str(constant_name, const_name_len);
} else {
value = NULL;
if (!c) {
if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
c = zend_get_constant_str_impl(constant_name, const_name_len);
}
}
} else {
if (cname) {
value = zend_get_constant(cname);
c = zend_get_constant_impl(cname);
} else {
value = zend_get_constant_str(name, name_len);
c = zend_get_constant_str_impl(name, name_len);
}
}

if (!value && !(flags & ZEND_FETCH_CLASS_SILENT)) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
if (!c) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
} else if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
}
}
return value;
return &c->value;
}

static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
#define CONST_CS 0 /* No longer used -- always case sensitive */
#define CONST_PERSISTENT (1<<0) /* Persistent */
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand DownExpand Up@@ -86,9 +87,10 @@ ZEND_API int zend_register_constant(zend_constant *c);
void zend_copy_constants(HashTable *target, HashTable *sourc);
#endif

ZEND_API zval *_zend_get_special_const(const char *name, size_t name_len);
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t name_len);

static zend_always_inline zval *zend_get_special_const(const char *name, size_t name_len) {
static zend_always_inline zend_constant *zend_get_special_const(
const char *name, size_t name_len) {
if (name_len == 4 || name_len == 5) {
return _zend_get_special_const(name, name_len);
}
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -4301,6 +4301,10 @@ static zend_always_inline int _zend_quick_get_constant(

if (!check_defined_only) {
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
return SUCCESS;
}
}

CACHE_PTR(opline->extended_value, c);
Expand Down
8 changes: 4 additions & 4 deletions ext/opcache/Optimizer/block_pass.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,10 +33,10 @@
/* Checks if a constant (like "true") may be replaced by its value */
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
{
zval *zv;
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)
&& (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
|| !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
ZVAL_COPY_VALUE(result, &c->value);
Expand All@@ -50,9 +50,9 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
}

/* Special constants null/true/false can always be substituted. */
zv = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (zv) {
ZVAL_COPY_VALUE(result, zv);
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
ZVAL_COPY_VALUE(result, &c->value);
return 1;
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,8 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC);

zend_register_class_alias("_ZendTestClassAlias", zend_test_class);

REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
return SUCCESS;
}

Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
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
25 changes: 25 additions & 0 deletions Zend/tests/const_deprecation.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
--TEST--
Internal constant deprecation
--SKIPIF--
<?php
if (!extension_loaded('zend-test')) die('skip requires zend-test');
?>
--FILE--
<?php

var_dump(ZEND_TEST_DEPRECATED);
var_dump(constant('ZEND_TEST_DEPRECATED'));

const X = ZEND_TEST_DEPRECATED;
var_dump(X);

?>
--EXPECTF--
Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)
29 changes: 20 additions & 9 deletions Zend/zend_compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1409,15 +1409,27 @@ ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char
}
/* }}} */

static zend_bool can_ct_eval_const(zend_constant *c) {
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
return 0;
}
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
&& (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
return 1;
}
if (Z_TYPE(c->value) < IS_OBJECT
&& !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
return 1;
}
return 0;
}

static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c && (
((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
|| (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
)) {
if (c && can_ct_eval_const(c)) {
ZVAL_COPY_OR_DUP(zv, &c->value);
return 1;
}
Expand All@@ -1426,14 +1438,13 @@ static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool i
/* Substitute true, false and null (including unqualified usage in namespaces) */
const char *lookup_name = ZSTR_VAL(name);
size_t lookup_len = ZSTR_LEN(name);
zval *val;

if (!is_fully_qualified) {
zend_get_unqualified_name(name, &lookup_name, &lookup_len);
}

if ((val = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, val);
if ((c = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, &c->value);
return 1;
}

Expand Down
77 changes: 47 additions & 30 deletions Zend/zend_constants.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@
#define RESET_CONSTANT_VISITED(zv) Z_ACCESS_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK

/* Use for special null/true/false constants. */
static zval null_value, true_value, false_value;
static zend_constant *null_const, *true_const, *false_const;

void free_zend_constant(zval *zv)
{
Expand DownExpand Up@@ -138,9 +138,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

ZVAL_NULL(&null_value);
ZVAL_TRUE(&true_value);
ZVAL_FALSE(&false_value);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}


Expand DownExpand Up@@ -235,22 +235,22 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
}
}

ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
{
if (len == 4) {
if ((name[0] == 'n' || name[0] == 'N') &&
(name[1] == 'u' || name[1] == 'U') &&
(name[2] == 'l' || name[2] == 'L') &&
(name[3] == 'l' || name[3] == 'L')
) {
return &null_value;
return null_const;
}
if ((name[0] == 't' || name[0] == 'T') &&
(name[1] == 'r' || name[1] == 'R') &&
(name[2] == 'u' || name[2] == 'U') &&
(name[3] == 'e' || name[3] == 'E')
) {
return &true_value;
return true_const;
}
} else {
if ((name[0] == 'f' || name[0] == 'F') &&
Expand All@@ -259,10 +259,10 @@ ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
(name[3] == 's' || name[3] == 'S') &&
(name[4] == 'e' || name[4] == 'E')
) {
return &false_value;
return false_const;
}
}
return 0;
return NULL;
}
/* }}} */

Expand All@@ -279,36 +279,54 @@ ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *
}
/* }}} */

ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
if (c) {
return &c->value;
return c;
}

c = zend_get_halt_offset_constant(name, name_len);
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(name, name_len);
}

ZEND_API zval *zend_get_constant(zend_string *name)
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
zend_constant *c = zend_get_constant_str_impl(name, name_len);
if (c) {
return &c->value;
}
return NULL;
}

static zend_constant *zend_get_constant_impl(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
return c;
}

c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
}

ZEND_API zval *zend_get_constant(zend_string *name)
{
zend_constant *c = zend_get_constant_impl(name);
if (c) {
return &c->value;
}
return NULL;
}

ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
{
zend_constant *c;
Expand DownExpand Up@@ -402,7 +420,6 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}

/* non-class constant */
zval *value;
if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
/* compound constant name */
int prefix_len = colon - name;
Expand All@@ -423,28 +440,28 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
free_alloca(lcname, use_heap);

if (c) {
return &c->value;
}

if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
value = zend_get_constant_str(constant_name, const_name_len);
} else {
value = NULL;
if (!c) {
if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
c = zend_get_constant_str_impl(constant_name, const_name_len);
}
}
} else {
if (cname) {
value = zend_get_constant(cname);
c = zend_get_constant_impl(cname);
} else {
value = zend_get_constant_str(name, name_len);
c = zend_get_constant_str_impl(name, name_len);
}
}

if (!value && !(flags & ZEND_FETCH_CLASS_SILENT)) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
if (!c) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
} else if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
}
}
return value;
return &c->value;
}

static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
#define CONST_CS 0 /* No longer used -- always case sensitive */
#define CONST_PERSISTENT (1<<0) /* Persistent */
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand DownExpand Up@@ -86,9 +87,10 @@ ZEND_API int zend_register_constant(zend_constant *c);
void zend_copy_constants(HashTable *target, HashTable *sourc);
#endif

ZEND_API zval *_zend_get_special_const(const char *name, size_t name_len);
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t name_len);

static zend_always_inline zval *zend_get_special_const(const char *name, size_t name_len) {
static zend_always_inline zend_constant *zend_get_special_const(
const char *name, size_t name_len) {
if (name_len == 4 || name_len == 5) {
return _zend_get_special_const(name, name_len);
}
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -4301,6 +4301,10 @@ static zend_always_inline int _zend_quick_get_constant(

if (!check_defined_only) {
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
return SUCCESS;
}
}

CACHE_PTR(opline->extended_value, c);
Expand Down
8 changes: 4 additions & 4 deletions ext/opcache/Optimizer/block_pass.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,10 +33,10 @@
/* Checks if a constant (like "true") may be replaced by its value */
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
{
zval *zv;
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)
&& (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
|| !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
ZVAL_COPY_VALUE(result, &c->value);
Expand All@@ -50,9 +50,9 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
}

/* Special constants null/true/false can always be substituted. */
zv = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (zv) {
ZVAL_COPY_VALUE(result, zv);
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
ZVAL_COPY_VALUE(result, &c->value);
return 1;
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,8 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC);

zend_register_class_alias("_ZendTestClassAlias", zend_test_class);

REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
return SUCCESS;
}

Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
25 changes: 25 additions & 0 deletions Zend/tests/const_deprecation.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
--TEST--
Internal constant deprecation
--SKIPIF--
<?php
if (!extension_loaded('zend-test')) die('skip requires zend-test');
?>
--FILE--
<?php

var_dump(ZEND_TEST_DEPRECATED);
var_dump(constant('ZEND_TEST_DEPRECATED'));

const X = ZEND_TEST_DEPRECATED;
var_dump(X);

?>
--EXPECTF--
Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)
29 changes: 20 additions & 9 deletions Zend/zend_compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1409,15 +1409,27 @@ ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char
}
/* }}} */

static zend_bool can_ct_eval_const(zend_constant *c) {
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
return 0;
}
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
&& (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
return 1;
}
if (Z_TYPE(c->value) < IS_OBJECT
&& !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
return 1;
}
return 0;
}

static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c && (
((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
|| (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
)) {
if (c && can_ct_eval_const(c)) {
ZVAL_COPY_OR_DUP(zv, &c->value);
return 1;
}
Expand All@@ -1426,14 +1438,13 @@ static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool i
/* Substitute true, false and null (including unqualified usage in namespaces) */
const char *lookup_name = ZSTR_VAL(name);
size_t lookup_len = ZSTR_LEN(name);
zval *val;

if (!is_fully_qualified) {
zend_get_unqualified_name(name, &lookup_name, &lookup_len);
}

if ((val = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, val);
if ((c = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, &c->value);
return 1;
}

Expand Down
77 changes: 47 additions & 30 deletions Zend/zend_constants.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@
#define RESET_CONSTANT_VISITED(zv) Z_ACCESS_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK

/* Use for special null/true/false constants. */
static zval null_value, true_value, false_value;
static zend_constant *null_const, *true_const, *false_const;

void free_zend_constant(zval *zv)
{
Expand DownExpand Up@@ -138,9 +138,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

ZVAL_NULL(&null_value);
ZVAL_TRUE(&true_value);
ZVAL_FALSE(&false_value);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}


Expand DownExpand Up@@ -235,22 +235,22 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
}
}

ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
{
if (len == 4) {
if ((name[0] == 'n' || name[0] == 'N') &&
(name[1] == 'u' || name[1] == 'U') &&
(name[2] == 'l' || name[2] == 'L') &&
(name[3] == 'l' || name[3] == 'L')
) {
return &null_value;
return null_const;
}
if ((name[0] == 't' || name[0] == 'T') &&
(name[1] == 'r' || name[1] == 'R') &&
(name[2] == 'u' || name[2] == 'U') &&
(name[3] == 'e' || name[3] == 'E')
) {
return &true_value;
return true_const;
}
} else {
if ((name[0] == 'f' || name[0] == 'F') &&
Expand All@@ -259,10 +259,10 @@ ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
(name[3] == 's' || name[3] == 'S') &&
(name[4] == 'e' || name[4] == 'E')
) {
return &false_value;
return false_const;
}
}
return 0;
return NULL;
}
/* }}} */

Expand All@@ -279,36 +279,54 @@ ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *
}
/* }}} */

ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
if (c) {
return &c->value;
return c;
}

c = zend_get_halt_offset_constant(name, name_len);
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(name, name_len);
}

ZEND_API zval *zend_get_constant(zend_string *name)
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
zend_constant *c = zend_get_constant_str_impl(name, name_len);
if (c) {
return &c->value;
}
return NULL;
}

static zend_constant *zend_get_constant_impl(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
return c;
}

c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
}

ZEND_API zval *zend_get_constant(zend_string *name)
{
zend_constant *c = zend_get_constant_impl(name);
if (c) {
return &c->value;
}
return NULL;
}

ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
{
zend_constant *c;
Expand DownExpand Up@@ -402,7 +420,6 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}

/* non-class constant */
zval *value;
if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
/* compound constant name */
int prefix_len = colon - name;
Expand All@@ -423,28 +440,28 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
free_alloca(lcname, use_heap);

if (c) {
return &c->value;
}

if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
value = zend_get_constant_str(constant_name, const_name_len);
} else {
value = NULL;
if (!c) {
if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
c = zend_get_constant_str_impl(constant_name, const_name_len);
}
}
} else {
if (cname) {
value = zend_get_constant(cname);
c = zend_get_constant_impl(cname);
} else {
value = zend_get_constant_str(name, name_len);
c = zend_get_constant_str_impl(name, name_len);
}
}

if (!value && !(flags & ZEND_FETCH_CLASS_SILENT)) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
if (!c) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
} else if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
}
}
return value;
return &c->value;
}

static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
#define CONST_CS 0 /* No longer used -- always case sensitive */
#define CONST_PERSISTENT (1<<0) /* Persistent */
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand DownExpand Up@@ -86,9 +87,10 @@ ZEND_API int zend_register_constant(zend_constant *c);
void zend_copy_constants(HashTable *target, HashTable *sourc);
#endif

ZEND_API zval *_zend_get_special_const(const char *name, size_t name_len);
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t name_len);

static zend_always_inline zval *zend_get_special_const(const char *name, size_t name_len) {
static zend_always_inline zend_constant *zend_get_special_const(
const char *name, size_t name_len) {
if (name_len == 4 || name_len == 5) {
return _zend_get_special_const(name, name_len);
}
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -4301,6 +4301,10 @@ static zend_always_inline int _zend_quick_get_constant(

if (!check_defined_only) {
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
return SUCCESS;
}
}

CACHE_PTR(opline->extended_value, c);
Expand Down
8 changes: 4 additions & 4 deletions ext/opcache/Optimizer/block_pass.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,10 +33,10 @@
/* Checks if a constant (like "true") may be replaced by its value */
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
{
zval *zv;
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)
&& (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
|| !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
ZVAL_COPY_VALUE(result, &c->value);
Expand All@@ -50,9 +50,9 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
}

/* Special constants null/true/false can always be substituted. */
zv = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (zv) {
ZVAL_COPY_VALUE(result, zv);
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
ZVAL_COPY_VALUE(result, &c->value);
return 1;
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,8 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC);

zend_register_class_alias("_ZendTestClassAlias", zend_test_class);

REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
return SUCCESS;
}

Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
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
25 changes: 25 additions & 0 deletions Zend/tests/const_deprecation.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
--TEST--
Internal constant deprecation
--SKIPIF--
<?php
if (!extension_loaded('zend-test')) die('skip requires zend-test');
?>
--FILE--
<?php

var_dump(ZEND_TEST_DEPRECATED);
var_dump(constant('ZEND_TEST_DEPRECATED'));

const X = ZEND_TEST_DEPRECATED;
var_dump(X);

?>
--EXPECTF--
Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)
29 changes: 20 additions & 9 deletions Zend/zend_compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1409,15 +1409,27 @@ ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char
}
/* }}} */

static zend_bool can_ct_eval_const(zend_constant *c) {
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
return 0;
}
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
&& (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
return 1;
}
if (Z_TYPE(c->value) < IS_OBJECT
&& !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
return 1;
}
return 0;
}

static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c && (
((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
|| (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
)) {
if (c && can_ct_eval_const(c)) {
ZVAL_COPY_OR_DUP(zv, &c->value);
return 1;
}
Expand All@@ -1426,14 +1438,13 @@ static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool i
/* Substitute true, false and null (including unqualified usage in namespaces) */
const char *lookup_name = ZSTR_VAL(name);
size_t lookup_len = ZSTR_LEN(name);
zval *val;

if (!is_fully_qualified) {
zend_get_unqualified_name(name, &lookup_name, &lookup_len);
}

if ((val = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, val);
if ((c = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, &c->value);
return 1;
}

Expand Down
77 changes: 47 additions & 30 deletions Zend/zend_constants.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@
#define RESET_CONSTANT_VISITED(zv) Z_ACCESS_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK

/* Use for special null/true/false constants. */
static zval null_value, true_value, false_value;
static zend_constant *null_const, *true_const, *false_const;

void free_zend_constant(zval *zv)
{
Expand DownExpand Up@@ -138,9 +138,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

ZVAL_NULL(&null_value);
ZVAL_TRUE(&true_value);
ZVAL_FALSE(&false_value);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}


Expand DownExpand Up@@ -235,22 +235,22 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
}
}

ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
{
if (len == 4) {
if ((name[0] == 'n' || name[0] == 'N') &&
(name[1] == 'u' || name[1] == 'U') &&
(name[2] == 'l' || name[2] == 'L') &&
(name[3] == 'l' || name[3] == 'L')
) {
return &null_value;
return null_const;
}
if ((name[0] == 't' || name[0] == 'T') &&
(name[1] == 'r' || name[1] == 'R') &&
(name[2] == 'u' || name[2] == 'U') &&
(name[3] == 'e' || name[3] == 'E')
) {
return &true_value;
return true_const;
}
} else {
if ((name[0] == 'f' || name[0] == 'F') &&
Expand All@@ -259,10 +259,10 @@ ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
(name[3] == 's' || name[3] == 'S') &&
(name[4] == 'e' || name[4] == 'E')
) {
return &false_value;
return false_const;
}
}
return 0;
return NULL;
}
/* }}} */

Expand All@@ -279,36 +279,54 @@ ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *
}
/* }}} */

ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
if (c) {
return &c->value;
return c;
}

c = zend_get_halt_offset_constant(name, name_len);
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(name, name_len);
}

ZEND_API zval *zend_get_constant(zend_string *name)
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
zend_constant *c = zend_get_constant_str_impl(name, name_len);
if (c) {
return &c->value;
}
return NULL;
}

static zend_constant *zend_get_constant_impl(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
return c;
}

c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
}

ZEND_API zval *zend_get_constant(zend_string *name)
{
zend_constant *c = zend_get_constant_impl(name);
if (c) {
return &c->value;
}
return NULL;
}

ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
{
zend_constant *c;
Expand DownExpand Up@@ -402,7 +420,6 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}

/* non-class constant */
zval *value;
if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
/* compound constant name */
int prefix_len = colon - name;
Expand All@@ -423,28 +440,28 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
free_alloca(lcname, use_heap);

if (c) {
return &c->value;
}

if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
value = zend_get_constant_str(constant_name, const_name_len);
} else {
value = NULL;
if (!c) {
if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
c = zend_get_constant_str_impl(constant_name, const_name_len);
}
}
} else {
if (cname) {
value = zend_get_constant(cname);
c = zend_get_constant_impl(cname);
} else {
value = zend_get_constant_str(name, name_len);
c = zend_get_constant_str_impl(name, name_len);
}
}

if (!value && !(flags & ZEND_FETCH_CLASS_SILENT)) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
if (!c) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
} else if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
}
}
return value;
return &c->value;
}

static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
#define CONST_CS 0 /* No longer used -- always case sensitive */
#define CONST_PERSISTENT (1<<0) /* Persistent */
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand DownExpand Up@@ -86,9 +87,10 @@ ZEND_API int zend_register_constant(zend_constant *c);
void zend_copy_constants(HashTable *target, HashTable *sourc);
#endif

ZEND_API zval *_zend_get_special_const(const char *name, size_t name_len);
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t name_len);

static zend_always_inline zval *zend_get_special_const(const char *name, size_t name_len) {
static zend_always_inline zend_constant *zend_get_special_const(
const char *name, size_t name_len) {
if (name_len == 4 || name_len == 5) {
return _zend_get_special_const(name, name_len);
}
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -4301,6 +4301,10 @@ static zend_always_inline int _zend_quick_get_constant(

if (!check_defined_only) {
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
return SUCCESS;
}
}

CACHE_PTR(opline->extended_value, c);
Expand Down
8 changes: 4 additions & 4 deletions ext/opcache/Optimizer/block_pass.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,10 +33,10 @@
/* Checks if a constant (like "true") may be replaced by its value */
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
{
zval *zv;
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)
&& (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
|| !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
ZVAL_COPY_VALUE(result, &c->value);
Expand All@@ -50,9 +50,9 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
}

/* Special constants null/true/false can always be substituted. */
zv = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (zv) {
ZVAL_COPY_VALUE(result, zv);
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
ZVAL_COPY_VALUE(result, &c->value);
return 1;
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,8 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC);

zend_register_class_alias("_ZendTestClassAlias", zend_test_class);

REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
return SUCCESS;
}

Expand Down
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
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
25 changes: 25 additions & 0 deletions Zend/tests/const_deprecation.phpt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
--TEST--
Internal constant deprecation
--SKIPIF--
<?php
if (!extension_loaded('zend-test')) die('skip requires zend-test');
?>
--FILE--
<?php

var_dump(ZEND_TEST_DEPRECATED);
var_dump(constant('ZEND_TEST_DEPRECATED'));

const X = ZEND_TEST_DEPRECATED;
var_dump(X);

?>
--EXPECTF--
Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)

Deprecated: Constant ZEND_TEST_DEPRECATED is deprecated in %s on line %d
int(42)
29 changes: 20 additions & 9 deletions Zend/zend_compile.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -1409,15 +1409,27 @@ ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char
}
/* }}} */

static zend_bool can_ct_eval_const(zend_constant *c) {
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
return 0;
}
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
&& (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
return 1;
}
if (Z_TYPE(c->value) < IS_OBJECT
&& !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
return 1;
}
return 0;
}

static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c && (
((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
&& !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
|| (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
)) {
if (c && can_ct_eval_const(c)) {
ZVAL_COPY_OR_DUP(zv, &c->value);
return 1;
}
Expand All@@ -1426,14 +1438,13 @@ static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool i
/* Substitute true, false and null (including unqualified usage in namespaces) */
const char *lookup_name = ZSTR_VAL(name);
size_t lookup_len = ZSTR_LEN(name);
zval *val;

if (!is_fully_qualified) {
zend_get_unqualified_name(name, &lookup_name, &lookup_len);
}

if ((val = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, val);
if ((c = zend_get_special_const(lookup_name, lookup_len))) {
ZVAL_COPY_VALUE(zv, &c->value);
return 1;
}

Expand Down
77 changes: 47 additions & 30 deletions Zend/zend_constants.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@
#define RESET_CONSTANT_VISITED(zv) Z_ACCESS_FLAGS_P(zv) &= ~IS_CONSTANT_VISITED_MARK

/* Use for special null/true/false constants. */
static zval null_value, true_value, false_value;
static zend_constant *null_const, *true_const, *false_const;

void free_zend_constant(zval *zv)
{
Expand DownExpand Up@@ -138,9 +138,9 @@ void zend_register_standard_constants(void)
REGISTER_MAIN_BOOL_CONSTANT("FALSE", 0, CONST_PERSISTENT);
REGISTER_MAIN_NULL_CONSTANT("NULL", CONST_PERSISTENT);

ZVAL_NULL(&null_value);
ZVAL_TRUE(&true_value);
ZVAL_FALSE(&false_value);
true_const = zend_hash_str_find_ptr(EG(zend_constants), "TRUE", sizeof("TRUE")-1);
false_const = zend_hash_str_find_ptr(EG(zend_constants), "FALSE", sizeof("FALSE")-1);
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}


Expand DownExpand Up@@ -235,22 +235,22 @@ static zend_constant *zend_get_halt_offset_constant(const char *name, size_t nam
}
}

ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t len) /* {{{ */
{
if (len == 4) {
if ((name[0] == 'n' || name[0] == 'N') &&
(name[1] == 'u' || name[1] == 'U') &&
(name[2] == 'l' || name[2] == 'L') &&
(name[3] == 'l' || name[3] == 'L')
) {
return &null_value;
return null_const;
}
if ((name[0] == 't' || name[0] == 'T') &&
(name[1] == 'r' || name[1] == 'R') &&
(name[2] == 'u' || name[2] == 'U') &&
(name[3] == 'e' || name[3] == 'E')
) {
return &true_value;
return true_const;
}
} else {
if ((name[0] == 'f' || name[0] == 'F') &&
Expand All@@ -259,10 +259,10 @@ ZEND_API zval *_zend_get_special_const(const char *name, size_t len) /* {{{ */
(name[3] == 's' || name[3] == 'S') &&
(name[4] == 'e' || name[4] == 'E')
) {
return &false_value;
return false_const;
}
}
return 0;
return NULL;
}
/* }}} */

Expand All@@ -279,36 +279,54 @@ ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *
}
/* }}} */

ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
static zend_constant *zend_get_constant_str_impl(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_str_find_ptr(EG(zend_constants), name, name_len);
if (c) {
return &c->value;
return c;
}

c = zend_get_halt_offset_constant(name, name_len);
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(name, name_len);
}

ZEND_API zval *zend_get_constant(zend_string *name)
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
zend_constant *c = zend_get_constant_str_impl(name, name_len);
if (c) {
return &c->value;
}
return NULL;
}

static zend_constant *zend_get_constant_impl(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
return c;
}

c = zend_get_halt_offset_constant(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
return &c->value;
return c;
}

return zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
}

ZEND_API zval *zend_get_constant(zend_string *name)
{
zend_constant *c = zend_get_constant_impl(name);
if (c) {
return &c->value;
}
return NULL;
}

ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope, uint32_t flags)
{
zend_constant *c;
Expand DownExpand Up@@ -402,7 +420,6 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}

/* non-class constant */
zval *value;
if ((colon = zend_memrchr(name, '\\', name_len)) != NULL) {
/* compound constant name */
int prefix_len = colon - name;
Expand All@@ -423,28 +440,28 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
c = zend_hash_str_find_ptr(EG(zend_constants), lcname, lcname_len);
free_alloca(lcname, use_heap);

if (c) {
return &c->value;
}

if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
value = zend_get_constant_str(constant_name, const_name_len);
} else {
value = NULL;
if (!c) {
if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
/* name requires runtime resolution, need to check non-namespaced name */
c = zend_get_constant_str_impl(constant_name, const_name_len);
}
}
} else {
if (cname) {
value = zend_get_constant(cname);
c = zend_get_constant_impl(cname);
} else {
value = zend_get_constant_str(name, name_len);
c = zend_get_constant_str_impl(name, name_len);
}
}

if (!value && !(flags & ZEND_FETCH_CLASS_SILENT)) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
if (!(flags & ZEND_FETCH_CLASS_SILENT)) {
if (!c) {
zend_throw_error(NULL, "Undefined constant '%s'", name);
} else if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", name);
}
}
return value;
return &c->value;
}

static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_constant *c)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
#define CONST_CS 0 /* No longer used -- always case sensitive */
#define CONST_PERSISTENT (1<<0) /* Persistent */
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand DownExpand Up@@ -86,9 +87,10 @@ ZEND_API int zend_register_constant(zend_constant *c);
void zend_copy_constants(HashTable *target, HashTable *sourc);
#endif

ZEND_API zval *_zend_get_special_const(const char *name, size_t name_len);
ZEND_API zend_constant *_zend_get_special_const(const char *name, size_t name_len);

static zend_always_inline zval *zend_get_special_const(const char *name, size_t name_len) {
static zend_always_inline zend_constant *zend_get_special_const(
const char *name, size_t name_len) {
if (name_len == 4 || name_len == 5) {
return _zend_get_special_const(name, name_len);
}
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_execute.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -4301,6 +4301,10 @@ static zend_always_inline int _zend_quick_get_constant(

if (!check_defined_only) {
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
return SUCCESS;
}
}

CACHE_PTR(opline->extended_value, c);
Expand Down
8 changes: 4 additions & 4 deletions ext/opcache/Optimizer/block_pass.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,10 +33,10 @@
/* Checks if a constant (like "true") may be replaced by its value */
int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int copy)
{
zval *zv;
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
&& !(ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED)
&& (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
|| !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
ZVAL_COPY_VALUE(result, &c->value);
Expand All@@ -50,9 +50,9 @@ int zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
}

/* Special constants null/true/false can always be substituted. */
zv = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (zv) {
ZVAL_COPY_VALUE(result, zv);
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
if (c) {
ZVAL_COPY_VALUE(result, &c->value);
return 1;
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -301,6 +301,8 @@ PHP_MINIT_FUNCTION(zend_test)
zend_declare_property_null(zend_test_trait, "testProp", sizeof("testProp")-1, ZEND_ACC_PUBLIC);

zend_register_class_alias("_ZendTestClassAlias", zend_test_class);

REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
return SUCCESS;
}

Expand Down