]> Sergey Matveev's repositories - nnn.git/commitdiff
icons-hash: fix bitwise rotation
authorNRK <nrk@disroot.org>
Mon, 2 Jan 2023 20:20:21 +0000 (02:20 +0600)
committerNRK <nrk@disroot.org>
Mon, 2 Jan 2023 20:20:21 +0000 (02:20 +0600)
in case the rotation is 0, `v >> (32 - r)` would end up doing a 32 right
shift which is equal to the width of `v` and thus undefined behavior.

ref: https://blog.regehr.org/archives/1063

src/icons-hash.c

index 1b10ae4734ea7684a592558e9decf475431ad508..da6403534e6d4832ac293bfb517915f2836ed065 100644 (file)
@@ -109,7 +109,7 @@ pcg(uint64_t *state)
        *state *= GOLDEN_RATIO_64;
        uint32_t r = (oldstate >> 59);
        uint32_t v = (oldstate ^ (oldstate >> 18)) >> 27;
-       return (v >> (32 - r)) | (v << r);
+       return (v >> (-r & 31)) | (v << r);
 }
 
 int