Skip to content

Commit 49e5ff3

Browse files
authored
patched overflow in setBit (#623)
Since `bitValue` is an `int`, the expression `bitValue << bitIndex` can overflow when `bitIndex` equals/exceeds 32. This is solved by casting `bitValue` into a `qindex` before shifting. (Tyson updated authorlists)
1 parent c474226 commit 49e5ff3

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

AUTHORS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Dr Ian Bush [consultant]
4444
HPC
4545

4646
External contributors:
47+
James Richings
48+
patched overflow in bitwise.hpp logic
4749
Luc Jaulmes
4850
patched v4's install process using CMake
4951
Jakub Adamski

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ See the [docs](docs/README.md) for enabling acceleration and running the unit te
257257

258258
In addition to QuEST's [authors](AUTHORS.txt), we sincerely thank the following external contributors to QuEST.
259259

260+
- [James Richings](https://github.com/JPRichings) for patching a v4 overflow bug.
260261
- [Luc Jaulmes](https://github.com/lucjaulmes) for patching v4's CMake installation.
261262
- [Jakub Adamski](https://github.com/jjacobx) for optimising distributed communication of max-size messages.
262263
- [Bruno Villasenor Alvarez](https://github.com/bvillasen) of [AMD](https://www.amd.com/en.html) for porting the v3 GPU backend to [HIP](https://github.com/ROCm-Developer-Tools/HIP), for compatibility with AMD GPUs.

quest/src/core/bitwise.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* @author Tyson Jones
66
* @author Erich Essmann (improved OS agnosticism)
7+
* @author James Richings (patched setBit)
78
*/
89

910
#ifndef BITWISE_HPP
@@ -106,7 +107,8 @@ INLINE qindex insertBit(qindex number, int bitIndex, int bitValue) {
106107

107108
INLINE qindex setBit(qindex number, int bitIndex, int bitValue) {
108109

109-
qindex bitInPlace = bitValue << bitIndex;
110+
// beware that shifting the raw int would overflow (#623)
111+
qindex bitInPlace = ((qindex) bitValue) << bitIndex;
110112
qindex oneInPlace = QINDEX_ONE << bitIndex;
111113
return (number & ~oneInPlace) | bitInPlace;
112114
}

0 commit comments

Comments
 (0)