Repository navigation
Conversation
CustomSpecification::expand() split the specification on the two placeholder forms it knew and passed every other character through to vsprintf. Two consequences: - A sprintf modifier inside a placeholder, as in %1$05s, was not recognised, so the placeholder reached the rendered SQL intact and vsprintf then formatted the bound parameter name rather than the value. - A conversion other than %s, such as %d or %f, was left for vsprintf to consume, which shifted every later parameter by one. Both now go through one tokenizer that matches a placeholder carrying any sprintf modifier. A conversion other than the string conversion, plain and positional placeholders mixed in one specification, and an unescaped percent sign are all rejected with a named constructor. Modifiers are accepted and ignored, because the value they would format is a placeholder name rather than a value. Escaped percents are skipped by the tokenizer and left for AbstractSql to unescape exactly once. ArgumentPlaceholders is extracted from CustomSpecification so that the validation does not push either class past the complexity gate. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
simon-mundy
added a commit
to simon-mundy/phpdb
that referenced
this pull request
Nov 4, 2026
Capture a positional placeholder's argument number in the placeholder pattern rather than slicing it out with substr(), and require a specification to use plain (%s) or positional (%N$s) placeholders throughout. Mixing them now throws InvalidArgumentException with its own message. Adopted from the review of php-db#247.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The prototype above confirms one tokenizer covers both defects. Recommendation:
Replace the narrow split with a full format-string tokenizer and fail closed.
Walk
preg_match_all(..., PREG_OFFSET_CAPTURE | PREG_SET_ORDER), emit the literal run between matches, then the placeholder. Then:s(case-insensitive). Anything else throws a newforUnsupportedSpecificationConversion($spec, $conversion). That answers item 2:%dno longer reachesvsprintfto eat a value.%1$05s,%.2s,%10s). The modifier applied to a rendered value, which is now a placeholder name, so honoring it would pad:abcinto garbage. Ignoring is the only correct reading; document it in the docblock.%%pairs, any residual%in a literal run (including a trailing%) throws the same exception. The message must name%%as the escape, or specs containing a literal percent become unusable and the change reads as a regression.%%intact forprocessExpression()'svsprintfto unescape exactly once. Do not convert it insideexpand(), or you double-unescape.This deletes the
substr($text, offset: 1, length: -2)surgery and gives one place to also reject mixed%s/%N$sstyles (item 3), since the tokenizer already knows which style each placeholder used. Keep that as a separate exception,forMixedSpecificationPlaceholders, so the message stops blaming the argument.Tests worth adding:
%1$05swith a two-item list renders both values (proved above);%.2sand%10sbehave like%s;%dthrows; trailing%throws;100%% = ANY (ARRAY[%s])still renders one literal percent;'%s = ANY (ARRAY[%1$s])'still throws not-referenced-once.Blast radius is nil:
expand()runs only whensetSpecification()was called, the class is@internal, and the defaultInpath is untouched. These paths currently emit wrong SQL silently, so throwing is strictly better.