]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
libext2fs: fix UBSAN warning in ext2fs_mmp_new_seq()
authorTheodore Ts'o <tytso@mit.edu>
Fri, 22 Jan 2021 04:27:00 +0000 (23:27 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 22 Jan 2021 04:27:00 +0000 (23:27 -0500)
Left shifting the pid by 16 bits can cause a UBSAN warning if the pid
is greater than or equal to 2**16.  It doesn't matter since we're just
using the pid to seed for a pseudo-random number generator, but
silence the warning by just swapping the high and low 16 bits of the
pid instead.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/mmp.c

index e96a227327fcaa691d3aa9fe5d8eaef26bb8d999..223b617d93139de8245adcc694258a2c589969bf 100644 (file)
@@ -172,9 +172,11 @@ unsigned ext2fs_mmp_new_seq(void)
 #ifdef CONFIG_MMP
        unsigned new_seq;
        struct timeval tv;
+       unsigned long pid = getpid();
 
        gettimeofday(&tv, 0);
-       srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
+       pid = (pid >> 16) | ((pid & 0xFFFF) << 16);
+       srand(pid ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
 
        gettimeofday(&tv, 0);
        /* Crank the random number generator a few times */