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

DOMXPath::quote(string $str): string #13456

Merged
merged 19 commits into from
Feb 22, 2024
Merged
2 changes: 2 additions & 0 deletions ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,8 @@ public function registerNamespace(string $prefix, string $namespace): bool {}
public function registerPhpFunctions(string|array|null $restrict = null): void {}

public function registerPhpFunctionNS(string $namespaceURI, string $name, callable $callable): void {}

public static function quote(string $str): string {}
}
#endif

Expand Down
12 changes: 11 additions & 1 deletion ext/dom/php_dom_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions ext/dom/tests/DOMXPath_quote.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
--TEST--
Test DOMXPath::quote with various inputs
--SKIPIF--
<?php if (!class_exists('DOMXPath')) die('skip DOMXPath not available.'); ?>
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
--FILE--
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
<?php
$dom = new DOMDocument();
$xpath = new DOMXPath($dom);


/**
* Quote a string for use in an XPath expression.
*
* Example: $xp->query("//span[contains(text()," . $xp->quote($string) . ")]")
*
* @param string $string string to quote.
* @return string quoted string.
*/
function UserlandDOMXPathQuote(string $string): string
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it makes sense trying to add this to the fuzzer to see if it finds differences in behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't know what fuzzer you're talking about, but sounds good because if there is any difference, it's probably a bug in the C version 👍

Copy link
Member

Choose a reason for hiding this comment

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

We could fuzz this, but it will be a bit cumbersome to write a specific driver for this. We could ask ourselves whether we want to do it generalised for all these kinds of quoting methods.

{
if (false === \strpos($string, '\'')) {
return '\'' . $string . '\'';
}
if (false === \strpos($string, '"')) {
return '"' . $string . '"';
}
// if the string contains both single and double quotes, construct an
// expression that concatenates all non-double-quote substrings with
// the quotes, e.g.:
// 'foo'"bar => concat("'foo'", '"bar")
$sb = [];
while ($string !== '') {
$bytesUntilSingleQuote = \strcspn($string, '\'');
$bytesUntilDoubleQuote = \strcspn($string, '"');
$quoteMethod = ($bytesUntilSingleQuote > $bytesUntilDoubleQuote) ? "'" : '"';
$bytesUntilQuote = \max($bytesUntilSingleQuote, $bytesUntilDoubleQuote);
$sb[] = $quoteMethod . \substr($string, 0, $bytesUntilQuote) . $quoteMethod;
$string = \substr($string, $bytesUntilQuote);
}
$sb = \implode(',', $sb);
return 'concat(' . $sb . ')';
}



$tests = [
'foo' => "'foo'", // no quotes
'"foo' => '\'"foo\'', // double quotes only
'\'foo' => '"\'foo"', // single quotes only
'\'foo"bar' => 'concat("\'foo",\'"bar\')', // both; double quotes in mid-string
'\'foo"bar"baz' => 'concat("\'foo",\'"bar"baz\')', // multiple double quotes in mid-string
'\'foo"' => 'concat("\'foo",\'"\')', // string ends with double quotes
'\'foo""' => 'concat("\'foo",\'""\')', // string ends with run of double quotes
'"\'foo' => 'concat(\'"\',"\'foo")', // string begins with double quotes
'""\'foo' => 'concat(\'""\',"\'foo")', // string begins with run of double quotes
'\'foo""bar' => 'concat("\'foo",\'""bar\')', // run of double quotes in mid-string
];

foreach ($tests as $input => $expected) {
$result = $xpath->quote($input);
if ($result === $expected) {
echo "Pass: {$input} => {$result}\n";
} else {
echo 'Fail: ';
var_dump([
'input' => $input,
'expected' => $expected,
'result' => $result,
'userland_implementation_result' => UserlandDOMXPathQuote($input),
]);
}
}
?>
--EXPECT--
Pass: foo => 'foo'
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
Pass: "foo => '"foo'
Pass: 'foo => "'foo"
Pass: 'foo"bar => concat("'foo",'"bar')
Pass: 'foo"bar"baz => concat("'foo",'"bar"baz')
Pass: 'foo" => concat("'foo",'"')
Pass: 'foo"" => concat("'foo",'""')
Pass: "'foo => concat('"',"'foo")
Pass: ""'foo => concat('""',"'foo")
Pass: 'foo""bar => concat("'foo",'""bar')
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
87 changes: 87 additions & 0 deletions ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,93 @@ PHP_METHOD(DOMXPath, registerPhpFunctionNS)
);
}

/* {{{ */
PHP_METHOD(DOMXPath, quote) {
char *input;
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
size_t input_len;
char *output;
size_t output_len = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &input, &input_len) ==
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
FAILURE) {
RETURN_THROWS();
}
if (memchr(input, '\'', input_len) == NULL) {
output_len = input_len + 2;
output = emalloc(output_len);
output[0] = '\'';
memcpy(output + 1, input, input_len);
output[output_len - 1] = '\'';
} else if (memchr(input, '"', input_len) == NULL) {
output_len = input_len + 2;
output = emalloc(output_len);
output[0] = '"';
memcpy(output + 1, input, input_len);
output[output_len - 1] = '"';
} else {
// need to do the concat() trick published by Robert Rossney at https://stackoverflow.com/a/1352556/1067003
// first lets calculate the length (probably faster than repeated reallocs)
output_len = strlen("concat(");
for (size_t i = 0; i < input_len; ++i) {
uintptr_t bytesUntilSingleQuote =
(uintptr_t)memchr(input + i, '\'', input_len - i);
if (bytesUntilSingleQuote == 0) {
bytesUntilSingleQuote = input_len - i;
} else {
bytesUntilSingleQuote = bytesUntilSingleQuote - (uintptr_t)(input + i);
}
uintptr_t bytesUntilDoubleQuote =
(uintptr_t)memchr(input + i, '"', input_len - i);
if (bytesUntilDoubleQuote == 0) {
bytesUntilDoubleQuote = input_len - i;
} else {
bytesUntilDoubleQuote = bytesUntilDoubleQuote - (uintptr_t)(input + i);
}
const size_t bytesUntilQuote =
(bytesUntilSingleQuote > bytesUntilDoubleQuote)
? bytesUntilSingleQuote
: bytesUntilDoubleQuote;
i += bytesUntilQuote - 1;
output_len += 1 + bytesUntilQuote + 1 + 1; // "bytesUntilQuote"[,)]
}
output = emalloc(output_len);
size_t outputPos = strlen("concat(");
memcpy(output, "concat(", outputPos);
for (size_t i = 0; i < input_len; ++i) {
uintptr_t bytesUntilSingleQuote =
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
(uintptr_t)memchr(input + i, '\'', input_len - i);
if (bytesUntilSingleQuote == 0) {
bytesUntilSingleQuote = input_len - i;
} else {
bytesUntilSingleQuote = bytesUntilSingleQuote - (uintptr_t)(input + i);
}
uintptr_t bytesUntilDoubleQuote =
(uintptr_t)memchr(input + i, '"', input_len - i);
if (bytesUntilDoubleQuote == 0) {
bytesUntilDoubleQuote = input_len - i;
} else {
bytesUntilDoubleQuote = bytesUntilDoubleQuote - (uintptr_t)(input + i);
}
const size_t bytesUntilQuote =
(bytesUntilSingleQuote > bytesUntilDoubleQuote)
? bytesUntilSingleQuote
: bytesUntilDoubleQuote;
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
const char quoteMethod =
(bytesUntilSingleQuote > bytesUntilDoubleQuote) ? '\'' : '"';
output[outputPos++] = quoteMethod;
memcpy(output + outputPos, input + i, bytesUntilQuote);
outputPos += bytesUntilQuote;
output[outputPos++] = quoteMethod;
i += bytesUntilQuote - 1;
output[outputPos++] = ',';
}
output[outputPos - 1] = ')';
}
RETVAL_STRINGL(output, output_len);
efree(output); // todo: directly return a string and avoid the copy, probably possible but idk how (reviewers may want to check this)
divinity76 marked this conversation as resolved.
Show resolved Hide resolved
}
/* }}} */

#endif /* LIBXML_XPATH_ENABLED */

#endif
Loading