Skip to content

Commit ee836f3

Browse files
committed
address first round of PR comments
- use hardcoded allow list for $share_options - use zval_try_get_long
1 parent 39ff28b commit ee836f3

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

ext/curl/share.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,39 @@ PHP_FUNCTION(curl_share_init_persistent)
160160
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(share_opts), entry) {
161161
ZVAL_DEREF(entry);
162162

163-
zend_ulong option = zval_get_long_ex(entry, true);
164-
165-
if (option == CURL_LOCK_DATA_COOKIE) {
166-
zend_throw_exception_ex(
167-
NULL,
168-
0,
169-
"CURL_LOCK_DATA_COOKIE is not allowed with persistent curl share handles"
170-
);
163+
bool failed = false;
164+
zend_ulong option = zval_try_get_long(entry, &failed);
171165

166+
if (failed) {
167+
zend_argument_type_error(1, "must contain only longs, %s given", zend_zval_value_name(entry));
172168
goto error;
173169
}
174170

175-
// Ensure that each additional option results in a unique persistent ID.
176-
persistent_id += 1 << option;
171+
switch (option) {
172+
// Disallowed options
173+
case CURL_LOCK_DATA_COOKIE:
174+
zend_argument_value_error(1, "CURL_LOCK_DATA_COOKIE is not allowed");
175+
goto error;
176+
177+
// Allowed options
178+
case CURL_LOCK_DATA_DNS:
179+
persistent_id |= 1 << 0;
180+
break;
181+
case CURL_LOCK_DATA_SSL_SESSION:
182+
persistent_id |= 1 << 1;
183+
break;
184+
case CURL_LOCK_DATA_CONNECT:
185+
persistent_id |= 1 << 2;
186+
break;
187+
case CURL_LOCK_DATA_PSL:
188+
persistent_id |= 1 << 3;
189+
break;
190+
191+
// Unknown options
192+
default:
193+
zend_argument_value_error(1, "must contain only CURL_LOCK_DATA_* constants");
194+
goto error;
195+
}
177196
} ZEND_HASH_FOREACH_END();
178197

179198
zend_array_sort(Z_ARRVAL_P(share_opts), php_array_data_compare_unstable_i, 1);

ext/curl/tests/curl_persistent_share_003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $sh = curl_share_init_persistent([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT, 30
99

1010
?>
1111
--EXPECTF--
12-
Fatal error: Uncaught Exception: Could not construct persistent cURL share handle: Unknown share option in %scurl_persistent_share_003.php:3
12+
Fatal error: Uncaught ValueError: curl_share_init_persistent(): Argument #1 ($share_options) must contain only CURL_LOCK_DATA_* constants in %scurl_persistent_share_003.php:3
1313
Stack trace:
1414
#0 %scurl_persistent_share_003.php(3): curl_share_init_persistent(Array)
1515
#1 {main}

ext/curl/tests/curl_persistent_share_004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $sh = curl_share_init_persistent([CURL_LOCK_DATA_COOKIE]);
99

1010
?>
1111
--EXPECTF--
12-
Fatal error: Uncaught Exception: CURL_LOCK_DATA_COOKIE is not allowed with persistent curl share handles in %scurl_persistent_share_004.php:3
12+
Fatal error: Uncaught ValueError: curl_share_init_persistent(): Argument #1 ($share_options) CURL_LOCK_DATA_COOKIE is not allowed in %scurl_persistent_share_004.php:3
1313
Stack trace:
1414
#0 %scurl_persistent_share_004.php(3): curl_share_init_persistent(Array)
1515
#1 {main}

0 commit comments

Comments
 (0)