iio: test: iio-test-format: char format test and overrun checks
v2 cover iio: test: iio-test-format: char format test and overrun checks
Add a KUnit test for IIO_VAL_CHAR formatting and, as suggested by Andy, make IIO_TEST_FORMAT_EXPECT_EQ() check that iio_format_value() does not write past the formatted string.
A NUL character is currently emitted as is, so the attribute reads back as "\0\n". The test documents this behaviour; please let me know if it should rather be rejected or handled differently.
These patches were written with the help of an LLM-based coding assistant, which I used to draft the code and commit messages. I reviewed and tested all changes myself.
Tested with KUnit on arm64 QEMU.
---
Changes in v2:
- Drop the Assisted-by tag and describe the tool use here (Andy)
- New patch 1: poison the buffer and check that nothing is written
past the string (Andy)
- Add special, non-printable, NUL and out-of-range cases (Andy)
- Link to v1: https://patch.msgid.link/[email protected]
---
Raffaele Tranfaglia (2):
iio: test: iio-test-format: check that nothing is written past the string
iio: test: iio-test-format: add test case for char format
drivers/iio/test/iio-test-format.c | 117 +++++++++++++++++++++++++++++++------ 1 file changed, 100 insertions(+), 17 deletions(-)
---
base-commit: d1425b382b9cc5bd19223fad8ba4a8af3dbe1305
change-id: 20261025-iio-test-format-char-8a17fbf9a201
Best regards,
Signature · 1 line
Raffaele Tranfaglia <[email protected]>
PATCH v2 1/2 iio: test: iio-test-format: check that nothing is written past the string
IIO_TEST_FORMAT_EXPECT_EQ() compares the buffer only up to the first NUL character. It does not notice if iio_format_value() writes past the end of the formatted string, and it cannot check output that contains a NUL.
Poison the buffer before each call. Check the expected bytes, including the NUL terminator, with KUNIT_EXPECT_MEMEQ(), and check that the rest of the page still holds the poison value. Take the expected length from the string literal instead of calling strlen() on the buffer.
--- drivers/iio/test/iio-test-format.c | 50 +++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 17 deletions(-)
drivers/iio/test/iio-test-format.c +33 -17
@@ -6,20 +6,42 @@#include <kunit/test.h> #include <linux/iio/iio.h> +#include <linux/string.h> -#define IIO_TEST_FORMAT_EXPECT_EQ (_test, _buf, _ret, _val) do { \ - KUNIT_EXPECT_EQ(_test, strlen(_buf), _ret); \ - KUNIT_EXPECT_STREQ(_test, (_buf), (_val)); \ +#define IIO_TEST_FORMAT_POISON 0x5a + +/* +* Check the formatted string including its NUL terminator, and that nothing +* beyond it was written. _val must be a string literal. The buffer is poisoned +* again afterwards for the next check. +*/ +#define IIO_TEST_FORMAT_EXPECT_EQ (_test, _buf, _ret, _val) do { \ + KUNIT_EXPECT_EQ(_test, _ret, (ssize_t)sizeof(_val) - 1); \ + KUNIT_EXPECT_MEMEQ(_test, _buf, _val, sizeof(_val)); \ + KUNIT_EXPECT_NULL(_test, memchr_inv((_buf) + sizeof(_val), \ + IIO_TEST_FORMAT_POISON, \ + PAGE_SIZE - sizeof(_val))); \ + memset(_buf, IIO_TEST_FORMAT_POISON, PAGE_SIZE); \ } while (0) +static char *iio_test_format_alloc_buf (struct kunit *test) +{ +char *buf; + + buf =kunit_kmalloc (test,PAGE_SIZE ,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); +memset (buf,IIO_TEST_FORMAT_POISON ,PAGE_SIZE ); + +return buf; +} +static void iio_test_iio_format_value_integer (struct kunit *test) {char *buf;int val;int ret; - buf =kunit_kmalloc (test,PAGE_SIZE ,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); + buf =iio_test_format_alloc_buf (test); val =42 ; ret =iio_format_value (buf,IIO_VAL_INT ,1 , &val);
@@ -48,8 +70,7 @@ static void iio_test_iio_format_value_fixedpoint(struct kunit *test)char *buf;int ret; - buf =kunit_kmalloc (test,PAGE_SIZE ,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); + buf =iio_test_format_alloc_buf (test);/* positive >= 1 */ values[0 ] =1 ;
@@ -109,8 +130,7 @@ static void iio_test_iio_format_value_fractional(struct kunit *test)char *buf;int ret; - buf =kunit_kmalloc (test,PAGE_SIZE ,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); + buf =iio_test_format_alloc_buf (test);/* positive < 1 */ values[0 ] =1 ;
@@ -149,8 +169,7 @@ static void iio_test_iio_format_value_fractional_log2(struct kunit *test)char *buf;int ret; - buf =kunit_kmalloc (test,PAGE_SIZE ,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); + buf =iio_test_format_alloc_buf (test);/* positive < 1 */ values[0 ] =123 ;
@@ -189,8 +208,7 @@ static void iio_test_iio_format_value_multiple(struct kunit *test)char *buf;int ret; - buf =kunit_kmalloc (test,PAGE_SIZE ,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); + buf =iio_test_format_alloc_buf (test); ret =iio_format_value (buf,IIO_VAL_INT_MULTIPLE ,ARRAY_SIZE (values), values);
@@ -203,8 +221,7 @@ static void iio_test_iio_format_value_integer_64(struct kunit *test)char *buf;int ret; - buf =kunit_kmalloc (test,PAGE_SIZE ,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); + buf =iio_test_format_alloc_buf (test);iio_val_s64_decompose (24 , &values[0 ], &values[1 ]); ret =iio_format_value (buf,IIO_VAL_INT_64 ,ARRAY_SIZE (values), values);
@@ -241,8 +258,7 @@ static void iio_test_iio_format_value_decimal_64(struct kunit *test)char *buf;int ret; - buf =kunit_kmalloc (test,PAGE_SIZE ,GFP_KERNEL ); -KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); + buf =iio_test_format_alloc_buf (test);/* DECIMAL64_MILLI: positive >= 1, value 1.234 */ iio_val_s64_decompose (1234 , &values[0 ], &values[1 ]);
Signature · 1 line
2.56.0
Re: [PATCH v2 1/2] iio: test: iio-test-format: check that nothing is written past the string
Quoted 11 lines · On Mon, Nov 02, 2026 at 08:33:55AM +0000, Raffaele Tranfaglia via B4 Relay…
IIO_TEST_FORMAT_EXPECT_EQ()compares the buffer only up to the first NUL character. It does not notice ifiio_format_value()writes past the end of the formatted string, and it cannot check output that contains a NUL.Poison the buffer before each call. Check the expected bytes, including the NUL terminator, with
KUNIT_EXPECT_MEMEQ(), and check that the rest of the page still holds the poison value. Take the expected length from the string literal instead of callingstrlen()on the buffer.Suggested-by Andy Shevchenko <[email protected]>
...
Quoted 5 lines
drivers/iio/test/iio-test-format.c +5
-0+#define IIO_TEST_FORMAT_POISON 0x5a + +/* +* Check the formatted string including its NUL terminator, and that nothing +* beyond it was written. _val must be a string literal. The buffer is poisoned
drivers/iio/test/iio-test-format.c line 25
Quoted 10 lines
drivers/iio/test/iio-test-format.c +9
-0+* again afterwards for the next check. +*/ +#define IIO_TEST_FORMAT_EXPECT_EQ (_test, _buf, _ret, _val) do { \ + KUNIT_EXPECT_EQ(_test, _ret, (ssize_t)sizeof(_val) - 1); \ + KUNIT_EXPECT_MEMEQ(_test, _buf, _val, sizeof(_val)); \ + KUNIT_EXPECT_NULL(_test, memchr_inv((_buf) + sizeof(_val), \ + IIO_TEST_FORMAT_POISON, \ + PAGE_SIZE - sizeof(_val))); \ + memset(_buf, IIO_TEST_FORMAT_POISON, PAGE_SIZE); \ } while (0)
Format it as
#define IIO_TEST_FORMAT_EXPECT_EQ(_test, _buf, _ret, _val) \
do { \
KUNIT_EXPECT_EQ(_test, _ret, (ssize_t)sizeof(_val) - 1); \
KUNIT_EXPECT_MEMEQ(_test, _buf, _val, sizeof(_val)); \
KUNIT_EXPECT_NULL(_test, \
memchr_inv((_buf) + sizeof(_val), \
IIO_TEST_FORMAT_POISON, \
PAGE_SIZE - sizeof(_val))); \
memset(_buf, IIO_TEST_FORMAT_POISON, PAGE_SIZE); \
} while (0)Alternatively
#define IIO_TEST_FORMAT_EXPECT_EQ(_test, _buf, _ret, _val) \
do { \
size_t _sz = sizeof(_val); \
\
KUNIT_EXPECT_EQ(_test, _ret, (ssize_t)sz - 1); \
KUNIT_EXPECT_MEMEQ(_test, _buf, _val, sz); \
KUNIT_EXPECT_NULL(_test, \
memchr_inv((_buf) + sz, IIO_TEST_FORMAT_POISON, PAGE_SIZE - sz)); \
memset(_buf, IIO_TEST_FORMAT_POISON, PAGE_SIZE); \
} while (0)Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 2/2 iio: test: iio-test-format: add test case for char format
In reply to Raffaele TranfagliaIIO_VAL_CHAR is used by thermocouple drivers to report the thermocouple type, but iio_format_value() has no test for it.
Add a test case covering printable, special and non-printable characters, a NUL character, and values that do not fit in a char.
--- drivers/iio/test/iio-test-format.c | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+)
drivers/iio/test/iio-test-format.c +67 -0
@@ -215,6 +215,72 @@ static void iio_test_iio_format_value_multiple(struct kunit *test)IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"1 -2 3 -4 5 \n" ); } +static void iio_test_iio_format_value_char (struct kunit *test) +{ +char *buf; +int val; +int ret; + + buf =iio_test_format_alloc_buf (test); + +/* Printable characters, e.g. a thermocouple type */ + val ='K' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"K\n" ); + + val ='j' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"j\n" ); + + val ='0' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"0\n" ); + +/* Special characters are printed as is */ + val =' ' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"\x20\n" ); + + val ='%' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"%\n" ); + + val ='\\' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"\\\n" ); + +/* Non-printable characters are printed as is */ + val ='\n' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"\n\n" ); + + val ='\t' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"\t\n" ); + + val =0x7f ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"\x7f\n" ); + + val =0xff ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"\xff\n" ); + +/* A NUL character is emitted and counted like any other */ + val ='\0' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"\0\n" ); + +/* Only the low byte of the value is used */ + val =0x100 +'K' ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"K\n" ); + + val =-1 ; + ret =iio_format_value (buf,IIO_VAL_CHAR ,1 , &val); +IIO_TEST_FORMAT_EXPECT_EQ (test, buf, ret,"\xff\n" ); +} +static void iio_test_iio_format_value_integer_64 (struct kunit *test) {int values[2 ];
@@ -324,6 +390,7 @@ static struct kunit_case iio_format_test_cases[] = {KUNIT_CASE (iio_test_iio_format_value_fractional),KUNIT_CASE (iio_test_iio_format_value_fractional_log2),KUNIT_CASE (iio_test_iio_format_value_multiple), +KUNIT_CASE (iio_test_iio_format_value_char),KUNIT_CASE (iio_test_iio_format_value_integer_64), KUNIT_CASE(iio_test_iio_format_value_decimal_64), { }
Signature · 1 line
2.56.0