mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: raffaele.tranfaglia@gmail.com
Cc: "Rodrigo Alencar" <rodrigo.alencar@analog.com>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/2] iio: test: iio-test-format: check that nothing is written past the string
Date: Tue, 3 Nov 2026 13:33:38 +0200	[thread overview]
Message-ID: <aunGkq5O9KAYKPmG@ashevche-desk.local> (raw)
In-Reply-To: <20261102-iio-test-format-char-v2-1-926100689e62@gmail.com>

On Mon, Nov 02, 2026 at 08:33:55AM +0000, Raffaele Tranfaglia via B4 Relay wrote:
> 
> 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.

> Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>

...

> +#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)


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)

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-11-03 11:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02  8:33 [PATCH v2 0/2] iio: test: iio-test-format: char format test and overrun checks Raffaele Tranfaglia via B4 Relay
2026-11-02  8:33 ` [PATCH v2 1/2] iio: test: iio-test-format: check that nothing is written past the string Raffaele Tranfaglia via B4 Relay
2026-11-03 11:33   ` Andy Shevchenko [this message]
2026-11-02  8:33 ` [PATCH v2 2/2] iio: test: iio-test-format: add test case for char format Raffaele Tranfaglia via B4 Relay

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 \
    --in-reply-to=aunGkq5O9KAYKPmG@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=raffaele.tranfaglia@gmail.com \
    --cc=rodrigo.alencar@analog.com \
    /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

all inboxes | Powered by JetHome®