Skip to content

Commit 00a4f8f

Browse files
wdebruijkuba-moo
authored andcommitted
selftests/net: mptcp: fix uninitialized variable warnings
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: 048d19d ("mptcp: add basic kselftest for mptcp") Fixes: b518805 ("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>
1 parent 59fef37 commit 00a4f8f

2 files changed

Lines changed: 8 additions & 14 deletions

File tree

tools/testing/selftests/net/mptcp/mptcp_connect.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <sys/ioctl.h>
2020
#include <sys/poll.h>
21+
#include <sys/random.h>
2122
#include <sys/sendfile.h>
2223
#include <sys/stat.h>
2324
#include <sys/socket.h>
@@ -1125,15 +1126,11 @@ int main_loop_s(int listensock)
11251126

11261127
static void init_rng(void)
11271128
{
1128-
int fd = open("/dev/urandom", O_RDONLY);
11291129
unsigned int foo;
11301130

1131-
if (fd > 0) {
1132-
int ret = read(fd, &foo, sizeof(foo));
1133-
1134-
if (ret < 0)
1135-
srand(fd + foo);
1136-
close(fd);
1131+
if (getrandom(&foo, sizeof(foo), 0) == -1) {
1132+
perror("getrandom");
1133+
exit(1);
11371134
}
11381135

11391136
srand(foo);

tools/testing/selftests/net/mptcp/mptcp_inq.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <time.h>
1919

2020
#include <sys/ioctl.h>
21+
#include <sys/random.h>
2122
#include <sys/socket.h>
2223
#include <sys/types.h>
2324
#include <sys/wait.h>
@@ -519,15 +520,11 @@ static int client(int unixfd)
519520

520521
static void init_rng(void)
521522
{
522-
int fd = open("/dev/urandom", O_RDONLY);
523523
unsigned int foo;
524524

525-
if (fd > 0) {
526-
int ret = read(fd, &foo, sizeof(foo));
527-
528-
if (ret < 0)
529-
srand(fd + foo);
530-
close(fd);
525+
if (getrandom(&foo, sizeof(foo), 0) == -1) {
526+
perror("getrandom");
527+
exit(1);
531528
}
532529

533530
srand(foo);

0 commit comments

Comments
 (0)