-
Notifications
You must be signed in to change notification settings - Fork 373
All: Fix left-shifts of signed vs unsigned constants #2801
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
All: Fix left-shifts of signed vs unsigned constants #2801
Conversation
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.
see comment.
selection[1] |= (1 << (pcr_index[i] - 8)) % 256; | ||
selection[2] |= (1 << (pcr_index[i] - 16)) % 256; | ||
selection[3] |= (1 << (pcr_index[i] - 24)) % 256; | ||
selection[0] |= (((UINT32)1) << pcr_index[i]) % 256; |
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.
Should not be used here UINT8?
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.
No, because we want to shift for up to 24 bits to the left. So we need 32bit.
Then we cast it down to BYTE using modulo.
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.
@AndreasFuchsTPM yes that's true. There are still some cases in the test files and one in mpsse.c.
grep -R "1 << [^0-9]" .
Should they also be changed?
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.
The mpsse.c file should be ok for the time being. In ReadBits() and WriteBits(), the code restricts the max left shift to 8 bits. Regarding PinState(), the max left shift is up to 12+4 bits (currently only 12 GPIOs are defined by libmpsse: https://github.com/devttys0/libmpsse/blob/master/docs/README.GPIO).
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 checked the remaining shifts in the test files (displayed by grep). They also should be ok.
1e90318
to
6c3128e
Compare
Reference: https://github.com/AGHABEY/Books/blob/master/c-in-a-nutshell-o-reilly-peter-prinz-tony-crawford.pdf I believe the error flagged by "-fsanitize=undefined" is due to the integer promotion operation. Considering the integer promotion rules, operations like
Type ranks:
The correct implementation should be: You can experiment with:
|
In order to silence -fsanitize=undefined all left shifts of constants are now cast before shifting, in order to avoid undefined behavior, if the target variable is unsigned. Signed-off-by: Andreas Fuchs <andreas.fuchs@infineon.com>
6c3128e
to
1763649
Compare
In order to silence -fsanitize=undefined all left shifts of constants are now cast before shifting, in order to avoid undefined behavior, if the target variable is unsigned.
Fixes: #2799