]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/net: mptcp: fix uninitialized variable warnings
authorWillem de Bruijn <willemb@google.com>
Fri, 24 Nov 2023 17:15:22 +0000 (12:15 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 8 Dec 2023 07:51:17 +0000 (08:51 +0100)
[ Upstream commit 00a4f8fd9c750f20d8fd4535c71c9caa7ef5ff2f ]

Same init_rng() in both tests. The function reads /dev/urandom to
initialize srand(). In case of failure, it falls back onto the
entropy in the uninitialized variable. Not sure if this is on purpose.
But failure reading urandom should be rare, so just fail hard. While
at it, convert to getrandom(). Which man 4 random suggests is simpler
and more robust.

    mptcp_inq.c:525:6:
    mptcp_connect.c:1131:6:

    error: variable 'foo' is used uninitialized
    whenever 'if' condition is false
    [-Werror,-Wsometimes-uninitialized]

Fixes: 048d19d444be ("mptcp: add basic kselftest for mptcp")
Fixes: b51880568f20 ("selftests: mptcp: add inq test case")
Cc: Florian Westphal <fw@strlen.de>
Signed-off-by: Willem de Bruijn <willemb@google.com>
----

When input is randomized because this is expected to meaningfully
explore edge cases, should we also add
1. logging the random seed to stdout and
2. adding a command line argument to replay from a specific seed
I can do this in net-next, if authors find it useful in this case.
Reviewed-by: Matthieu Baerts <matttbe@kernel.org>
Link: https://lore.kernel.org/r/20231124171645.1011043-5-willemdebruijn.kernel@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/testing/selftests/net/mptcp/mptcp_connect.c
tools/testing/selftests/net/mptcp/mptcp_inq.c

index 7df6b9b6f9a847393a52f24bdabb166b1da20b6b..e6b514cb7bdda9a2c7775f16f187dc8a736530aa 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <sys/ioctl.h>
 #include <sys/poll.h>
+#include <sys/random.h>
 #include <sys/sendfile.h>
 #include <sys/stat.h>
 #include <sys/socket.h>
@@ -1050,15 +1051,11 @@ again:
 
 static void init_rng(void)
 {
-       int fd = open("/dev/urandom", O_RDONLY);
        unsigned int foo;
 
-       if (fd > 0) {
-               int ret = read(fd, &foo, sizeof(foo));
-
-               if (ret < 0)
-                       srand(fd + foo);
-               close(fd);
+       if (getrandom(&foo, sizeof(foo), 0) == -1) {
+               perror("getrandom");
+               exit(1);
        }
 
        srand(foo);
index 8672d898f8cdad5dd146460089ab03ed376b41d7..218aac46732125c005212d0d561ae3bce8f91a74 100644 (file)
@@ -18,6 +18,7 @@
 #include <time.h>
 
 #include <sys/ioctl.h>
+#include <sys/random.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -519,15 +520,11 @@ static int client(int unixfd)
 
 static void init_rng(void)
 {
-       int fd = open("/dev/urandom", O_RDONLY);
        unsigned int foo;
 
-       if (fd > 0) {
-               int ret = read(fd, &foo, sizeof(foo));
-
-               if (ret < 0)
-                       srand(fd + foo);
-               close(fd);
+       if (getrandom(&foo, sizeof(foo), 0) == -1) {
+               perror("getrandom");
+               exit(1);
        }
 
        srand(foo);