Commit graph

1308 commits

Author SHA1 Message Date
Tim Düsterhus
5c693c770a
Remove ->last_unsafe from php_random_status (#9132)
Whenever ->last_unsafe is set to `true` an exception has been thrown. Thus we
can replace the check for `->last_unsafe` with a check for `EG(exception)`
which is a much more natural way to ommunicate an error up the chain.
2022-07-26 09:02:51 +02:00
Go Kudo
4d8dd8d258
Implement Random Extension
https://wiki.php.net/rfc/rng_extension
https://wiki.php.net/rfc/random_extension_improvement
2022-07-19 10:27:38 +01:00
Michael Voříšek
e80925445c Fix GH-8924 str_split of empty string must return empty array
Closes #8945.
2022-07-08 15:49:45 +01:00
Arnaud Le Blanc
4df3dd7679
Reduce memory allocated by var_export, json_encode, serialize, and other (#8902)
smart_str uses an over-allocated string to optimize for append operations. Functions that use smart_str tend to return the over-allocated string directly. This results in unnecessary memory usage, especially for small strings.

The overhead can be up to 231 bytes for strings smaller than that, and 4095 for other strings. This can be avoided for strings smaller than `4096 - zend_string header size - 1` by reallocating the string.

This change introduces `smart_str_trim_to_size()`, and calls it in `smart_str_extract()`. Functions that use `smart_str` are updated to use `smart_str_extract()`.

Fixes GH-8896
2022-07-08 14:47:46 +02:00
George Peter Banyard
4ccf0b0181
Make php_fgetcsv() return a HashTale instead of in-out zval param (#8936)
Also refactor what happens on an empty line to return NULL instead of setting the array to [NULL] which makes no design sense at all.
However, as this is the current behaviour create a BC Shim inline function to recreate this weird HashTable in the functions which currently use this API
2022-07-08 12:11:05 +01:00
George Peter Banyard
ef287bfceb
Minor refactoring of std string extension (#8196)
Mainly using more appropriate types, early returns, and moving the happy path to the primary scope (failure path is guarded by ``UNEXPECTED`` macros.
2022-04-23 12:15:13 +01:00
George Peter Banyard
5171cb435a Fix [-Wundef] warnings in standard extension 2022-04-01 15:48:41 +01:00
Max Kellermann
b9e895bca0
Replace memcmp() with zend_string functions (#8216)
* ext/oci8: use zend_string_equals()

Eliminate duplicate code.

* main/php_variables: use zend_string_equals_literal()

Eliminate duplicate code.

* Zend/zend_string: add zend_string_equals_cstr()

Allows eliminating duplicate code.

* Zend, ext/{opcache,standard}, main/output: use zend_string_equals_cstr()

Eliminate duplicate code.

* Zend/zend_string: add zend_string_starts_with()

* ext/{opcache,phar,spl,standard}: use zend_string_starts_with()

This adds missing length checks to several callers, e.g. in
cache_script_in_shared_memory().  This is important when the
zend_string is shorter than the string parameter, when memcmp()
happens to check backwards; this can result in an out-of-bounds memory
access.
2022-03-31 16:27:58 +02:00
George Peter Banyard
e948d3c9c8
Use zend_string_to(upper|lower)() API directly 2022-03-23 23:59:41 +00:00
George Peter Banyard
71a110fcaa
Remove strnatcmp_ex() wrappers
These APIs always returned SUCCESS.

Closes GH-8195
2022-03-23 23:53:12 +00:00
George Peter Banyard
dd62ec065e
Refactor php_next_utf8_char() to use zend_result 2022-03-13 13:48:21 +00:00
Ilija Tovilo
2f5295692f
Optimize stripos/stristr
Closes GH-7847
Closes GH-7852

Previously stripos/stristr would lowercase both the haystack and the
needle to reuse strpos. The approach in this PR is similar to strpos.
memchr is highly optimized so we're using it to search for the first
character of the needle in the haystack. If we find it we compare the
remaining characters of the needle manually.

The new implementation seems to perform about half as well as strpos (as
two memchr calls are necessary to find the next candidate).
2022-01-31 21:44:31 +01:00
Tim Starling
8eee0d6130
Make strtolower() and strtoupper() do ASCII case conversion (#7506)
Implement RFC https://wiki.php.net/rfc/strtolower-ascii
2021-12-15 08:38:35 -05:00
Dmitry Stogov
085b360abb Improve strtr(), str_replace() and substr_count() performance
Use SSE2 to calculate number of occurrences of a given character in a string
2021-11-09 22:26:37 +03:00
Dmitry Stogov
6e3f3cbaee Improve strtr() performance using SSE2 instructions 2021-11-09 17:32:27 +03:00
Dmitry Stogov
90b7bde615 Use more compact representation for packed arrays.
- for packed arrays we store just an array of zvals without keys.
- the elements of packed array are accessible throuf as ht->arPacked[i]
  instead of ht->arData[i]
- in addition to general ZEND_HASH_FOREACH_* macros, we introduced similar
  familied for packed (ZEND_HASH_PACKED_FORECH_*) and real hashes
  (ZEND_HASH_MAP_FOREACH_*)
- introduced an additional family of macros to access elements of array
  (packed or real hashes) ZEND_ARRAY_ELEMET_SIZE, ZEND_ARRAY_ELEMET_EX,
  ZEND_ARRAY_ELEMET, ZEND_ARRAY_NEXT_ELEMENT, ZEND_ARRAY_PREV_ELEMENT
- zend_hash_minmax() prototype was changed to compare only values

Because of smaller data set, this patch may show performance improvement
on some apps and benchmarks that use packed arrays. (~1% on PHP-Parser)

TODO:
    - sapi/phpdbg needs special support for packed arrays (WATCH_ON_BUCKET).
    - zend_hash_sort_ex() may require converting packed arrays to hash.
2021-11-03 15:18:26 +03:00
Kamil Tekiela
c3dda473cc
Fix 'can not' in test data and in code comments 2021-10-05 09:51:58 +01:00
Tim Starling
da0c70508e
Add upper case functions to zend_operators.c and use them (#7521)
Add a family of upper case conversion functions to zend_operators.c,
by analogy with the lower case functions.

Move the single-character conversion macros to the header so that they
can be used as a locale-independent replacement for tolower() and
toupper().

Factor out the ugly bits of the SSE2 case conversion so that the four
functions that use it are easy to read and processor-independent.

Use the new ASCII upper case functions in ext/xml, ext/pdo_dblib and as
an optimization for strtoupper() when the locale is "C".
2021-09-29 09:37:40 +02:00
Nikita Popov
1441271f18 Don't use else after return in php_string_tolower() 2021-09-28 09:43:13 +02:00
Nikita Popov
6d505d4445 Add RETURN/RETVAL_COPY_DEREF() macros
These were missing from the set...

I think quite a few of these usages don't actually need the DEREF,
but I've just kept things as is for now.
2021-07-22 09:44:19 +02:00
Christoph M. Becker
6dc20e1a24
Merge branch 'PHP-8.0'
* PHP-8.0:
  Fix #72146: Integer overflow on substr_replace
2021-07-15 12:57:24 +02:00
Christoph M. Becker
c0a1ef3e32
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4:
  Fix #72146: Integer overflow on substr_replace
2021-07-15 12:56:12 +02:00
Christoph M. Becker
33f8dfb15a
Fix #72146: Integer overflow on substr_replace
Adding two `zend_long`s may overflow, and casting `size_t` to
`zend_long` may truncate; we can avoid this here by enforcing unsigned
arithmetic.

Closes GH-7240.
2021-07-15 12:54:28 +02:00
Patrick Allaert
aff365871a Fixed some spaces used instead of tabs 2021-06-29 11:30:26 +02:00
George Peter Banyard
db1bde73cf
Use equals OR equal instead of >= && <=
For only two values this is clearer
2021-05-18 15:08:12 +01:00
George Peter Banyard
aca6aefd85
Remove 'register' type qualifier (#6980)
The compiler should be smart enough to optimize this on its own
2021-05-14 13:38:01 +01:00
George Peter Banyard
c40231afbf
Mark various functions with void arguments.
This fixes a bunch of [-Wstrict-prototypes] warning,
because in C func() and func(void) have different semantics.
2021-05-12 14:55:53 +01:00
KsaR
01b3fc03c3
Update http->https in license (#6945)
1. Update: http://www.php.net/license/3_01.txt to https, as there is anyway server header "Location:" to https.
2. Update few license 3.0 to 3.01 as 3.0 states "php 5.1.1, 4.1.1, and earlier".
3. In some license comments is "at through the world-wide-web" while most is without "at", so deleted.
4. fixed indentation in some files before |
2021-05-06 12:16:35 +02:00
twosee
0236bbc777
Merge branch 'PHP-8.0'
* PHP-8.0:
  Fixed bug #80958
2021-04-16 16:19:43 +08:00
twosee
ecc4bf14f0
Fixed bug #80958
Missing check after zval_try_get_string().

Closes GH-6871.
2021-04-16 16:17:37 +08:00
George Peter Banyard
5caaf40b43
Introduce pseudo-keyword ZEND_FALLTHROUGH
And use it instead of comments
2021-04-07 00:46:29 +01:00
Dmitry Stogov
a13a1be734 Use ZEND_HASH_FILL_* API for explode() 2021-04-06 16:57:02 +03:00
Dmitry Stogov
550a662f67 strtr() optimization 2021-03-22 17:37:11 +03:00
George Peter Banyard
a6fc427b8c Use zend_string_equals() API instead of strcmp() in various places
Closes GH-6784
2021-03-17 16:36:23 +00:00
Dmitry Stogov
bb1d61a848 Attempt to fix ext\standard\tests\file\basename_bug66395_variation2-win32.phpt and ext\standard\tests\file\pathinfo_basic1-win32.phpt 2021-02-19 21:48:19 +03:00
Dmitry Stogov
5e01542526 Improve basename(). Avoid calling mblen() for ASCII compatible locales. 2021-02-19 15:42:21 +03:00
Nikita Popov
4fdaf84cc3 Merge branch 'PHP-8.0'
* PHP-8.0:
  Avoid signed integer overflow in substr()
2021-02-18 10:35:17 +01:00
Nikita Popov
85ffe8dcdc Avoid signed integer overflow in substr()
Perform negation after the (size_t) cast rather than before,
so as to avoid a signed integer overflow for PHP_INT_MIN.

Fixes oss-fuzz #31069.
2021-02-18 10:34:54 +01:00
Nikita Popov
3e01f5afb1 Replace zend_bool uses with bool
We're starting to see a mix between uses of zend_bool and bool.
Replace all usages with the standard bool type everywhere.

Of course, zend_bool is retained as an alias.
2021-01-15 12:33:06 +01:00
Nikita Popov
26fc4bea44 Remove some INDIRECT handling in standard library 2021-01-06 12:46:31 +01:00
Nikita Popov
2772f7c3ad Avoid direct calls to zend_cpu_supports()
While the use of zend_cpu_supports_*() is only strictly necessary
inside ifunc resolvers, where the cpu state has not been initialized
yet, we should prefer the compiler builtins in all cases.
2020-11-27 11:18:10 +01:00
Nikita Popov
d776d25a8e Don't throw for out of bounds offsets in strspn()
Make strspn($str1, $str2, $offset, $length) behaviorally
equivalent to strspn(substr($str1, $offset, $length), $str2)
by not throwing for out of bounds offset.

There have been two reports that this change cause issues,
including bug #80285.
2020-10-27 11:42:01 +01:00
Nikita Popov
13b791c79c Normalize substr() behavior
Make the behavior of substr(), mb_substr(), iconv_substr() and
grapheme_substr() consistent when it comes to the handling of
out of bounds offsets. substr() will now always clamp out of
bounds offsets to the string boundary. Cases that previously
returned false will now return an empty string. This means that
substr() itself *always* returns a string now (like mb_substr()
already did before.)

Closes GH-6182.
2020-09-25 09:58:21 +02:00
Nikita Popov
5d9ab53a5d Check string bounds in strspn/strcspn
strspn/strcspn are string search functions, and as such should throw
ValueError on out-of-bounds offsets, just like strpos etc do.
2020-09-22 10:46:50 +02:00
Nikita Popov
12e772f18d Promote substr_replace warnings
The implementation here was pretty confused. In reality the only
error condition it has right now is that for a string input,
from & length cannot be arrays.

The fact that the array lengths are the same was probably supposed
to be checked for the case of array input, as it wouldn't matter
otherwise.
2020-09-22 10:22:43 +02:00
Máté Kocsis
c37a1cd650
Promote a few remaining errors in ext/standard
Closes GH-6110
2020-09-15 14:26:16 +02:00
Máté Kocsis
46c0c82a0f
Declare array|int and object-of-class|int types in stubs
Closes GH-6081

Co-Authored-By: Nikita Popov <nikic@php.net>
2020-09-14 11:59:32 +02:00
Máté Kocsis
c98d47696f
Consolidate new union type ZPP macro names
They will now follow the canonical order of types. Older macros are
left intact due to maintaining BC.

Closes GH-6112
2020-09-11 11:00:18 +02:00
Dmitry Stogov
4a2ae84188 Add "const". Move constant strings to read-only memory. 2020-09-07 21:35:48 +03:00
Máté Kocsis
2c96780e1c
Fix UNKNOWN default values in ext/standard
Closes GH-6026
2020-09-07 18:58:11 +02:00