Skip to content

Don't open-code string value accesses in xpath #15007

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

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,22 +486,22 @@ PHP_METHOD(DOMXPath, quote) {
}
if (memchr(input, '\'', input_len) == NULL) {
zend_string *const output = zend_string_safe_alloc(1, input_len, 2, false);
output->val[0] = '\'';
memcpy(output->val + 1, input, input_len);
output->val[input_len + 1] = '\'';
output->val[input_len + 2] = '\0';
ZSTR_VAL(output)[0] = '\'';
memcpy(ZSTR_VAL(output) + 1, input, input_len);
ZSTR_VAL(output)[input_len + 1] = '\'';
ZSTR_VAL(output)[input_len + 2] = '\0';
RETURN_STR(output);
} else if (memchr(input, '"', input_len) == NULL) {
zend_string *const output = zend_string_safe_alloc(1, input_len, 2, false);
output->val[0] = '"';
memcpy(output->val + 1, input, input_len);
output->val[input_len + 1] = '"';
output->val[input_len + 2] = '\0';
ZSTR_VAL(output)[0] = '"';
memcpy(ZSTR_VAL(output) + 1, input, input_len);
ZSTR_VAL(output)[input_len + 1] = '"';
ZSTR_VAL(output)[input_len + 2] = '\0';
RETURN_STR(output);
} else {
smart_str output = {0};
// need to use the concat() trick published by Robert Rossney at https://stackoverflow.com/a/1352556/1067003
smart_str_appendl(&output, "concat(", 7);
smart_str_appendl(&output, ZEND_STRL("concat("));
const char *ptr = input;
const char *const end = input + input_len;
while (ptr < end) {
Expand All @@ -518,7 +518,7 @@ PHP_METHOD(DOMXPath, quote) {
smart_str_appendc(&output, ',');
}
ZEND_ASSERT(ptr == end);
output.s->val[output.s->len - 1] = ')';
ZSTR_VAL(output.s)[output.s->len - 1] = ')';
RETURN_STR(smart_str_extract(&output));
}
}
Expand Down
Loading