public inbox for [email protected]
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: Jens Axboe <[email protected]>
Cc: io-uring Mailing List <[email protected]>,
	Ammar Faizi <[email protected]>, Stefan Roesch <[email protected]>
Subject: [PATCH liburing xattr-getdents64] test/xattr: Fix random failure due to undefined behavior
Date: Thu, 30 Dec 2021 06:23:10 +0700	[thread overview]
Message-ID: <[email protected]> (raw)

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


                 reply	other threads:[~2021-12-29 23:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox