Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Improve callbacks in ext/dom and ext/xsl #12627

Merged
merged 12 commits into from
Jan 12, 2024

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Nov 7, 2023

@nielsdos nielsdos added the RFC label Nov 7, 2023
@nielsdos nielsdos force-pushed the rfc-better-registerFunctions branch from 14499b1 to 5274a1c Compare November 15, 2023 18:51
@nielsdos nielsdos force-pushed the rfc-better-registerFunctions branch 5 times, most recently from 542ae01 to d219188 Compare November 30, 2023 22:10
@nielsdos nielsdos force-pushed the rfc-better-registerFunctions branch 3 times, most recently from cfc2fe5 to d7ee39b Compare January 1, 2024 21:13
@nielsdos nielsdos force-pushed the rfc-better-registerFunctions branch from d7ee39b to 48bcd44 Compare January 7, 2024 22:06
@nielsdos nielsdos requested a review from Girgias January 11, 2024 18:44
$xpath->registerPhpFunctions(null);
$xpath->evaluate("//a[php:function('var_dump', string(@href))]");

echo "--- Error cases ---\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually split error cases into a different test file


$xpath = new DOMXPath($doc);

echo "--- Error cases ---\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

ext/dom/xpath.c Outdated

static void dom_xpath_proxy_factory(xmlNodePtr node, zval *child, dom_object *intern, xmlXPathParserContextPtr ctxt)
{
(void) ctxt;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have a ZEND_UNUSED macro attribute for those cases?

ext/dom/xpath.c Outdated
Comment on lines 66 to 50
if (!zend_is_executing()) {
xmlGenericError(xmlGenericErrorContext,
"xmlExtFunctionTest: Function called from outside of PHP\n");
error = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Early return to have the happy path without any indentation?

Also maybe UNEXPECTED() ?

ext/dom/xpath.c Outdated
ZEND_PARSE_PARAMETERS_END();

if (zend_string_equals_literal(namespace, "http://php.net/xpath")) {
zend_argument_value_error(1, "must not be \"http://php.net/xpath\" because it is reserved for PHP");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zend_argument_value_error(1, "must not be \"http://php.net/xpath\" because it is reserved for PHP");
zend_argument_value_error(1, "must not be \"http://php.net/xpath\" because it is reserved by PHP");

Seems more logical?

ext/dom/xpath_callbacks.c Show resolved Hide resolved
Comment on lines 397 to 399
if (UNEXPECTED(result == FAILURE)) {
return FAILURE;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong.

I wouldn't expect result to ever be FAILURE see the comment in zend_API.h:

/* Can only return FAILURE if EG(active) is false during late engine shutdown.
 * If the call or call setup throws, EG(exception) will be set and the retval
 * will be UNDEF. Otherwise, the retval will be a non-UNDEF value. */

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, a bit surprising though.

Comment on lines +445 to +448
if (UNEXPECTED(num_args == 0)) {
zend_throw_error(NULL, "Function name must be passed as the first argument");
goto cleanup_no_obj;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an API usage failure? As this seems like it should only ever be possible to hit this case if C code does something wrong? Which maybe means this should be an assertion? Or am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is possible to hit by using a wrong XSL template that does: php:function() in its xpath expression instead of php:function('function_name').

Comment on lines 453 to 458
/* Last element of the stack is the function name */
xmlXPathObjectPtr obj = valuePop(ctxt);
if (obj->stringval == NULL) {
zend_type_error("Handler name must be a string");
goto cleanup;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Member Author

@nielsdos nielsdos Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #12627 (comment) it's possible to hit this using php:function(1234) for example

ext/dom/xpath.c Show resolved Hide resolved
@nielsdos nielsdos requested a review from kocsismate as a code owner January 11, 2024 22:25
@nielsdos
Copy link
Member Author

I believe I fixed or replied to all issues.

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nielsdos nielsdos merged commit 90785dd into php:master Jan 12, 2024
1 of 2 checks passed
@frederikbosch
Copy link
Contributor

The registerPhpFunctionNS functions are not yet documented, are they? I could provide these. Is it correct that I should add them here and then create a file for them inside the xsltprocessor folder?

@nielsdos
Copy link
Member Author

Hi @frederikbosch

The registerPhpFunctionNS functions are not yet documented, are they?

Yes it's not documented yet, we're tracking documentation status in php/doc-en#3872.
Thanks for your interest!

I could provide these. Is it correct that I should add them here and then create a file for them inside the xsltprocessor folder?

Indeed, adding an entry there for the version number, and if I were you I'd make a copy of the registerPHPFunctions xml document as a start for registerPHPFunctionNS and modify from there.

Same thing should be done for DOMXPath, but if you get a PR going for XSLTProcessor that is already great and can serve as a basis for DOMXPath's version of registerPHPFunctionNS.

The docs repo contains instructions on how to build the docs locally.

@frederikbosch
Copy link
Contributor

Thanks @nielsdos, as I suggested this change in the internals RFC discussion I am absolutely interested in this addition. It will serve my XSL 2.0 transpiler library very well. So, I'd say it's my turn to thank you for the implementation! And I'll do my best to come up with both PRs.

@frederikbosch
Copy link
Contributor

See the PR @ doc-en.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants