From 5890384df26c9480f9376b2a9aa8c9b8f95c4351 Mon Sep 17 00:00:00 2001 From: Tomas Ebenlendr Date: Tue, 16 Apr 2024 09:27:40 +0200 Subject: [PATCH 1/2] Disable automatic conversions from void * to bool. It leads to various bugs, e.g., when "struct * _zend_string" is passed where zval was in php < 8.0. --- include/class.h | 1 + include/classbase.h | 1 + include/constant.h | 1 + include/hashmember.h | 10 ++++++++++ include/ini.h | 1 + include/value.h | 16 ++++++++++++++++ 6 files changed, 30 insertions(+) diff --git a/include/class.h b/include/class.h index e18220ad..d3f33673 100644 --- a/include/class.h +++ b/include/class.h @@ -227,6 +227,7 @@ class PHPCPP_EXPORT Class : private ClassBase Class &property(const char *name, const char *value, int flags = Public) { ClassBase::property(name, value, flags); return *this; } Class &property(const char *name, const std::string &value, int flags = Public) { ClassBase::property(name, value, flags); return *this; } Class &property(const char *name, bool value, int flags = Public) { ClassBase::property(name, value, flags); return *this; } + Class &property(const char *name, const void *value, int flags = Public) = delete; Class &property(const char *name, double value, int flags = Public) { ClassBase::property(name, value, flags); return *this; } /** diff --git a/include/classbase.h b/include/classbase.h index 3e010719..dfa2f38b 100644 --- a/include/classbase.h +++ b/include/classbase.h @@ -273,6 +273,7 @@ class PHPCPP_EXPORT ClassBase void property(const char *name, int32_t value, int flags = Php::Public); void property(const char *name, int64_t value, int flags = Php::Public); void property(const char *name, bool value, int flags = Php::Public); + void property(const char *name, const void *value, int flags = Php::Public) = delete; void property(const char *name, char value, int flags = Php::Public); void property(const char *name, const std::string &value, int flags = Php::Public); void property(const char *name, const char *value, int flags = Php::Public); diff --git a/include/constant.h b/include/constant.h index bb3a2d89..f90966a2 100644 --- a/include/constant.h +++ b/include/constant.h @@ -44,6 +44,7 @@ class PHPCPP_EXPORT Constant * @param value Constant's value */ Constant(const char *name, bool value); + Constant(const char *name, const void *value) = delete; /** * Constructor to create a constant for a 32-bit integer diff --git a/include/hashmember.h b/include/hashmember.h index 378ee905..6e8d458d 100644 --- a/include/hashmember.h +++ b/include/hashmember.h @@ -191,6 +191,7 @@ class PHPCPP_EXPORT HashMember : private HashParent HashMember &operator+=(int32_t value) { return operator=(this->value() + value); } HashMember &operator+=(int64_t value) { return operator=(this->value() + value); } HashMember &operator+=(bool value) { return operator=(this->value() + value); } + HashMember &operator+=(const void *value) = delete; HashMember &operator+=(char value) { return operator=(this->value() + value); } HashMember &operator+=(const std::string &value) { return operator=(this->value() + value); } HashMember &operator+=(const char *value) { return operator=(this->value() + value); } @@ -206,6 +207,7 @@ class PHPCPP_EXPORT HashMember : private HashParent HashMember &operator-=(int32_t value) { return operator=(this->value() - value); } HashMember &operator-=(int64_t value) { return operator=(this->value() - value); } HashMember &operator-=(bool value) { return operator=(this->value() - value); } + HashMember &operator-=(const void *value) = delete; HashMember &operator-=(char value) { return operator=(this->value() - value); } HashMember &operator-=(const std::string &value) { return operator=(this->value() - value); } HashMember &operator-=(const char *value) { return operator=(this->value() - value); } @@ -221,6 +223,7 @@ class PHPCPP_EXPORT HashMember : private HashParent HashMember &operator*=(int32_t value) { return operator=(this->value() * value); } HashMember &operator*=(int64_t value) { return operator=(this->value() * value); } HashMember &operator*=(bool value) { return operator=(this->value() * value); } + HashMember &operator*=(const void *value) = delete; HashMember &operator*=(char value) { return operator=(this->value() * value); } HashMember &operator*=(const std::string &value) { return operator=(this->value() * value); } HashMember &operator*=(const char *value) { return operator=(this->value() * value); } @@ -236,6 +239,7 @@ class PHPCPP_EXPORT HashMember : private HashParent HashMember &operator/=(int32_t value) { return operator=(this->value() / value); } HashMember &operator/=(int64_t value) { return operator=(this->value() / value); } HashMember &operator/=(bool value) { return operator=(this->value() / value); } + HashMember &operator/=(const void *value) = delete; HashMember &operator/=(char value) { return operator=(this->value() / value); } HashMember &operator/=(const std::string &value) { return operator=(this->value() / value); } HashMember &operator/=(const char *value) { return operator=(this->value() / value); } @@ -251,6 +255,7 @@ class PHPCPP_EXPORT HashMember : private HashParent HashMember &operator%=(int32_t value) { return operator=(this->value() % value); } HashMember &operator%=(int64_t value) { return operator=(this->value() % value); } HashMember &operator%=(bool value) { return operator=(this->value() % value); } + HashMember &operator%=(const void *value) = delete; HashMember &operator%=(char value) { return operator=(this->value() % value); } HashMember &operator%=(const std::string &value) { return operator=(this->value() % value); } HashMember &operator%=(const char *value) { return operator=(this->value() % value); } @@ -266,6 +271,7 @@ class PHPCPP_EXPORT HashMember : private HashParent Value operator+(int32_t value) { return this->value() + value; } Value operator+(int64_t value) { return this->value() + value; } Value operator+(bool value) { return this->value() + value; } + Value operator+(const void *value) = delete; Value operator+(char value) { return this->value() + value; } Value operator+(const std::string &value) { return this->value() + value; } Value operator+(const char *value) { return this->value() + value; } @@ -281,6 +287,7 @@ class PHPCPP_EXPORT HashMember : private HashParent Value operator-(int32_t value) { return this->value() - value; } Value operator-(int64_t value) { return this->value() - value; } Value operator-(bool value) { return this->value() - value; } + Value operator-(const void *value) = delete; Value operator-(char value) { return this->value() - value; } Value operator-(const std::string &value) { return this->value() - value; } Value operator-(const char *value) { return this->value() - value; } @@ -296,6 +303,7 @@ class PHPCPP_EXPORT HashMember : private HashParent Value operator*(int32_t value) { return this->value() * value; } Value operator*(int64_t value) { return this->value() * value; } Value operator*(bool value) { return this->value() * value; } + Value operator*(const void *value) = delete; Value operator*(char value) { return this->value() * value; } Value operator*(const std::string &value) { return this->value() * value; } Value operator*(const char *value) { return this->value() * value; } @@ -311,6 +319,7 @@ class PHPCPP_EXPORT HashMember : private HashParent Value operator/(int32_t value) { return this->value() / value; } Value operator/(int64_t value) { return this->value() / value; } Value operator/(bool value) { return this->value() / value; } + Value operator/(const void *value) = delete; Value operator/(char value) { return this->value() / value; } Value operator/(const std::string &value) { return this->value() / value; } Value operator/(const char *value) { return this->value() / value; } @@ -326,6 +335,7 @@ class PHPCPP_EXPORT HashMember : private HashParent Value operator%(int32_t value) { return this->value() % value; } Value operator%(int64_t value) { return this->value() % value; } Value operator%(bool value) { return this->value() % value; } + Value operator%(const void *value) = delete; Value operator%(char value) { return this->value() % value; } Value operator%(const std::string &value) { return this->value() % value; } Value operator%(const char *value) { return this->value() % value; } diff --git a/include/ini.h b/include/ini.h index a1899a4c..03e6d264 100644 --- a/include/ini.h +++ b/include/ini.h @@ -61,6 +61,7 @@ class PHPCPP_EXPORT Ini */ Ini(const char *name, bool value, const Place place = Place::All) : _name(name), _value(bool2str(value)), _place(place) {} + Ini(const char *name, const void *value, const Place place = Place::All) = delete; /** * Constructors for integer values diff --git a/include/value.h b/include/value.h index 45f7464e..084102c1 100644 --- a/include/value.h +++ b/include/value.h @@ -56,6 +56,7 @@ class PHPCPP_EXPORT Value : private HashParent Value(int32_t value); Value(int64_t value); Value(bool value); + Value(const void *value) = delete; Value(char value); Value(const std::string &value); Value(const char *value, int size = -1); @@ -174,6 +175,7 @@ class PHPCPP_EXPORT Value : private HashParent Value &operator=(int32_t value); Value &operator=(int64_t value); Value &operator=(bool value); + Value &operator=(const void *value) = delete; Value &operator=(char value); Value &operator=(const std::string &value); Value &operator=(const char *value); @@ -191,6 +193,7 @@ class PHPCPP_EXPORT Value : private HashParent Value &operator+=(int32_t value); Value &operator+=(int64_t value); Value &operator+=(bool value); + Value &operator+=(const void *value) = delete; Value &operator+=(char value); Value &operator+=(const std::string &value); Value &operator+=(const char *value); @@ -206,6 +209,7 @@ class PHPCPP_EXPORT Value : private HashParent Value &operator-=(int32_t value); Value &operator-=(int64_t value); Value &operator-=(bool value); + Value &operator-=(const void *value) = delete; Value &operator-=(char value); Value &operator-=(const std::string &value); Value &operator-=(const char *value); @@ -221,6 +225,7 @@ class PHPCPP_EXPORT Value : private HashParent Value &operator*=(int32_t value); Value &operator*=(int64_t value); Value &operator*=(bool value); + Value &operator*=(const void *value) = delete; Value &operator*=(char value); Value &operator*=(const std::string &value); Value &operator*=(const char *value); @@ -236,6 +241,7 @@ class PHPCPP_EXPORT Value : private HashParent Value &operator/=(int32_t value); Value &operator/=(int64_t value); Value &operator/=(bool value); + Value &operator/=(const void *value) = delete; Value &operator/=(char value); Value &operator/=(const std::string &value); Value &operator/=(const char *value); @@ -251,6 +257,7 @@ class PHPCPP_EXPORT Value : private HashParent Value &operator%=(int32_t value); Value &operator%=(int64_t value); Value &operator%=(bool value); + Value &operator%=(const void *value) = delete; Value &operator%=(char value); Value &operator%=(const std::string &value); Value &operator%=(const char *value); @@ -266,6 +273,7 @@ class PHPCPP_EXPORT Value : private HashParent Value operator+(int32_t value); Value operator+(int64_t value); Value operator+(bool value); + Value operator+(const void *value) = delete; Value operator+(char value); Value operator+(const std::string &value); Value operator+(const char *value); @@ -281,6 +289,7 @@ class PHPCPP_EXPORT Value : private HashParent Value operator-(int32_t value); Value operator-(int64_t value); Value operator-(bool value); + Value operator-(const void *value) = delete; Value operator-(char value); Value operator-(const std::string &value); Value operator-(const char *value); @@ -296,6 +305,7 @@ class PHPCPP_EXPORT Value : private HashParent Value operator*(int32_t value); Value operator*(int64_t value); Value operator*(bool value); + Value operator*(const void *value) = delete; Value operator*(char value); Value operator*(const std::string &value); Value operator*(const char *value); @@ -311,6 +321,7 @@ class PHPCPP_EXPORT Value : private HashParent Value operator/(int32_t value); Value operator/(int64_t value); Value operator/(bool value); + Value operator/(const void *value) = delete; Value operator/(char value); Value operator/(const std::string &value); Value operator/(const char *value); @@ -326,6 +337,7 @@ class PHPCPP_EXPORT Value : private HashParent Value operator%(int32_t value); Value operator%(int64_t value); Value operator%(bool value); + Value operator%(const void *value) = delete; Value operator%(char value); Value operator%(const std::string &value); Value operator%(const char *value); @@ -333,6 +345,10 @@ class PHPCPP_EXPORT Value : private HashParent /** * Comparison operators for hardcoded strings + * + * Note that this works only for string values, + * other values segfaults! + * * @param value */ bool operator==(const char *value) const { return ::strcmp(rawValue(), value) == 0; } From 9950a0b1c53ba167d09081307170194999daf9c8 Mon Sep 17 00:00:00 2001 From: Tomas Ebenlendr Date: Tue, 16 Apr 2024 09:29:46 +0200 Subject: [PATCH 2/2] Php::Value constructor from _zend_string Previous commit exposes mishandling of _zend_string function arguments by PHP-CPP to compiler. This commit fixes that problem. --- include/value.h | 1 + zend/value.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/value.h b/include/value.h index 084102c1..996eb48f 100644 --- a/include/value.h +++ b/include/value.h @@ -60,6 +60,7 @@ class PHPCPP_EXPORT Value : private HashParent Value(char value); Value(const std::string &value); Value(const char *value, int size = -1); + Value(struct _zend_string *value); Value(double value); Value(const IniValue &value); diff --git a/zend/value.cpp b/zend/value.cpp index 6f53af42..aa05efcb 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -128,6 +128,25 @@ Value::Value(const char *value, int size) } } +/** + * Constructor based on zend_string + * @param value + */ +Value::Value(struct _zend_string *value) +{ + // is there a value? + if (value) + { + // create a string zval + ZVAL_STRINGL(_val, value->val, value->len); + } + else + { + // store null + ZVAL_NULL(_val); + } +} + /** * Constructor based on decimal value * @param value