From 0e5f500a05db7d06a48996cdedc295af708f7995 Mon Sep 17 00:00:00 2001 From: Bernard Normier Date: Tue, 29 Oct 2024 20:00:07 -0400 Subject: [PATCH] Fix PHP --- php/src/Operation.cpp | 2 +- php/src/Proxy.cpp | 6 ++--- php/src/Util.cpp | 61 ++++++++++++++++++++++++++++++++++++++++--- php/src/Util.h | 3 +++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/php/src/Operation.cpp b/php/src/Operation.cpp index bd5d7d5b9cd..f04f7882e16 100644 --- a/php/src/Operation.cpp +++ b/php/src/Operation.cpp @@ -628,7 +628,7 @@ IcePHP::SyncTypedInvocation::invoke(INTERNAL_FUNCTION_PARAMETERS) Ice::Context ctx; if (ZEND_NUM_ARGS() == static_cast(_op->numParams) + 1) { - if (!extractStringMap(&args[ZEND_NUM_ARGS() - 1], ctx)) + if (!extractContext(&args[ZEND_NUM_ARGS() - 1], ctx)) { return; } diff --git a/php/src/Proxy.cpp b/php/src/Proxy.cpp index f1381101b70..7d451cde855 100644 --- a/php/src/Proxy.cpp +++ b/php/src/Proxy.cpp @@ -174,7 +174,7 @@ ZEND_METHOD(Ice_ObjectPrx, ice_getContext) ProxyPtr _this = Wrapper::value(getThis()); assert(_this); - if (!createStringMap(return_value, _this->proxy->ice_getContext())) + if (!createContext(return_value, _this->proxy->ice_getContext())) { RETURN_NULL(); } @@ -195,7 +195,7 @@ ZEND_METHOD(Ice_ObjectPrx, ice_context) // Populate the context. Ice::Context ctx; - if (arr && !extractStringMap(arr, ctx)) + if (arr && !extractContext(arr, ctx)) { RETURN_NULL(); } @@ -1474,7 +1474,7 @@ do_cast(INTERNAL_FUNCTION_PARAMETERS, bool check) // Populate the context. Ice::Context ctx; - if (arr && !extractStringMap(arr, ctx)) + if (arr && !extractContext(arr, ctx)) { RETURN_NULL(); } diff --git a/php/src/Util.cpp b/php/src/Util.cpp index 2faf7448b95..25aafff6a5f 100644 --- a/php/src/Util.cpp +++ b/php/src/Util.cpp @@ -293,7 +293,7 @@ IcePHP::createStringMap(zval* zv, const map& ctx) { array_init(zv); - for (map::const_iterator p = ctx.begin(); p != ctx.end(); ++p) + for (auto p = ctx.begin(); p != ctx.end(); ++p) { add_assoc_stringl_ex( zv, @@ -318,7 +318,63 @@ IcePHP::extractStringMap(zval* zv, map& ctx) } HashTable* arr = Z_ARRVAL_P(zv); - zend_ulong num_key; + [[maybe_unused]] zend_ulong num_key; + zend_string* key; + zval* val; + ZEND_HASH_FOREACH_KEY_VAL(arr, num_key, key, val) + { + if (!key) + { + invalidArgument("array key must be a string"); + return false; + } + + if (Z_TYPE_P(val) != IS_STRING) + { + invalidArgument("array value must be a string"); + return false; + } + + ctx[key->val] = Z_STRVAL_P(val); + } + ZEND_HASH_FOREACH_END(); + + return true; +} + +// TODO: avoid duplication with code above. + +bool +IcePHP::createContext(zval* zv, const Ice::Context& ctx) +{ + array_init(zv); + + for (auto p = ctx.begin(); p != ctx.end(); ++p) + { + add_assoc_stringl_ex( + zv, + const_cast(p->first.c_str()), + static_cast(p->first.length()), + const_cast(p->second.c_str()), + static_cast(p->second.length())); + } + + return true; +} + +bool +IcePHP::extractContext(zval* zv, Ice::Context& ctx) +{ + if (Z_TYPE_P(zv) != IS_ARRAY) + { + ostringstream os; + os << "expected an associative array but received " << zendTypeToString(Z_TYPE_P(zv)); + invalidArgument(os.str()); + return false; + } + + HashTable* arr = Z_ARRVAL_P(zv); + [[maybe_unused]] zend_ulong num_key; zend_string* key; zval* val; ZEND_HASH_FOREACH_KEY_VAL(arr, num_key, key, val) @@ -336,7 +392,6 @@ IcePHP::extractStringMap(zval* zv, map& ctx) } ctx[key->val] = Z_STRVAL_P(val); - (void)num_key; // Avoids error from older versions of GCC about unused variable num_key. } ZEND_HASH_FOREACH_END(); diff --git a/php/src/Util.h b/php/src/Util.h index ba9b7772f38..0adc254567c 100644 --- a/php/src/Util.h +++ b/php/src/Util.h @@ -76,6 +76,9 @@ namespace IcePHP bool createStringMap(zval*, const std::map&); bool extractStringMap(zval*, std::map&); + bool createContext(zval*, const Ice::Context&); + bool extractContext(zval*, Ice::Context&); + bool createStringArray(zval*, const Ice::StringSeq&); bool extractStringArray(zval*, Ice::StringSeq&);