Commit Graph

357 Commits

Author SHA1 Message Date
Arnaud Le Blanc
efc8f0ebf8
Deprecate zend_atol() / add zend_ini_parse_quantity() (#7951)
Add zend_ini_parse_quantity() and deprecate zend_atol(), zend_atoi()

zend_atol() and zend_atoi() don't just do number parsing.
They also check for a 'K', 'M', or 'G' at the end of the string,
and multiply the parsed value out accordingly.

Unfortunately, they ignore any other non-numerics between the
numeric component and the last character in the string.
This means that numbers such as the following are both valid
and non-intuitive in their final output.

* "123KMG" is interpreted as "123G" -> 132070244352
* "123G " is interpreted as "123 " -> 123
* "123GB" is interpreted as "123B" -> 123
* "123 I like tacos." is also interpreted as "123." -> 123

Currently, in php-src these functions are used only for parsing ini values.

In this change we deprecate zend_atol(), zend_atoi(), and introduce a new
function with the same behavior, but with the ability to report invalid inputs
to the caller. The function's name also makes the behavior less unexpected:
zend_ini_parse_quantity().

Co-authored-by: Sara Golemon <pollita@php.net>
2022-06-17 14:12:53 +02:00
Tyson Andre
7b90ebeb3f
Fix -Werror=sign-compare warning in zend_memnistr (#8042)
`error: comparison of integer expressions of different signedness: ‘size_t’ {aka
‘long unsigned int’} and ‘long int’ [-Werror=sign-compare]`

This is asserting `end >= haystack` as a precondition for callers (in debug
builds) and existing callers are correct.

An alternate option is to cast the left side to `int64_t`, but that may be
slightly inefficient for 32-bit builds.
2022-02-05 14:18:37 -05: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
Nikita Popov
e32642c541 Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix bug #81598: Use C.UTF-8 as LC_CTYPE locale by default
2021-12-05 21:04:10 +01:00
Nikita Popov
26e424465c Fix bug #81598: Use C.UTF-8 as LC_CTYPE locale by default
Unfortunately, libedit is locale based and does not accept UTF-8
input when the C locale is used. This patch switches the default
locale to C.UTF-8 instead (if it is available). This makes libedit
work and I believe it shouldn't affect behavior of single-byte
locale-dependent functions that PHP otherwise uses.

Closes GH-7635.
2021-12-05 21:03:27 +01:00
Dmitry Stogov
067df26344 Use memrchr() when available
On x86_64 glibc memrchr() uses SSE/AVX CPU extensions and works much
faster then naive loop. On x86 32-bit we still use inlined version.

memrchr() is a GNU extension. Its prototype  becomes available when
<string.h> is included with defined _GNU_SOURCE macro. Previously, we
defined it in "php_config.h", but some sources may include <string.h>
befire it. To avod mess we also pass -D_GNU_SOURCE to C compiler.
2021-11-24 16:13:34 +03:00
Dmitry Stogov
c7484ef956 zend_memnstr() micro-optimization 2021-11-10 10:19:05 +03: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
498674058c Remove zend_binary_zval_strcasecmp() APIs
These are thin wrappers ... around the wrong functions. They call
the "_l()" version of the underlying APIs. For clarify, just call
the wrapped API directly.
2021-09-24 09:38:08 +02:00
Nikita Popov
7968ce93a8 Remove unnecessary const qualifier
To fix -Wignored-qualifiers warning.
2021-09-16 10:40:03 +02:00
Nikita Popov
604848188b Add additional double to string APIs
zend_double_to_str() converts a double to string in the way that
(string) would (using %.*H using precision).

smart_str_append_double() provides some more fine control over
the precision, and whether a zero fraction should be appeneded
for whole numbers.

A caveat here is that raw calls to zend_gcvt and going through
s*printf has slightly different behavior for the degenarate
precision=0 case. zend_gcvt will add a dummy E+0 in that case,
while s*printf convert this to precision=1 and will not. I'm
going with the s*printf behavior here, which is more common,
but does result in a minor change to the precision.phpt test.
2021-08-02 16:14:53 +02:00
Nikita Popov
a733b1ada7 Restore zend_atoi()
I dropped this in preparation for changes that I didn't end up
doing. Restore the function for now to avoid unnecessary churn for
extensions.
2021-07-16 14:46:56 +02:00
Nikita Popov
1cba7764b4
Remove zend_atoi() (#7232)
It's the same as (int) zend_atol() -- it doesn't try to do anything
integer size specific. Canonicalize to one function in preparation
for renaming zend_atol() to something less misleading.

FFI test is adjusted to use a zend_test function. It just calls
zend_atol() internally, but could really be anything.

Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
2021-07-13 09:22:31 +02:00
Nikita Popov
821a5a1239 Drop fast_div_function
This is the same as div_function nowadays.
2021-06-14 11:32:28 +02:00
George Peter Banyard
b6958bb847
Implement "Deprecate implicit non-integer-compatible float to int conversions" RFC. (#6661)
RFC: https://wiki.php.net/rfc/implicit-float-int-deprecate

Co-authored-by: Nikita Popov <nikita.ppv@gmail.com>
2021-05-31 15:48:45 +01:00
Nikita Popov
0c5711856f Add zend_ulong_to_str() API
No point in going through a smart_str and append_unsigned if we
can construct the result directly...
2021-04-13 15:56:24 +02:00
Nikita Popov
65a5c184d7 Add functions to convert i64/u64 to string
PDO implement half of this, but this functionality is generally
useful. Provide these as zend_u64_to_str and zend_i64_to_str to
complement zend_long_to_str.
2021-04-13 15:43:23 +02:00
Dmitry Stogov
5e01542526 Improve basename(). Avoid calling mblen() for ASCII compatible locales. 2021-02-19 15:42:21 +03: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
16cf1b915d compare_function() returns zend_result 2021-01-15 11:51:28 +01:00
Nikita Popov
058756b3bb Remove the convert_to_long_base function
This function is unused in php-src, and has somewhat dubious
semantics, especially since we switched convert_to_long to not
use strtol for the base 10 case.

If you want to convert strings from a different base, use
ZEND_STRTOL directly.
2021-01-15 10:43:26 +01:00
Nikita Popov
b429228420 Remove zend_locale_sprintf_double()
This function is unused, and also not particularly useful now that
PHP no longer prints doubles in a locale-sensitive way unless
someone really goes out of their way to force it.
2021-01-14 12:13:34 +01:00
Nikita Popov
422d1665a2 Make convert_to_*_ex simple aliases of convert_to_*
Historically, the _ex variants separated the zval first, if a
conversion was necessary. This distinction no longer makes sense
since PHP 7.

The only difference that was still left is that _ex checked whether
the type is the same first, but the usage of these macros did not
actually distinguish on whether such an inlined check is valuable
or not in a given context.

Also drop the unused convert_to_explicit_type macros.
2021-01-14 12:11:11 +01:00
George Peter Banyard
fa8d9b1183 Improve type declarations for Zend APIs
Voidification of Zend API which always succeeded
Use bool argument types instead of int for boolean arguments
Use bool return type for functions which return true/false (1/0)
Use zend_result return type for functions which return SUCCESS/FAILURE as they don't follow normal boolean semantics

Closes GH-6002
2020-08-28 15:41:27 +02:00
George Peter Banyard
b2248789ed Implement 'Saner Numeric Strings' RFC:
RFC: https://wiki.php.net/rfc/saner-numeric-strings

This removes the -1 allow_error mode from is_numeric_string functions and replaces it by
a trailing boolean out argument to preserve BC in a couple of places.

Most of the changes can be resumed to "numeric" strings which emitted a E_NOTICE now emit
a E_WARNING and "numeric" strings which emitted a E_WARNING now throw a TypeError.

This mostly affects:
 - String offsets
 - Arithmetic operations
 - Bitwise operations

Closes GH-5762
2020-07-29 02:51:09 +01:00
George Peter Banyard
25acc4a6b4 Fix [-Wundef] warning in Zend folder 2020-05-12 22:01:51 +02:00
Nikita Popov
53f5cfd99e Drop multi_convert_* APIs
These are no longer used internally, and I'd rather they weren't
used externally either.
2020-05-12 17:10:06 +02:00
Máté Kocsis
4a816584a4
Make float to string casts locale-independent
From now on, float to string casting will always behave locale-independently.
RFC: https://wiki.php.net/rfc/locale_independent_float_to_string
Closes GH-5224

Co-authored-by: George Peter Banyard <girgias@php.net>
2020-05-08 10:52:23 +02:00
Nikita Popov
9c0afc859c Remove op_func from TRY_BINARY_OP1 macro
And move the operator overloading helpers into zend_operators.c,
there's no reason for them to be in the header.
2020-04-01 10:15:03 +02:00
Nikita Popov
fb5bfcb75b Add a ZEND_UNCOMPARABLE value
To explicitly indicate that objects are uncomparable. For now
this has no functional difference from the usual 1 return value,
but makes intent clearer.
2020-03-31 12:36:48 +02:00
Christoph M. Becker
1ff6911d6a Merge branch 'PHP-7.4'
* PHP-7.4:
  Avoid undefined behavior
2019-12-30 23:18:34 +01:00
Christoph M. Becker
98df5c97f4 Avoid undefined behavior 2019-12-30 23:17:39 +01:00
Nikita Popov
c858d17f06 Optimize instanceof_function
Split out the simple equality check into an inline function --
this is one of the common cases.

Replace instanceof_function_ex with zend_class_implements_interface.
There are a few more places where it may be used.
2019-10-25 10:51:17 +02:00
Nikita Popov
19bea48c9a Merge branch 'PHP-7.4'
* PHP-7.4:
  Clean up and clarify instanceof_function_ex()
2019-10-24 17:42:04 +02:00
Nikita Popov
435f269108 Clean up and clarify instanceof_function_ex()
The instanceof_interface_only() function was dead code (always
returned zero).

Clarify that the last parameter indicates whether the passed CE
is interface or class and rewrite the code in terms of assertions.
2019-10-24 17:40:25 +02:00
Dmitry Stogov
b02b81299c Comparison cleanup:
- introduce zend_compare() that returns -1,0,1 dirctly (without intermediate zval)
- remove compare_objects() object handler, and keep only compare() handler
2019-10-07 17:57:49 +03:00
Nikita Popov
33bf1495b2 Don't use asm arithmetic under msan
Clang 9 supports asm goto, so these no longer get automatically
skipped.
2019-09-29 11:38:34 +02:00
Nikita Popov
d26eb10f08 Merge branch 'PHP-7.4' 2019-09-16 15:01:22 +02:00
Nikita Popov
8c0c06da86 Merge branch 'PHP-7.3' into PHP-7.4 2019-09-16 15:01:14 +02:00
Nikita Popov
8a11f1284e Merge branch 'PHP-7.2' into PHP-7.3 2019-09-16 15:01:05 +02:00
Nikita Popov
2f6efd835d Fixed bug #78545
By using an explicit (double) cast.
2019-09-16 15:00:20 +02:00
Nikita Popov
937d5f6598 Compute needle end only after checking it's non-empty 2019-08-28 09:32:18 +02:00
George Peter Banyard
6d578482a9 Improve strpos and strstr function family implementation 2019-08-26 17:11:37 +02:00
Peter Kokot
7f994990ea Merge branch 'PHP-7.4'
* PHP-7.4:
  Remove HAVE_STRCOLL check
2019-06-28 00:13:25 +02:00
Peter Kokot
638c21765c Remove HAVE_STRCOLL check
The strcoll function is defined in the C89 standard and should be
on today's systems always available via the <string.h> header.

https://port70.net/~nsz/c/c89/c89-draft.html#4.11.4.3

- Remove also SKIPIF strcoll check in test
2019-06-28 00:05:55 +02:00
Nikita Popov
6aaab9adf7 Merge branch 'PHP-7.4' 2019-06-20 16:24:31 +02:00
Nikita Popov
e4455c32ed Bail out earlier in zend_memnrstr
To avoid decrementing a null pointer lateron. As we need to check
for NULL here anayway, we should take the chance to bail out right
away.
2019-06-20 16:21:53 +02:00
Dmitry Stogov
63deb0d569 Merge branch 'PHP-7.4'
* PHP-7.4:
  Use ZEND_ASSUME() to perform the following check only on "slow" path.
2019-06-06 14:23:02 +03:00
Dmitry Stogov
741468d145 Use ZEND_ASSUME() to perform the following check only on "slow" path. 2019-06-06 14:22:10 +03:00
Dmitry Stogov
b4d21310cb Merge branch 'PHP-7.4'
* PHP-7.4:
  Fixed reference-counting
2019-06-06 01:40:59 +03:00