diff options
author | Daniel Wyatt <[email protected]> | 2017-06-28 19:50:46 -0400 |
---|---|---|
committer | Daniel Wyatt <[email protected]> | 2017-06-28 20:12:50 -0400 |
commit | 2b37c13dcf9f8460bf86c4b9096dcc49aed72a44 (patch) | |
tree | a659fc187146c4b10559ec77a1f632ce0e76612e /src/lib/utils | |
parent | 62c94693cb1cf5ddc5e8e43a787561e7d8351258 (diff) |
Allow bit rotation by more than sizeof(T)*8 bits.
Currently these functions will happily bit shift by >= sizeof(T)*8 bits.
However, this is undefined behavior, and results in unexpected results (0)
on at least one platform I've tested.
With this update, you can expect that rotate_left<uint32_t>(1, 32)==1
and rotate_right<uint32_t>(1, 32)==1.
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/rotate.h | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/src/lib/utils/rotate.h b/src/lib/utils/rotate.h index b936521e6..8361e4705 100644 --- a/src/lib/utils/rotate.h +++ b/src/lib/utils/rotate.h @@ -20,6 +20,7 @@ namespace Botan { */ template<typename T> inline T rotate_left(T input, size_t rot) { + rot %= 8 * sizeof(T); return (rot == 0) ? input : static_cast<T>((input << rot) | (input >> (8*sizeof(T)-rot)));; } @@ -31,6 +32,7 @@ template<typename T> inline T rotate_left(T input, size_t rot) */ template<typename T> inline T rotate_right(T input, size_t rot) { + rot %= 8 * sizeof(T); return (rot == 0) ? input : static_cast<T>((input >> rot) | (input << (8*sizeof(T)-rot))); } |