Commit graph

270 commits

Author SHA1 Message Date
Ilija Tovilo
80022c035b
Fix failed assertion with throwing __toString in binary const expr
Solve this with the same pattern as ZEND_AST_GREATER[_EQUAL].

Fixes OSS-Fuzz #434346548
Closes GH-19291
2025-07-30 13:34:01 +02:00
Oleg Efimov
087f38f347
Fix GH-18695: float numbers zero fraction is now preserved in zend_ast_export() (#18699) 2025-05-29 13:46:11 -07:00
Niels Dossche
98cb17f4fd
Fix OSS-Fuzz #418106144
The VM assumes that an exception must be handled when the AST evaluation
returns FAILURE. However, the comparison functions always return SUCCESS
even if an exception happened. This can be fixed in
zend_ast_evaluate_inner() or we can make is_smaller_function() etc check
for the exception. I chose the former to avoid impact or API breaks.
Perhaps in the future the comparison functions should either return void
or return whether an exception happened, as to be not misleading.

Closes GH-18589.
2025-05-19 19:05:32 +02:00
Niels Dossche
160a4a65ad
Export visibility for promoted property (8.3) 2024-12-17 19:14:07 +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
Nikita Popov
c230aa9be3 Correctly handle multiple constants in typed declaration
While here also fix AST printing support.
2023-05-21 14:17:01 +01:00
Ilija Tovilo
5107483cd6
Correctly copy lineno for zval asts (#11203)
The comment was incorrect. Zval ASTs store their lineno in u2, but u2 does not
get copied in ZVAL_COPY. This triggers use-of-uninitialized errors with MSAN.
Unfortunately, I don't have a simple reproducer.
2023-05-07 13:17:19 +02:00
Ilija Tovilo
ba33bbe260
Merge branch 'PHP-8.2'
* PHP-8.2:
  Unary minus const expression consistency
2023-03-31 14:28:39 +02:00
Ilija Tovilo
ed80a7e8c9
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Unary minus const expression consistency
2023-03-31 14:28:25 +02:00
Ilija Tovilo
41bbb116dd
Unary minus const expression consistency
- of 0.0 should result in -0.0

Closes GH-10978
2023-03-31 14:27:54 +02:00
Ilija Tovilo
35a36b13e5
Fix comp-time and constant evaluation of dynamic class constant fetch
Fixes GH-10486
Fixes oss-fuzz #55436
Fixes oss-fuzz #55472
Closes GH-10487
2023-02-02 19:18:17 +01:00
Ilija Tovilo
9e097822e8
Fix lineno for all constant expressions
Fixes GH-8821
Closes GH-8855
2023-02-02 19:03:47 +01:00
Christoph M. Becker
bf1cfc0753
Revert GH-10300
Cf. <https://github.com/php/php-src/pull/10220#issuecomment-1383739816>.

This reverts commit 68ada76f9a.
his reverts commit 45384c6e20.
This reverts commit ef7fbfd710.
This reverts commit 9b9ea0d7c6.
This reverts commit f15747c26b.
This reverts commit e883ba93c4.
This reverts commit 7e87551c37.
This reverts commit 921274d2b8.
This reverts commit fc1f528e5e.
This reverts commit 0961715cda.
This reverts commit a93f264526.
This reverts commit 72dd94e1c6.
This reverts commit 29b2dc8964.
This reverts commit 05c7653bba.
This reverts commit 5190e5c260.
This reverts commit 6b55bf228c.
This reverts commit 184b4a12d3.
This reverts commit 4c31b7888a.
This reverts commit d44e9680f0.
This reverts commit 4069a5c43f.
2023-01-16 12:22:54 +01:00
Max Kellermann
5190e5c260 Zend/zend_ast: include cleanup 2023-01-15 15:07:58 +00:00
Niels
23fe58c3a8
Remove _zend_ast_decl->lex_pos (#10046)
This struct member was only written to, but never read.
2022-12-22 15:01:21 +01:00
Christoph M. Becker
9f0e4a55e7
Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-9769: Misleading error message for unpacking of objects
2022-12-02 13:12:14 +01:00
Christoph M. Becker
6b1f4c5a44
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix GH-9769: Misleading error message for unpacking of objects
2022-12-02 13:11:51 +01:00
蝦米
93592ea743
Fix GH-9769: Misleading error message for unpacking of objects
Only arrays can be unpacked in constant expressions.

Closes GH-9776.
2022-12-02 13:10:59 +01:00
Ilija Tovilo
683d81e4bd
Fix incorrect short-circuiting in constant expressions (#10030)
Fixes GH-10014
2022-12-01 19:30:44 +01:00
Ilija Tovilo
8731fb2d09
Fix caching of default params with side-effects
Fixes GH-9965
Closes GH-9935
2022-11-17 11:52:12 +01:00
Ilija Tovilo
1d6b32f65c
Remove unnecessary ast eval bailout
We can just reset the filename_override to NULL in php_request_shutdown.

Closes GH-9805
2022-10-27 10:54:59 +02:00
Ilija Tovilo
d36874d002
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix class name FQN when AST dumping new and class const
2022-09-02 08:58:27 +02:00
Ilija Tovilo
2cfb028e22
Fix class name FQN when AST dumping new and class const
Fixes GH-9447
Closes GH-9462
2022-09-02 08:57:26 +02:00
Ilija Tovilo
f957e3e7f1
Fix arrow function with never return type
Fixes GH-7900
Closes GH-9103
2022-07-29 12:25:09 +02:00
Ilija Tovilo
7aadbcb8f4
GH-8344 Fetch properties of enums in const expressions 2022-07-18 23:52:28 +02:00
Ilija Tovilo
ddc0b490f7
Allow arbitrary const expressions in backed enums
Closes GH-7821
Closes GH-8190
Closes GH-8418
2022-06-12 22:56:05 +02:00
Máté Kocsis
7850c10389
Add support for readonly classes (#7305)
RFC: https://wiki.php.net/rfc/readonly_classes
2022-05-16 20:40:23 +02:00
Ilija Tovilo
3b4eaf67ec
Merge branch 'PHP-8.1'
* PHP-8.1:
  Fix preloading of constants containing enums
2022-04-21 11:59:15 +02:00
Ilija Tovilo
4397811db2
Fix preloading of constants containing enums
Fixes GH-8133
2022-04-21 11:57:12 +02:00
Dmitry Stogov
efb014dda2 Reset EG(filename_override) after fatal error
Fixes oss-fuzz #45492
2022-03-14 11:07:49 +03:00
Ilija Tovilo
e3ef7bbbb8
Adjust filename/lineno for constant expressions
Closes GH-7771
Closes GH-8124
2022-03-09 18:41:04 +01:00
Tyson Andre
944b6b6bbd
Merge concatenated literal strings while compiling. (#7948)
The output of token_get_all is unaffected, so projects such as the userland
nikic/php-parser are unaffected.

Extensions such as nikic/php-ast which expose the internal php ast would see
literals be flattened, though.

This makes php significantly faster at parsing code such as
`$x = eval('return ' . var_export(str_repeat("\0", 100), true) . ';');`
and avoids the stack overflow from recursing 100000 times in
zend_eval_const_expr to process `'' . "\0" . '' . "\0" . ...`

Closes GH-7946

* Don't create binary op if unnecessary
* Update Zend/zend_ast.c

Co-authored-by: Nikita Popov <nikita.ppv@googlemail.com>
2022-01-16 13:31:58 -05:00
Cameron Porter
812df2bd8a Fix bug #81611
Add zend_fetch_class_with_scope() which accepts a scope to use for
self/parent, and use that during constant expression evaluation.

Closes GH-7649.
2021-11-16 14:40:06 +01:00
Nikita Popov
f34114b1fb Export AST for default value strings in reflection
When dumping default values in ReflectionXXX::__toString(), for
expression initializers print the AST export instead of trying to
evaluate the expression. With the introduction of "new in
initializers" the result of the evaluation will commonly not be
printable at all, and "__toString" will throw an exception, which
is not particularly useful. Using the AST export also provides more
information on how the parameter was originally declared, e.g. it
preserves the fact that a certain constant was used.

Closes GH-7540.
2021-10-01 16:13:35 +02:00
Nikita Popov
7d6a7e78fc Use array_set_zval_key() in zend_ast_add_array_element()
This reimplemented basically the same logic.
2021-08-31 10:52:21 +02:00
Nikita Popov
b964d007e3 Fix AST print of first class callable in anon class expr
This will cause a compile error later, but the expression is
printed first.

Fixes oss-fuzz #37473.
2021-08-26 12:00:37 +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
8834cf013b Handle missing class when evaluating CONST_ENUM_INIT
When resolving constants on a dynamically declared class during
preloading, the enum class may not be available. Fail gracefully
in that case.

Possibly we shouldn't be trying to evaluate constants on
non-linked classes at all?
2021-07-27 14:36:38 +02:00
Nikita Popov
a374230c15 Add support for internal enums
This adds support for internal enums with the same basic approach
as userland enums. Enum values are stored as CONSTANT_AST and
objects created during constant updating at runtime. This means
that we need to use mutable_data for internal enums.

This just adds basic support and APIs, it does not include the
stubs integration from #7212.

Closes GH-7302.
2021-07-27 09:19:14 +02:00
Nikita Popov
6780aaa532 Implement readonly properties
Add support for readonly properties, for which only a single
initializing assignment from the declaring scope is allowed.

RFC: https://wiki.php.net/rfc/readonly_properties_v2

Closes GH-7089.
2021-07-20 12:05:46 +02:00
Nikita Popov
b6dcab2cbb Fix assert with attribute + first-class callable in assert
This is going to result in a compile-time error, but AST printing
is invoked first and should handle it gracefully. Handle it by
falling back to more generic printing code.

Fixes oss-fuzz #36264.
2021-07-19 12:08:03 +02:00
Joe Watkins
d0b09a7be4 Add first-class callables
Support acquiring a Closure to a callable using the syntax
func(...), $obj->method(...), etc. This is essentially a
shortcut for Closure::fromCallable().

RFC: https://wiki.php.net/rfc/first_class_callable_syntax

Closes GH-7019.

Co-Authored-By: Nikita Popov <nikita.ppv@gmail.com>
2021-07-14 14:37:25 +02:00
Nikita Popov
52d3d0d8d7 Add limited support for new in initializers
Add support for new expressions inside parameter default values,
static variable initializers, global constant initializers and
attribute arguments.

RFC: https://wiki.php.net/rfc/new_in_initializers

Closes GH-7153.
2021-07-13 14:03:20 +02:00
George Peter Banyard
069a9fa5e4
Pure Intersection types (#6799)
Implement pure intersection types RFC

RFC: https://wiki.php.net/rfc/pure-intersection-types

Co-authored-by: Nikita Popov <nikic@php.net>
Co-authored-by: Ilija Tovilo <ilutov@php.net>
2021-07-05 14:11:03 +02:00
Patrick Allaert
aff365871a Fixed some spaces used instead of tabs 2021-06-29 11:30:26 +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
Tyson Andre
d60bc0e2d9
Support doc comments on enum cases (#6984)
Because php supports doc comments on class constants, I believe it would also
make sense to support them on enum cases.

I don't have strong opinions about whether attributes should be moved to be the
last element or whether the doc comment should go after the attribute,
but the ast will likely change again before php 8.1 is stable.
So far, all attributes are the last ast child node.

I didn't notice that doc comments weren't implemented due to
https://github.com/php/php-src/pull/6489 being a large change.

https://wiki.php.net/rfc/enumerations
did not mention whether or not doc comments were meant to be supported
2021-05-14 15:25:03 -04:00
Dmitry Stogov
4dcde9cf18 Don't evalutae ZEND_AST_CLASS_CONST to ZEND_AST_CONSTANT ar
compile-time. Keep at to run-time and use Fast Class Cache during
run-time evaluation.
2021-04-09 17:48:28 +03:00
George Peter Banyard
5caaf40b43
Introduce pseudo-keyword ZEND_FALLTHROUGH
And use it instead of comments
2021-04-07 00:46:29 +01:00
Ilija Tovilo
269c8dac1d
Implement enums
RFC: https://wiki.php.net/rfc/enumerations

Co-authored-by: Nikita Popov <nikita.ppv@gmail.com>

Closes GH-6489.
2021-03-17 19:08:03 +01:00