From: Zhou Yuhang <zhouyuh...@kylinos.cn>

On x86_64, the size of struct flock is 32 bytes,
and the layout of this structure may be as follows:

 +------------+  offset 0
 | l_type     |  2 bytes
 +------------+  offset 2
 | l_whence   |  2 bytes
 +------------+  offset 4
 | padding    |  4 bytes
 +------------+  offset 8
 | l_start    |  8 bytes
 +------------+  offset 16
 | l_len      |  8 bytes
 +------------+  offset 24
 | l_pid      |  4 bytes
 +------------+  offset 28
 | padding    |  4 bytes
 +------------+  offset 32

Flock fl and fl2 are not initialized after definition.
The padding bytes in the structure may contain random values,
which could cause memcmp() to return a non-zero value,
potentially leading to test failure. The output is as follows:

 # [INFO] opened fds 3 4
 # [SUCCESS] set OFD read lock on first fd
 # [SUCCESS] read and write locks conflicted
 # [SUCCESS] F_UNLCK test returns: locked, type 0 pid -1 len 3
 # [FAIL] F_UNLCK test returns: locked, type 0 pid -1 len 3

Initialize them to zero to solve this problem.

Signed-off-by: Zhou Yuhang <zhouyuh...@kylinos.cn>
---
changes in v2:
        - Add a description of the struct flock layout to the commit message.
---
 tools/testing/selftests/filelock/ofdlocks.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/testing/selftests/filelock/ofdlocks.c 
b/tools/testing/selftests/filelock/ofdlocks.c
index a55b79810ab2..84e25505bebb 100644
--- a/tools/testing/selftests/filelock/ofdlocks.c
+++ b/tools/testing/selftests/filelock/ofdlocks.c
@@ -36,6 +36,8 @@ int main(void)
 {
        int rc;
        struct flock fl, fl2;
+       memset(&fl, 0, sizeof(fl));
+       memset(&fl2, 0, sizeof(fl2));
        int fd = open("/tmp/aa", O_RDWR | O_CREAT | O_EXCL, 0600);
        int fd2 = open("/tmp/aa", O_RDONLY);
 
-- 
2.33.0


Reply via email to