Skip to content

Fix GH-19088: Make sscanf %c read whitespace - #22041

Open
prateekbhujel wants to merge 4 commits into
php:masterfrom
prateekbhujel:prateekbhujel/fix-gh-19088-sscanf-c-whitespace
Open

prateekbhujel wants to merge 4 commits into
php:masterfrom
prateekbhujel:prateekbhujel/fix-gh-19088-sscanf-c-whitespace

Conversation

@prateekbhujel

@prateekbhujel prateekbhujel commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes GH-19088.

%c disables the leading whitespace skip, but still uses the %s scan path, which stops at whitespace. That makes sscanf(' ', '%c%n', ...) assign an empty string and leave the offset at 0.

Keep %c separate from %s so it consumes the requested characters, including whitespace. The same scanner handles fscanf(), so its existing %c expectations are updated too.

This targets master (PHP 8.7), with an UPGRADING note for the behavior change.

Tested on a fresh PHP 8.7 debug build: 60 scanf tests passed; 8 tests skipped because they require a 32-bit platform.

sapi/cli/php run-tests.php -q ext/standard/tests/strings/*sscanf*.phpt ext/standard/tests/strings/gh19088.phpt ext/standard/tests/strings/bug21730.phpt ext/standard/tests/file/fscanf*.phpt
git diff --check

Comment thread ext/standard/tests/strings/gh19088.phpt Resolved
Comment thread ext/standard/scanf.c Resolved
@prateekbhujel
prateekbhujel force-pushed the prateekbhujel/fix-gh-19088-sscanf-c-whitespace branch from acdb609 to 08ea8fb Compare May 14, 2026 21:16
@prateekbhujel

Copy link
Copy Markdown
Contributor Author

@devnexen quick follow-up on this one: the extra %c cases and the UPGRADING note are in now, and the checks are green. Is there anything else you would want changed here before merge?

@hakre hakre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you summarize if/how %c differs now from the original code? (see file comment)

Comment thread ext/standard/scanf.c Resolved
Comment thread UPGRADING Resolved
@prateekbhujel

Copy link
Copy Markdown
Contributor Author

can you summarize if/how %c differs now from the original code? (see file comment)

Sure. Previously %c already did not skip leading whitespace, but once the conversion started it still stopped when the next input character was whitespace, because it shared the %s stop condition.

Now %c consumes the next character(s) exactly, including whitespace. So sscanf(" hi", "%c") reads " ", and a width like %3c can include spaces. %s is unchanged and still stops at whitespace.

@hakre

hakre commented May 26, 2026

Copy link
Copy Markdown
Contributor

can you summarize if/how %c differs now from the original code? (see file comment)

Sure. Previously %c already did not skip leading whitespace, but once the conversion started it still stopped when the next input character was whitespace, because it shared the %s stop condition.

Now %c consumes the next character(s) exactly, including whitespace. So sscanf(" hi", "%c") reads " ", and a width like %3c can include spaces. %s is unchanged and still stops at whitespace.

Thanks for the detailed clarification – that matches exactly what I expected. The key point is that %c was already not skipping leading whitespace (correct), but the op = 's' alias made it stop at whitespace, which was the actual bug. So this PR restores the ANSI C / Tcl behavior that was lost in the original PHP port.

Given that this is a straightforward bug fix (and not a new feature or intentional behavioral change), it would be great to treat it as such – i.e., target the oldest supported branch where the bug exists, and remove the UPGRADING note (or replace it with a bug‑fix entry in the changelog). That way users on stable versions get the correct behavior too.

I mentioned the file comment in scanf.c because it explicitly references the Tcl 8.3.0 original code. If you haven’t already, it might be interesting to cross‑check that Tcl implementation – it already had op = 'c' and SCAN_NOSKIP for %c, so the PHP bug was indeed a porting oversight.

Happy to help with any further testing or wording if needed. 😊

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

Ah yes, that makes sense.

I added the UPGRADING note because the visible result changes, but I agree with your framing here: this is fixing the %c handling from the old port, not adding a new conversion behavior. %c already had SCAN_NOSKIP; the bug was that it was still sharing the %s whitespace stop path.

I’ll remove the UPGRADING note and keep this as a bug fix. For the target branch, I’ll follow whichever oldest supported branch the maintainers want this based on, rather than treating it as master-only. I’ll also cross-check the Tcl code before the next update so the PR text is clear about why this is a porting oversight.

Thanks, this helped make the direction much clearer.

@hakre

hakre commented May 26, 2026

Copy link
Copy Markdown
Contributor

That’s a great direction – thanks for taking the time to dig into the Tcl history. One small nuance: @devnexen has a point that any change in visible behavior, even a bug fix, can be a BC break for code that relied on the old (broken) %c behavior. A short UPGRADING note that says something like “%c now correctly reads whitespace characters (restoring ANSI C behavior); previously it stopped at whitespace” might still be helpful, even if we frame it as a correction rather than a new feature. That way users are warned, and the fix stays clearly classified as a bug fix.

Happy to help with the exact wording if needed. 😊

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

Yes, agreed. That is the better balance.

I put the UPGRADING note back, but worded it as a correction rather than a new feature:

%c conversions now correctly read whitespace characters, restoring ANSI C behavior. Previously, they stopped at whitespace.

Pushed that in d88f22eab35. Thanks for pointing out the BC angle here — it makes sense to warn users while still keeping the PR framed as a bug fix.

@hakre

hakre commented May 26, 2026

Copy link
Copy Markdown
Contributor

Given that this is a bug fix that isn't a security fix,
the contributing guide suggests targeting the lowest actively supported branch where the bug exists — if I'm reading it right, that would be 8.4 at the moment.

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

Understood, that reading makes sense to me.

I would rather leave the final branch/base decision to the maintainers here instead of guessing and moving the PR myself. If @devnexen, @Girgias, or @TimWolla confirm that PHP-8.4 is the right target, I can retarget/rebase the PR there and keep the current wording/tests.

So for now I will wait for their call.

@hakre

hakre commented May 27, 2026

Copy link
Copy Markdown
Contributor

The contributing guide is pretty clear that bug fixes go to the lowest supported branch and master is for RFCs—I think you're safe to retarget to PHP-8.4. If the maintainers disagree, they'll let us know, but taking the initiative is always appreciated. 😊

@TimWolla

Copy link
Copy Markdown
Member

From what I see, @ndossche requested this as a master-only change in the corresponding issue since a change to long-standing behavior that is not obviously buggy is not something folks expect within the the same PHP version - and I would agree with that.

I defer to someone else to evaluate the patch itself.

@hakre

hakre commented May 27, 2026

Copy link
Copy Markdown
Contributor

For what it’s worth, the BC impact here is minimal: because the old %c stopped at whitespace, it was effectively unusable for its intended purpose. The fix restores the documented ANSI C behaviour without breaking any reasonable existing code.

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

Any updates here, or is anything else needed from my side?

@Girgias

Girgias commented Jun 28, 2026

Copy link
Copy Markdown
Member

This has been the behaviour since PHP 4.3.2: https://3v4l.org/6t26R#veol

And while this might be a bug fix, my experience fixing stuff for SPL tells me that people will either be working around this or expecting this, so we will get people complaining we broke something.

@hakre

hakre commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

This has been the behaviour since PHP 4.3.2: https://3v4l.org/6t26R#veol

Interesting, PHP 4.3.2 is from a time and major PHP version that is different from ... SPL:

And while this might be a bug fix, my experience fixing stuff for SPL tells me that people will either be working around this or expecting this, so we will get people complaining we broke something.

This leaves us with the range you opened up here: User work arounds or expecting this. Please assess that in context of the fix, that is about the original port to PHP from TCL, not the SPL. And very interesting in my eyes, but decide for yourself @Girgias

@hakre

hakre commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

In case it helps @Girgias (and naturally for others who are interested), some resources:

(refs, please vet the version/release dates independently, I took the TCL version named in the PHP sources)

@prateekbhujel
prateekbhujel force-pushed the prateekbhujel/fix-gh-19088-sscanf-c-whitespace branch from d88f22e to c8f19f4 Compare June 28, 2026 18:42
@prateekbhujel

Copy link
Copy Markdown
Contributor Author

Thanks @Girgias, that concern is fair. I rebased this on current master now, so the conflicts should be gone and CI is running again.

I am keeping this master-only as @TimWolla suggested. My view is still that this corrects the %c handling, but I understand the BC risk with behavior this old. I will leave the final call to @Girgias / maintainers here.

@hakre

hakre commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Thanks @Girgias, that concern is fair.

Hmm, good point and thinking I probably have barken up the wrong tree previously: Please share your assessment of the risk then in regards to the SPL, which was introduced later. @prateekbhujel

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

Thanks @hakre, yeah I might have phrased that badly.

I do not read the SPL part as being directly related to this code path. I understood @Girgias’ point more as a maintainer experience thing: even when some old behavior looks wrong, if it has existed for a long time, some users may already rely on it or have workarounds around it.

So for this PR my risk assessment is not really SPL-specific. The risk is that %c in sscanf() / fscanf() has behaved this way since PHP 4.3.2, so changing it in a stable branch would be surprising for some users.

That is why I think keeping it master-only, with the UPGRADING note, is the safer framing. I still think this corrects the %c handling, but I agree the final BC call should be with maintainers.

@hakre

hakre commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Thanks @hakre, yeah I might have phrased that badly.

You're welcome & all fine - if then it's my Englitsch I'm sure.

I do not read the SPL part as being directly related to this code path. I understood @Girgias’ point more as a maintainer experience thing: even when some old behavior looks wrong, if it has existed for a long time, some users may already rely on it or have workarounds around it.

So for this PR my risk assessment is not really SPL-specific. The risk is that %c in sscanf() / fscanf() has behaved this way since PHP 4.3.2, so changing it in a stable branch would be surprising for some users.

That is why I think keeping it master-only, with the UPGRADING note, is the safer framing. I still think this corrects the %c handling, but I agree the final BC call should be with maintainers.

Which seems was the concensus already earlier, and the reason to fix on master not on a branch with a number. I'd also not go that far and say the fix is risky: From what I know any workaround involves not using %c so is technically not affected. Perhaps the maintainers could voice what would help them to better assess the risk, because I don't write this in ignorance. E.g. changing the trim default cutset that is actually backwards breaking and with its own risks, and be it only in the toolings in use (refs 3v4l.org).

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

@hakre Yeah that makes sense. I also do not want to overstate the risk here. My point was mostly that since this behavior has existed for so long, I understand why maintainers may want to be conservative, even if the practical breakage is probably small.

@hakre

hakre commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

You're a genius in my eyes: it not only makes sense, when thinking conservatively and addressing the SPL as the messenger (the thing it actually is), making out the fix must be in the PHP language in the sscanf() and fscanf() functions only, to address the differences between the PHP standard library and the SPL as both are part of it, but they are different libraries.

The SPL retains the implementation defined behaviour under the modernization of PHP: PHP 5 with the introduction of the unified __construct()-or and the SPL following it suite, users since then use the SPL as the object library it is, PHP's *scanf() in the languages own standard library can then enable them (the language users).

Wow, this reflects the heritage of ANSI C, TCL, PHP 4.3 and later, and last, but not least, the SPL.

I really love this bugfix, and it's not only because of the code, thanks for keeping the discussion going @prateekbhujel /cc @Girgias

@Girgias

Girgias commented Jul 2, 2026

Copy link
Copy Markdown
Member

I will leave the final call to @Girgias / maintainers here.

I can't make a final call, this is not how decisions are made here. Please send a quick email to internals if other people think it's problematic, if not I'll merge it.

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

@hakre thank you, that honestly means a lot. I am still learning my way around php-src and internals, so the discussion here helped me understand the framing much better.

@Girgias I sent the quick mail to internals now as suggested. I will wait for feedback there, and if there are no objections we can continue from that.

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

@hakre if you have a moment, could you also share your thoughts in the internals discussion? Your points here about the practical BC risk would be helpful there too.

@prateekbhujel

Copy link
Copy Markdown
Contributor Author

@Girgias Just following up on this. The internals discussion has been open for about 8 weeks now with no objections or concerns raised:
https://discourse.thephp.foundation/t/php-dev-discussion-sscanf-fscanf-c-whitespace-handling-in-master-gh-19088/5701

The branch is rebased, mergeable, and all CI checks are green. Let me know if you're good to merge this or if anything else is needed !

@prateekbhujel
prateekbhujel force-pushed the prateekbhujel/fix-gh-19088-sscanf-c-whitespace branch from c8f19f4 to 3b03fbd Compare September 22, 2026 13:51
@prateekbhujel

Copy link
Copy Markdown
Contributor Author

@Girgias rebased onto current master after the 8.6 fork and kept the UPGRADING note under 8.7. The 60 scanf tests pass on a fresh debug build; 8 tests need 32-bit, and CI is rerunning.

The internals thread still has no replies. Could you take another look when you have time?

Keep the %c conversion separate from %s so it reads whitespace instead of stopping at it. The %c conversion already suppresses leading whitespace skipping; it also must not treat whitespace as the end of the field.
@prateekbhujel
prateekbhujel force-pushed the prateekbhujel/fix-gh-19088-sscanf-c-whitespace branch from 3b03fbd to 1a18fc8 Compare November 23, 2026 17:10
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.

sscanf c conversion specifier

5 participants