Skip to content

doh: fix undefined behaviour in swap#4350

Closed
pauldreik wants to merge 1 commit intocurl:masterfrom
pauldreik:paul/defined_bswap
Closed

doh: fix undefined behaviour in swap#4350
pauldreik wants to merge 1 commit intocurl:masterfrom
pauldreik:paul/defined_bswap

Conversation

@pauldreik
Copy link
Contributor

The undefined behaviour is annoying when running fuzzing with
sanitizers. The codegen is the same, but the meaning is now
not up for dispute. See https://cppinsights.io/s/516a2ff4

By incrementing the pointer first, both gcc and clang recognize this
as a bswap and optimizes it to a single instruction.
See https://godbolt.org/z/994Zpx

The undefined behaviour is annoying when running fuzzing with
sanitizers. The codegen is the same, but the meaning is now
not up for dispute. See https://cppinsights.io/s/516a2ff4

By incrementing the pointer first, both gcc and clang recognize this
as a bswap and optimizes it to a single instruction.
See https://godbolt.org/z/994Zpx
Copy link
Member

@bagder bagder left a comment

Choose a reason for hiding this comment

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

There are certainly several ways to fix the undefined behavior (even if most compilers seem to be fine with it), but if this has the benefit that it generates better code I'm all for it!

@pauldreik pauldreik changed the title adc: fix undefined behaviour in swap doh: fix undefined behaviour in swap Sep 14, 2019
@bagder
Copy link
Member

bagder commented Sep 15, 2019

Thanks!

@bagder bagder closed this in dda4182 Sep 15, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Dec 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants