Skip to content

The **const** modifier needs to be added to read-only parameters of all ZendAPI and PHPAPI functions #19523

@matyhtf

Description

@matyhtf

Description

Many functions in the php source code receive the zval parameter. The value of the parameter is not modified in the function implementation. These parameters are read-only, but the const modifier is not added.

As a result, C/C++underlying developers do not know whether the function is a read-only operation or a write operation, and will pollute all downstream functions. If these PHPAPI or ZEND_API functions are used in a C++function, even if the function is designed to be read-only, the const modifier cannot be used, or only unsafe const_cast can be used to cast.

An example:

class Variant {
  protected:
    zval val;
    void destroy() {
        zval_ptr_dtor(&val);
    }

  public:
    Variant(const zval *v) noexcept {
        ZVAL_COPY_VALUE(&val, v);
    }
    const zval *const_ptr() const {
        return &val;
    }
    // fast_equal_check_function should be `fast_equal_check_function(const zval *op1, const zval *op2)`
     bool equals(const Variant &v) const {
        return fast_equal_check_function(const_ptr(), v.const_ptr());         
    }
}

Problem function:

  • fast_equal_check_function
  • php_debug_zval_dump
  • php_var_dump
  • php_var_unserialize
  • concat_function
  • add_function
  • is_equal_function
  • zval_get_string
  • zval_get_double // version 8.1

... a lot more functions

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions