Skip to content
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 padding of compound types #581

Merged
merged 7 commits into from
May 25, 2022
Merged

Fix padding of compound types #581

merged 7 commits into from
May 25, 2022

Conversation

alkino
Copy link
Member

@alkino alkino commented May 21, 2022

It seems that it can fix everything and staying > 0 in the same time.

Comment on lines 273 to 274
(((member_size) >= (current_size)) ? (((member_size) - (current_size)) % (member_size)) \
: ((((member_size) - (((current_size) - (member_size)) % (member_size)))) % (member_size)))
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know if I'm able to explain it 🤪

@codecov-commenter
Copy link

codecov-commenter commented May 21, 2022

Codecov Report

❗ No coverage uploaded for pull request base (master@c594647). Click here to learn what that means.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master     #581   +/-   ##
=========================================
  Coverage          ?   80.38%           
=========================================
  Files             ?       66           
  Lines             ?     3539           
  Branches          ?        0           
=========================================
  Hits              ?     2845           
  Misses            ?      694           
  Partials          ?        0           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c594647...47bb978. Read the comment docs.

(((member_size) >= (current_size)) \
? (((member_size) - (current_size)) % (member_size)) \
: ((((member_size) - (((current_size) - (member_size)) % (member_size)))) % \
(member_size)))
Copy link
Member

Choose a reason for hiding this comment

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

My brain can't handle these parenthesis! Worse than Lisp… do we really need all the moduli?

Copy link
Member Author

@alkino alkino May 24, 2022

Choose a reason for hiding this comment

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

In fact, it seems that yes.
What we want is:
(member_size-current_size) % member_size
if member_size is smaller than current-size it does not matter we will got (in math) a positive modulo.
But in C++ it gives a negative one (more over we use size_t).

So we compute the opposite modulo:
m = (current_size-member_size) % member_size
After we only need:
member_size - m
But if m == 0, the result should be 0 and not member_size so, a new modulo.

Example:
member_size = 8
current_size = 16
(current_size - member_size) % member_size = 0
member_size - 0 = 8
8 % 8 = 0

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this would benefit from a clear description of what it's trying to achieve. Examples of question are:

  • align to which boundary?
  • what is current_size what is member_size (which one corresponds to the 'element` in the comment)?

Then, since none of us seem particularly confident about our integer arithmetic, it might be good to check all of this in unit-tests, covering all corner cases.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for the rundown, @alkino 🙏

matz-e
matz-e previously approved these changes May 24, 2022
Copy link
Member

@matz-e matz-e left a comment

Choose a reason for hiding this comment

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

LGTM

(((member_size) >= (current_size)) \
? (((member_size) - (current_size)) % (member_size)) \
: ((((member_size) - (((current_size) - (member_size)) % (member_size)))) % \
(member_size)))
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for the rundown, @alkino 🙏

@alkino alkino merged commit de3bcc4 into master May 25, 2022
@alkino alkino deleted the fix_padding branch May 25, 2022 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compound types with fixed-lengths strings of certain size cause exception
4 participants