-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix BigInt#to_s
emitting null bytes for certain values
#11063
Fix BigInt#to_s
emitting null bytes for certain values
#11063
Conversation
I'm wondering if we can't just combine both branches instead of duplicating a lot of very similar code. |
They look very different to me and I think merging the two branches would make the logic even harder to follow. |
The diff really isn't that big: https://github.com/HertzDevil/crystal/compare/bug/bigint-to_s...straight-shoota:bug/bigint-to_s?w=1 |
And now the logic of whether to reserve space for zero padding and rewrite the minus sign is hidden behind those extra clamping operations, when the intent should have been very clear: more digits ( |
The offset calculation via max and clamp can be simplified to a single conditional. I've added a commit to fix that. That should better express the intent. The combined implementation adds three conditionals and a couple of simple expressions, while also simplifying some existing expressions. For me, that results in a net reduction of cognitive load because I don't have to grok two different, but very similar branches while trying to keep track of their minimal differences. |
That's because these aren't "minimal" differences; the two branches are dissimilar enough that they deserve different considerations, and if any portions between them do look alike, that doesn't imply it is desirable to factor out the changing parts rather than the common parts. That cognitive load does not come from studying the code's correctness. I would especially like not to over-factor code like that when it comes to code that deals with C or unsafe memory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with merging this either way, as it fixes a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
mpz_sizeinbase
does not always return an exact number of digits when the base argument is not a power of 2:This means the last byte of the returned string can be a null byte if
mpz_sizeinbase
is greater than the exact count:This PR fixes that. Follow-up to #10926.
Originally discovered in https://github.com/crystal-money/money/runs/3245218564