Skip to content

ext/standard: no floor/ceil in uuencode - #24604

Open
Sjord wants to merge 2 commits into
php:masterfrom
Sjord:uuencode-no-math
Open

Sjord wants to merge 2 commits into
php:masterfrom
Sjord:uuencode-no-math

Conversation

@Sjord

@Sjord Sjord commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Just use integer division instead of floor/ceil to determine buffer sizes. Add more tests.

Just use integer division instead of floor/ceil to determine buffer
sizes. Add more tests.
@Sjord

Sjord commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@iliaal Could you take a look at this? I am not sure it is much of an improvement to remove floor/ceil. What do you think?

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's an improvement: floor(len * 1.33) only works because 1.33 is close enough to 4/3 for the line lengths uuencode allows, while ((len + 2) / 3) * 4 is the exact encoded length. I diffed old and new decoders on random input and on truncated or mutated encodings and got identical results.

One problem: src_len * 3 can overflow on 32-bit, where the double version could not.

Comment thread ext/standard/uuencode.c Outdated
Co-authored-by: Ilia Alshanetsky <ilia@ilia.ws>
@Sjord
Sjord marked this pull request as ready for review October 24, 2026 18:54
@Sjord
Sjord requested a review from bukka as a code owner October 24, 2026 18:54
@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

I fuzzed base against head, round-trip and malformed input, byte-identical on gcc and clang. That includes the inputs where floor(len*1.33) and ((len+2)/3)*4 disagree. The decode loop bounds mask the difference, so output stays the same. ASan is clean, including a 5 MB round-trip.

You asked whether it's much of an improvement. On speed, no: decode instructions are flat, gcc 13 encode is about 4% more, and cycles are lost in the noise at this size. The real gain is the exact formula, which drops the len==45 special case and the double-precision risk in the decode allocation for huge inputs.

LGTM.

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