All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Horman <horms@kernel.org>
To: 0x7f454c46@gmail.com
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	Mohammad Nassiri <mnassiri@ciena.com>,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH net-next v3 2/8] selftests/net: Provide test_snprintf() helper
Date: Wed, 21 Aug 2024 20:10:04 +0100	[thread overview]
Message-ID: <20240821191004.GF2164@kernel.org> (raw)
In-Reply-To: <20240815-tcp-ao-selftests-upd-6-12-v3-2-7bd2e22bb81c@gmail.com>

On Thu, Aug 15, 2024 at 10:32:27PM +0100, Dmitry Safonov via B4 Relay wrote:
> From: Dmitry Safonov <0x7f454c46@gmail.com>
> 
> Instead of pre-allocating a fixed-sized buffer of TEST_MSG_BUFFER_SIZE
> and printing into it, call vsnprintf() with str = NULL, which will
> return the needed size of the buffer. This hack is documented in
> man 3 vsnprintf.
> 
> Essentially, in C++ terms, it re-invents std::stringstream, which is
> going to be used to print different tracing paths and formatted strings.
> Use it straight away in __test_print() - which is thread-safe version of
> printing in selftests.
> 
> Signed-off-by: Dmitry Safonov <0x7f454c46@gmail.com>

Hi Dmitry,

Some minor nits, as it looks like there will be a v4.

> ---
>  tools/testing/selftests/net/tcp_ao/lib/aolib.h | 59 ++++++++++++++++++++++----
>  1 file changed, 50 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/testing/selftests/net/tcp_ao/lib/aolib.h b/tools/testing/selftests/net/tcp_ao/lib/aolib.h
> index fbc7f6111815..60a63419cabb 100644
> --- a/tools/testing/selftests/net/tcp_ao/lib/aolib.h
> +++ b/tools/testing/selftests/net/tcp_ao/lib/aolib.h
> @@ -37,17 +37,58 @@ extern void __test_xfail(const char *buf);
>  extern void __test_error(const char *buf);
>  extern void __test_skip(const char *buf);
>  
> -__attribute__((__format__(__printf__, 2, 3)))
> -static inline void __test_print(void (*fn)(const char *), const char *fmt, ...)
> +static inline char *test_snprintf(const char *fmt, va_list vargs)
>  {
> -#define TEST_MSG_BUFFER_SIZE 4096
> -	char buf[TEST_MSG_BUFFER_SIZE];
> -	va_list arg;
> +	char *ret = NULL;
> +	size_t size = 0;
> +	va_list tmp;
> +	int n = 0;
>  
> -	va_start(arg, fmt);
> -	vsnprintf(buf, sizeof(buf), fmt, arg);
> -	va_end(arg);
> -	fn(buf);
> +	va_copy(tmp, vargs);
> +	n = vsnprintf(ret, size, fmt, tmp);
> +	if (n < 0)
> +		return NULL;
> +
> +	size = (size_t) n + 1;

nit: I'm not sure this cast is needed.
     But if it is, then the space after ')' should be dropped.

> +	ret = malloc(size);
> +	if (ret == NULL)

nit: if (!ret)

> +		return NULL;
> +
> +	n = vsnprintf(ret, size, fmt, vargs);
> +	if (n < 0 || n > size - 1) {
> +		free(ret);
> +		return NULL;
> +	}
> +	return ret;
> +}

...

  reply	other threads:[~2024-08-21 19:10 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-15 21:32 [PATCH net-next v3 0/8] net/selftests: TCP-AO selftests updates Dmitry Safonov
2024-08-15 21:32 ` Dmitry Safonov via B4 Relay
2024-08-15 21:32 ` [PATCH net-next v3 1/8] selftests/net: Clean-up double assignment Dmitry Safonov
2024-08-15 21:32   ` Dmitry Safonov via B4 Relay
2024-08-15 21:32 ` [PATCH net-next v3 2/8] selftests/net: Provide test_snprintf() helper Dmitry Safonov
2024-08-15 21:32   ` Dmitry Safonov via B4 Relay
2024-08-21 19:10   ` Simon Horman [this message]
2024-08-21 21:35     ` Dmitry Safonov
2024-08-22 10:13       ` Simon Horman
2024-08-23 14:27         ` Dmitry Safonov
2024-08-15 21:32 ` [PATCH net-next v3 3/8] selftests/net: Be consistent in kconfig checks Dmitry Safonov
2024-08-15 21:32   ` Dmitry Safonov via B4 Relay
2024-08-15 21:32 ` [PATCH net-next v3 4/8] selftests/net: Open /proc/thread-self in open_netns() Dmitry Safonov
2024-08-15 21:32   ` Dmitry Safonov via B4 Relay
2024-08-21 19:11   ` Simon Horman
2024-08-21 21:44     ` Dmitry Safonov
2024-08-22 10:14       ` Simon Horman
2024-08-15 21:32 ` [PATCH net-next v3 5/8] selftests/net: Don't forget to close nsfd after switch_save_ns() Dmitry Safonov
2024-08-15 21:32   ` Dmitry Safonov via B4 Relay
2024-08-15 21:32 ` [PATCH net-next v3 6/8] selftests/tcp_ao: Fix printing format for uint64_t Dmitry Safonov
2024-08-15 21:32   ` Dmitry Safonov via B4 Relay
2024-08-15 21:32 ` [PATCH net-next v3 7/8] selftests/net: Synchronize client/server before counters checks Dmitry Safonov
2024-08-15 21:32   ` Dmitry Safonov via B4 Relay
2024-08-15 21:32 ` [PATCH net-next v3 8/8] selftests/net: Add trace events matching to tcp_ao Dmitry Safonov
2024-08-15 21:32   ` Dmitry Safonov via B4 Relay
2024-08-19 17:19 ` [PATCH net-next v3 0/8] net/selftests: TCP-AO selftests updates Dmitry Safonov

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=20240821191004.GF2164@kernel.org \
    --to=horms@kernel.org \
    --cc=0x7f454c46@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mnassiri@ciena.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.