public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH liburing xattr-getdents64] test/xattr: Fix random failure due to undefined behavior
@ 2021-12-29 23:23 Ammar Faizi
  0 siblings, 0 replies; only message in thread
From: Ammar Faizi @ 2021-12-29 23:23 UTC (permalink / raw)
  To: Jens Axboe; +Cc: io-uring Mailing List, Ammar Faizi, Stefan Roesch

Running `test/xattr` sometimes fails. When it fails, there are strange
characters at the end of the returned value:
```
  $ for i in {1..10}; do ./xattr; done;
  Error: fgetxattr expected value: value2-a-lot-longer, returned value: value2-a-lot-longer��
  Error: fgetxattr expected value: value2-a-lot-longer, returned value: value2-a-lot-longerg�
  Error: fgetxattr expected value: value2-a-lot-longer, returned value: value2-a-lot-longeri�
  Error: fgetxattr expected value: value2-a-lot-longer, returned value: value2-a-lot-longerp�
  Error: fgetxattr expected value: value2-a-lot-longer, returned value: value2-a-lot-longer��
  Error: fgetxattr expected value: value2-a-lot-longer, returned value: value2-a-lot-longer"�
  Error: fgetxattr expected value: value2-a-lot-longer, returned value: value2-a-lot-longer��
  Error: fgetxattr expected value: value2-a-lot-longer, returned value: value2-a-lot-longerQ�
```

Debugged it with valgrind, valgrind says:
```
  ==59962== Conditional jump or move depends on uninitialised value(s)
  ==59962==    at 0x4849C59: strlen (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==59962==    by 0x1099E9: test_fxattr (xattr.c:213)
  ==59962==    by 0x109391: main (xattr.c:416)
```

It turned out that `char value[XATTR_SIZE]` may not be NUL
terminated, so the `strlen()` results in the wrong value and %s format
to the "returned value" shows strange characters.

Fix this by changing it to `char value[XATTR_SIZE + 1] = { }` so that
it is guaranteed to be NUL terminated.

Cc: Stefan Roesch <[email protected]>
Fixes: e194e24ff8721e67d8526737aa644b313bff3148 ("liburing: Add new test program to verify xattr support")
Signed-off-by: Ammar Faizi <[email protected]>
---
 test/xattr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/test/xattr.c b/test/xattr.c
index d88059c..af7df5f 100644
--- a/test/xattr.c
+++ b/test/xattr.c
@@ -175,7 +175,7 @@ static int test_fxattr(void)
 	int rc = 0;
 	size_t value_len;
 	struct io_uring ring;
-	char value[XATTR_SIZE];
+	char value[XATTR_SIZE + 1] = { };

 	/* Init io-uring queue. */
 	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
@@ -239,7 +239,7 @@ static int test_xattr(void)
 	int rc = 0;
 	int value_len;
 	struct io_uring ring;
-	char value[XATTR_SIZE];
+	char value[XATTR_SIZE + 1] = { };

 	/* Init io-uring queue. */
 	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
@@ -292,7 +292,7 @@ static int test_failure_fxattr(void)
 {
 	int rc = 0;
 	struct io_uring ring;
-	char value[XATTR_SIZE];
+	char value[XATTR_SIZE + 1] = { };

 	/* Init io-uring queue. */
 	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
@@ -335,7 +335,7 @@ static int test_failure_xattr(void)
 {
 	int rc = 0;
 	struct io_uring ring;
-	char value[XATTR_SIZE];
+	char value[XATTR_SIZE + 1] = { };

 	/* Init io-uring queue. */
 	int ret = io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
--
2.32.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-12-29 23:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-12-29 23:23 [PATCH liburing xattr-getdents64] test/xattr: Fix random failure due to undefined behavior Ammar Faizi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox