Commit graph

1189 commits

Author SHA1 Message Date
Gina Peter Banyard
25a5146180
Clean-up unused headers (#14365)
* ext/mbstring.c: clean-up headers and include intrinsics
2024-06-01 17:12:42 +01:00
Joshua Rüsweg
e4a8d5b16f
RFC: array_find (#14108)
see https://wiki.php.net/rfc/array_find
2024-05-31 19:39:12 +02:00
Levi Morrison
c461b60060
refactor: change zend_is_true to return bool (#14301)
Previously this returned `int`. Many functions actually take advantage
of the fact this returns exactly 0 or 1. For instance,
`main/streams/xp_socket.c` does:

    sockopts |= STREAM_SOCKOP_IPV6_V6ONLY_ENABLED * zend_is_true(tmpzval);

And `Zend/zend_compile.c` does:

    child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];

I changed a few places trivially from `int` to `bool`, but there are
still many places such as the object handlers which return `int` that
should eventually be `bool`.
2024-05-24 15:16:36 -06:00
Peter Kokot
04c417a35e
Remove unused always-enabled extension headers (#14042)
These were once used in these files but at this point aren't and are
only causing confusion whether file depends on additional extension.

- locale.h is added in main/SAPI.c for _ENABLE_PER_THREAD_LOCALE
2024-05-04 21:06:29 +02:00
Tim Düsterhus
dce6ed3199
random: Adjust status to state (#13521)
* random: Rename `status` local to `state`

* random: Rename `php_random_algo_with_state`'s `status` member to `state`
2024-02-26 20:38:45 +01:00
Tim Düsterhus
79133df156
random: Pass algorithm and state together as php_random_algo_with_state (#13350)
* random: Remove `php_random_status`

Since 162e1dce98, the `php_random_status` struct
contains just a single `void*`, resulting in needless indirection when
accessing the engine state and thus decreasing readability because of the
additional non-meaningful `->state` references / the local helper variables.

There is also a small, but measurable performance benefit:

    <?php
    $e = new Random\Engine\Xoshiro256StarStar(0);
    $r = new Random\Randomizer($e);

    for ($i = 0; $i < 15; $i++)
    	var_dump(strlen($r->getBytes(100000000)));

goes from roughly 3.85s down to 3.60s.

The names of the `status` variables have not yet been touched to keep the diff
small. They will be renamed to the more appropriate `state` in a follow-up
cleanup commit.

* Introduce `php_random_algo_with_state`
2024-02-25 20:48:58 +01:00
Ilija Tovilo
631bc81607
Implement stackless internal function calls
Co-authored-by: Dmitry Stogov <dmitry@zend.com>

Closes GH-12461
2024-02-06 17:42:28 +01:00
David Carlier
45b99f6b2f Forgotten piece of GH-13309/GH-13310 previous PR 2024-02-03 13:24:15 +00:00
David Carlier
e32821258e Merge branch 'PHP-8.2' into PHP-8.3 2024-02-03 13:22:58 +00:00
David Carlier
b06d6dba4f Forgotten piece of GH-13309/GH-13310 previous PR 2024-02-03 13:22:45 +00:00
David Carlier
0e93f03e65 Merge branch 'PHP-8.3' 2024-02-03 13:08:32 +00:00
David Carlier
6842d3c03a Merge branch 'PHP-8.2' into PHP-8.3 2024-02-03 13:07:51 +00:00
David Carlier
d91224cd2f Fix GH-13309 and GH-13310: array hashes comparison, wrong buffer len calculation.
php_array_key_compare_string_case_unstable_i has a typo for the second
operand resulting in a wrong buffer size calculation.

Issue reported by @AlexRudyuk

Close GH-13315
2024-02-03 13:07:15 +00:00
Ilija Tovilo
a135517376
Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix instable array during in-place modification in uksort
2024-01-31 19:25:59 +01:00
Ilija Tovilo
d65c395049
Fix instable array during in-place modification in uksort
The array isn't just observable if the array has RCn, but also if it is inside a
reference that is RCn. By-ref parameters are always RCn and as such always
observable.

Fixes GH-13279
Closes GH-13285
2024-01-31 19:25:30 +01:00
Ilija Tovilo
d653646841
Fix iterator position resetting
Previously, when an array was converted from packed to hashed, iterators would
not be correctly reset to 0. Similarly, removing the last element from an array
would decrease nNumUsed but not actually fix the iterator position, causing the
element to be skipped in the next iteration.

Some code was also removed that skips over IS_UNDEF elements for
nInternalPointer and iterator positions. This is unnecessary, as this already
happens during iteration.

Closes GH-13178
Closes GH-13188
2024-01-21 23:24:00 +01:00
Niels Dossche
fe064d7f12 Fix GH-13142: Undefined variable name is shortened when contains \0
Uses the new %S formatter and introduces the necessary changes and
helpers.
2024-01-20 23:49:13 +01:00
Tim Düsterhus
00ea756c93
random/standard: Adjust #13138 for PHP 8.3 2024-01-14 13:05:44 +01:00
Tim Düsterhus
f2f070a897
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  random/standard: Correctly handle broken engines in php_array_pick_keys (#13138)
2024-01-14 13:03:33 +01:00
Tim Düsterhus
97c6da1dec
random/standard: Correctly handle broken engines in php_array_pick_keys (#13138) 2024-01-14 13:01:29 +01:00
Niels Dossche
1d6f344bea Fix GH-13094: range(9.9, '0') causes segmentation fault
`start_type + end_type < 2*IS_STRING` is not right, in this test case
the types are start_type==5 (IS_DOUBLE), end_type==7 (IS_ARRAY).
The IS_ARRAY type is a sentinel to disambiguate single-byte strings.
The path must be taken when one of the types is not a string nor a
single-byte string. Therefore, use < IS_STRING with an OR condition.

Closes GH-13105.
2024-01-09 22:11:45 +01:00
Ilija Tovilo
2053af6628 Fix uouv in array_column
column_long and index_long might not be set, but are still used as arguments.
They are not actually used if column_str is set, but it's better to initialize
them anyway, if only to make MemorySanitizer happy.
2023-07-31 15:18:13 +02:00
George Peter Banyard
798c40a739
[RFC] Define proper semantics for range() function (#10826)
RFC: https://wiki.php.net/rfc/proper-range-semantics
2023-06-19 14:25:26 +01:00
George Peter Banyard
d5ad75108e
More usage of known zend_str instead of C string (#11381) 2023-06-08 13:03:29 +01:00
George Peter Banyard
a02f7f24c6
Use more appropriate types for php_array_walk() function 2023-06-06 12:12:07 +01:00
George Peter Banyard
15402454a6
ext/standard/array.c: Optimize min/max functions for int/float (#11194)
Co-authored-by: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
2023-06-02 10:27:46 +01:00
Ilija Tovilo
eb7ec15a61
Use single allocation for indirect values in array_multisort
Closes GH-11309
2023-05-24 20:49:53 +02:00
Ilija Tovilo
a59c933ac0
Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix exception handling in array_multisort()
2023-05-24 13:56:28 +02:00
Ilija Tovilo
0ee43718c0
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix exception handling in array_multisort()
2023-05-24 13:56:21 +02:00
Ilija Tovilo
b2ec6c24f8
Fix exception handling in array_multisort()
Closes GH-11302
2023-05-24 13:55:25 +02:00
Ilija Tovilo
ded8b7f5bd
Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix access on NULL pointer in array_merge_recursive()
2023-05-24 13:37:59 +02:00
Ilija Tovilo
4676d9bc43
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix access on NULL pointer in array_merge_recursive()
2023-05-24 13:37:47 +02:00
Ilija Tovilo
f5c54fd88b
Fix access on NULL pointer in array_merge_recursive()
Closes GH-11303
2023-05-24 13:36:52 +02:00
George Peter Banyard
646f54b594
ext/standard/array.c: use uint32_t instead of incorrect int type
Drive-by indentation fixes and bool usage
2023-05-07 15:01:37 +01:00
Niels Dossche
947eb95241
Allow array functions to operate in-place if the refcount is 1 (#11060)
This allows array_merge, array_intersect, array_replace, array_unique
and usort to avoid taking a copy and do the transformation in-place.

** Safety **

There are some array functions which take a copy of the input
array into a temporary C array for sorting purposes.
(e.g. array_unique, array_diff, and array_intersect do this).
Since we no longer take a copy in all cases, we must check if
it's possible that a value is accessed that was already destroyed.

For array_unique: cmpdata will never be removed so that will never reach
refcount 0. And when something is removed, it is the previous value of
cmpdata, not the one user later. So this seems okay.

For array_intersect: a previous pointer (ptr[0] - 1) is accessed.
But this can't be a destroyed value because the pointer is first moved forward.

For array_diff: it's possible a previous pointer is accessed after
destruction. So we can't optimise this case easily.
2023-04-24 23:18:05 +02:00
Ilija Tovilo
4dad419ae6
Merge branch 'PHP-8.2'
* PHP-8.2:
  Support enums in array_unique
2023-04-16 15:36:02 +02:00
Ilija Tovilo
35c1bb2e47
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Support enums in array_unique
2023-04-16 15:35:55 +02:00
Ilija Tovilo
e8b8341d3d
Support enums in array_unique
Fixes GH-9775
Closes GH-11015
2023-04-16 15:34:56 +02:00
Niels Dossche
a0476fd32f
Micro-optimize double comparison (#11061)
When using ZEND_NORMALIZE_BOOL(a - b) where a and b are doubles, this
generates the following instruction sequence on x64:
subsd   xmm0, xmm1
pxor    xmm1, xmm1
comisd  xmm0, xmm1
...

whereas if we use ZEND_THREEWAY_COMPARE we get two instructions less:
ucomisd xmm0, xmm1

The only difference is that the threeway compare uses *u*comisd instead
of comisd. The difference is that it will cause a FP signal if a
signaling NAN is used, but as far as I'm aware this doesn't matter for
our use case.

Similarly, the amount of instructions on AArch64 is also quite a bit
lower for this code compared to the old code.

** Results **

Using the benchmark https://gist.github.com/nielsdos/b36517d81a1af74d96baa3576c2b70df
I used hyperfine: hyperfine --runs 25 --warmup 3 './sapi/cli/php sort_double.php'
No extensions such as opcache used during benchmarking.

BEFORE THIS PATCH
-----------------
  Time (mean ± σ):     255.5 ms ±   2.2 ms    [User: 251.0 ms, System: 2.5 ms]
  Range (min … max):   251.5 ms … 260.7 ms    25 runs

AFTER THIS PATCH
----------------
  Time (mean ± σ):     236.2 ms ±   2.8 ms    [User: 228.9 ms, System: 5.0 ms]
  Range (min … max):   231.5 ms … 242.7 ms    25 runs
2023-04-14 18:22:42 +02:00
George Peter Banyard
3b06618813
RFC: Saner array_(sum|product)() (#10161)
RFC: https://wiki.php.net/rfc/saner-array-sum-product

Moreover, the internal fast_add_function() function was removed.
2023-03-07 15:40:17 +00:00
Niels Dossche
3ff8333473
Cleanup dead code in array_slice (#10539)
We can only get to this if condition if at least preserve_keys is true.
Therefore, the else branch of this check can never execute.
2023-02-08 00:54:42 +01:00
Tyson Andre
ab87283118 Merge branch 'PHP-8.2' 2023-02-03 09:23:48 -05:00
Tyson Andre
e3f04ddb0b Merge branch 'PHP-8.1' into PHP-8.2 2023-02-03 09:18:03 -05:00
Tyson Andre
fe2dc2b481
Avoid crash for reset/end/next/prev() on ffi classes (#9711)
(And any PECLs returning `zend_empty_array` in the handler->get_properties
overrides)

Closes GH-9697

This is similar to the fix used in d9651a9419
for array_walk.

This should make it safer for php-src (and PECLs, long-term) to return
the empty immutable array in `handler->get_properties` to avoid wasting memory.
See https://github.com/php/php-src/issues/9697#issuecomment-1273613175

The only possible internal iterator position for the empty array is at the end
of the empty array (nInternalPointer=0).
The `zend_hash*del*` helpers will always set nInternalPointer to 0 when an
array becomes empty,
regardless of previous insertions/deletions/updates to the array.
2023-02-03 09:17:33 -05:00
Máté Kocsis
7936c8085e
Fix GH-8329 Print true/false instead of bool in error and debug messages (#8385) 2023-01-23 10:52:14 +01:00
Christoph M. Becker
c8955c078a
Revert GH-10220
Cf. <https://github.com/php/php-src/pull/10220#issuecomment-1383739816>.

This reverts commit ecc880f491.
This reverts commit 588a07f737.
This reverts commit f377e15751.
This reverts commit b4ba16fe18.
This reverts commit 694ec1deea.
This reverts commit 6b34de8eba.
This reverts commit aa1cd02a43.
This reverts commit 308fd311ea.
This reverts commit 16203b53e1.
This reverts commit 738fb5ca54.
This reverts commit 9fdbefacd3.
This reverts commit cd4a7c1d90.
This reverts commit 928685eba2.
This reverts commit 01e5ffc85c.
2023-01-16 12:27:33 +01:00
Niels
6ab503814d
Make array_pad's $length warning less confusing (#10149)
Remove array_pad's arbitrary length restriction

The error message was wrong; it *is* possible to use a larger length.
Furthermore, there is an arbitrary restriction on the new array's
length.

Fix both by checking the length against HT_MAX_SIZE.
2023-01-14 12:15:56 +01:00
Max Kellermann
308fd311ea ext/{standard,json,random,...}: add missing includes 2023-01-10 14:19:03 +00:00
Dmitry Stogov
626e909877 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix GH-9697: array_walk($ffiInstance, function () {}) crashes due to expecting mutable array
2022-10-10 11:25:33 +03:00
Dmitry Stogov
fa2c519544 Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0:
  Fix GH-9697: array_walk($ffiInstance, function () {}) crashes due to expecting mutable array
2022-10-10 11:25:23 +03:00