DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Morten Brørup" <mb@smartsharesystems.com>
To: "lihuisong (C)" <lihuisong@huawei.com>,
	"Bruce Richardson" <bruce.richardson@intel.com>
Cc: <dev@dpdk.org>, <andrew.rybchenko@oktetlabs.ru>,
	<huangdaode@huawei.com>, <liudongdong3@huawei.com>,
	<fengchengwen@huawei.com>
Subject: RE: [PATCH V6 6/8] telemetry: support adding integer value as hexadecimal
Date: Thu, 15 Dec 2022 13:52:02 +0100	[thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35D875AF@smartserver.smartshare.dk> (raw)
In-Reply-To: <f60158da-cbed-6581-8a3c-9f6376a37d44@huawei.com>

> From: lihuisong (C) [mailto:lihuisong@huawei.com]
> Sent: Thursday, 15 December 2022 13.46
> 
> 在 2022/12/15 20:24, Morten Brørup 写道:
> >> From: Bruce Richardson [mailto:bruce.richardson@intel.com]
> >> Sent: Thursday, 15 December 2022 13.16
> >>
> >> On Thu, Dec 15, 2022 at 01:00:40PM +0100, Morten Brørup wrote:
> >>>> From: lihuisong (C) [mailto:lihuisong@huawei.com]
> >>>> Sent: Thursday, 15 December 2022 12.28
> >>>>
> >>>> 在 2022/12/15 18:46, Bruce Richardson 写道:
> >>>>> On Thu, Dec 15, 2022 at 06:31:44PM +0800, Huisong Li wrote:
> >>>>>> Sometimes displaying a unsigned integer value as hexadecimal
> >> encoded
> >>>> style
> >>>>>> is more expected for human consumption, such as, offload
> >> capability
> >>>> and
> >>>>>> device flag. This patch introduces two APIs to add unsigned
> >> integer
> >>>> value
> >>>>>> as hexadecimal encoded string to array or dictionary. And user
> >> can
> >>>> choose
> >>>>>> whether the stored value is padding to the specified width.
> >>>>>>
> >>>>>> Signed-off-by: Huisong Li <lihuisong@huawei.com>
> >>>>>> Acked-by: Morten Brørup <mb@smartsharesystems.com>
> >>>>>> Acked-by: Chengwen Feng <fengchengwen@huawei.com>
> >>>>>> ---
> >>>>>>    lib/telemetry/rte_telemetry.h  | 47 ++++++++++++++++++++++
> >>>>>>    lib/telemetry/telemetry_data.c | 72
> >>>> ++++++++++++++++++++++++++++++++++
> >>>>>>    lib/telemetry/version.map      |  9 +++++
> >>>>>>    3 files changed, 128 insertions(+)
> >>>>>>
> >>>>> <snip>
> >>>>>> +/* To suppress compiler warning about format string. */
> >>>>>> +#if defined(RTE_TOOLCHAIN_GCC)
> >>>>>> +#pragma GCC diagnostic push
> >>>>>> +#pragma GCC diagnostic ignored "-Wformat-nonliteral"
> >>>>>> +#elif defined(RTE_TOOLCHAIN_CLANG)
> >>>>>> +#pragma clang diagnostic push
> >>>>>> +#pragma clang diagnostic ignored "-Wformat-nonliteral"
> >>>>>> +#endif
> >>>>>> +
> >>>>>> +static int
> >>>>>> +rte_tel_uint_to_hex_encoded_str(char *buf, uint16_t len,
> >> uint64_t
> >>>> val,
> >>> The "len" parameter should be size_t or unsigned int, not uint16_t.
> >>>
> >>> And as a personal preference, I would name it "buf_len" instead of
> >> "len".
> >>>>>> +				uint8_t display_bitwidth)
> >>>>>> +{
> >>>>>> +#define RTE_TEL_UINT_HEX_FORMAT_LEN 16
> >>>>>> +
> >>>>>> +	uint8_t spec_hex_width = (display_bitwidth + 3) / 4;
> >>>>>> +	char format[RTE_TEL_UINT_HEX_FORMAT_LEN];
> >>>>>> +
> >>>>>> +	/* Buffer needs to have room to contain the prefix '0x' and
> >> '\0'.
> >>>> */
> >>>>>> +	if (len < spec_hex_width + 3)
> >>>>>> +		return -EINVAL;
> >>>>>> +
> >>>>> This check is not sufficient, as display_bitwidth could be 0 for
> >>>> example,
> >>>>> and the actual printed number have up to 16 characters.
> >>>> Yes, you are right. What do you think of the following check?
> >>>>
> >>>> max_hex_width = display_bitwidth == 0 ? (sizeof(uint64_t) * 2) :
> >>>> spec_hex_width;
> >>>> if (len < max_hex_width + 3)
> >>>>       return -EINVAL;
> >>> LGTM.
> >> That would work, but actually I think we should drop this check
> >> completely
> >> - see comment below.
> >>
> >>>>>> +	if (display_bitwidth != 0) {
> >>>>>> +		sprintf(format, "0x%%0%u" PRIx64, spec_hex_width);
> >>>>>> +		sprintf(buf, format, val);
> >>> snprintf(format, sizeof(format), "0x%%0%u" PRIx64, spec_hex_width);
> >>> snprintf(buf, len, format, val);
> >>>
> >>>>>> +	} else {
> >>>>>> +		sprintf(buf, "0x%" PRIx64, val);
> >>> snprintf(buf, len, "0x%" PRIx64, val);
> >>>
> >>>>>> +	}
> >>>>>> +
> >>>>> snprintf should always be used when printing to the buffer so as
> >> to
> >>>> avoid
> >>>>> overflow. The return value after snprintf should always be
> >> checked
> >>>> too.
> >>>> If check for buffer is sufficient, can the return value of
> snprintf
> >> not
> >>>> be checked?
> >>>> There are also many places in telemetry lib that are not checked
> >> for
> >>>> this return value.
> >>>> Do you have any principles for this?
> >>> You already check the buffer size before printf() into it, so I
> think
> >> it suffices. However, to keep automated code checkers happy, you
> could
> >> easily use snprintf() instead of printf(). (Sorry about not doing it
> in
> >> my example.)
> >>> I somewhat disagree with Bruce's suggestion to check the return
> value
> >> from snprintf() after checking that the buffer is large enough. It
> >> would be effectively dead code, which might cause some confusion. On
> >> the other hand, it might keep some automated code checkers happy. In
> >> this specific case here, I don't have a strong preference.
> >> Sure, you don't need 2 checks - we can either check the length at
> the
> >> start, or else check the length by looking for the return value from
> >> snprintf, but we don't need to do both.
> >>
> >> Given the slight complexity in determining the correct length of the
> >> printf
> >> for the initial size check, I think I would go with the approach of
> >> *not*
> >> checking the buffer initially and just check the snprintf return
> value.
> >> That would remove any possibility of us doing an incorrect length
> >> check, as
> >> was the case originally with this patch.
> > That sounds reasonable to me. Please do that.
> I think above check is necessary. Because snprintf returns the total
> length of string
> formated instead of negative when buffer size isn't sufficient. what do
> you think?

I had the same concern, so I looked it up.

snprintf() returns the length that the string would have, regardless if it was truncated or not.

In other words:

If the string is truncated, snprintf() returns a value greater than the buffer length parameter given to it.

It can be checked like this:

if (snprintf(buf, len, "0x%" PRIx64, val) > len)
    return -EINVAL;

In my opinion, checking for negative return values from snprintf() is not necessary.


  reply	other threads:[~2022-12-15 12:52 UTC|newest]

Thread overview: 122+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-08  8:05 [PATCH 0/8] fix possible data truncation and conversion error Huisong Li
2022-12-08  8:05 ` [PATCH 1/8] telemetry: move to header to controllable range Huisong Li
2022-12-08  8:05 ` [PATCH 2/8] telemetry: add u32 telemetry data type API Huisong Li
2022-12-08  8:05 ` [PATCH 3/8] test: add test cases for u32 telemetry data API Huisong Li
2022-12-08  8:05 ` [PATCH 4/8] ethdev: fix possible data truncation and conversion error Huisong Li
2022-12-08  8:05 ` [PATCH 5/8] mempool: " Huisong Li
2022-12-08  8:05 ` [PATCH 6/8] cryptodev: fix possible data " Huisong Li
2022-12-08  8:05 ` [PATCH 7/8] mem: possible data truncation and " Huisong Li
2022-12-08  8:05 ` [PATCH 8/8] ethdev: telemetry convert capability related variable to hex Huisong Li
2022-12-08 10:55   ` Morten Brørup
2022-12-08 11:20     ` Bruce Richardson
2022-12-09  3:07       ` lihuisong (C)
2022-12-09 11:04 ` [PATCH V2 00/11] telemetry: add u32 value type and hex integer string API Huisong Li
2022-12-09 11:04   ` [PATCH V2 01/11] telemetry: move to header to controllable range Huisong Li
2022-12-09 11:04   ` [PATCH V2 02/11] telemetry: add u32 value type Huisong Li
2022-12-09 11:04   ` [PATCH V2 03/11] test: add test cases for adding u32 value API Huisong Li
2022-12-09 11:04   ` [PATCH V2 04/11] ethdev: fix possible data truncation and conversion error Huisong Li
2022-12-09 11:04   ` [PATCH V2 05/11] mempool: " Huisong Li
2022-12-09 11:04   ` [PATCH V2 06/11] cryptodev: fix possible data " Huisong Li
2022-12-09 11:04   ` [PATCH V2 07/11] mem: possible data truncation and " Huisong Li
2022-12-09 11:04   ` [PATCH V2 08/11] telemetry: refactor mapping betwween value and array type Huisong Li
2022-12-09 11:04   ` [PATCH V2 09/11] telemetry: support adding integer value as hexadecimal Huisong Li
2022-12-09 11:04   ` [PATCH V2 10/11] test: add test cases for adding hex integer values API Huisong Li
2022-12-09 11:04   ` [PATCH V2 11/11] ethdev: display capability values in hexadecimal format Huisong Li
2022-12-09 18:24   ` [PATCH V2 00/11] telemetry: add u32 value type and hex integer string API Morten Brørup
2022-12-12  6:23     ` lihuisong (C)
2022-12-11  9:02   ` fengchengwen
2022-12-12  6:42 ` [PATCH V3 " Huisong Li
2022-12-12  6:42   ` [PATCH V3 01/11] telemetry: move to header to controllable range Huisong Li
2022-12-12  6:42   ` [PATCH V3 02/11] telemetry: add u32 value type Huisong Li
2022-12-12  6:42   ` [PATCH V3 03/11] test: add test cases for adding u32 value API Huisong Li
2022-12-12  6:42   ` [PATCH V3 04/11] ethdev: fix possible data truncation and conversion error Huisong Li
2022-12-12  6:43   ` [PATCH V3 05/11] mempool: " Huisong Li
2022-12-12  6:43   ` [PATCH V3 06/11] cryptodev: fix possible data " Huisong Li
2022-12-12  6:43   ` [PATCH V3 07/11] mem: possible data truncation and " Huisong Li
2022-12-12  6:43   ` [PATCH V3 08/11] telemetry: refactor mapping betwween value and array type Huisong Li
2022-12-12  6:43   ` [PATCH V3 09/11] telemetry: support adding integer value as hexadecimal Huisong Li
2022-12-12  6:43   ` [PATCH V3 10/11] test: add test cases for adding hex integer values API Huisong Li
2022-12-12  6:43   ` [PATCH V3 11/11] ethdev: display capability values in hexadecimal format Huisong Li
2022-12-12 10:31   ` [PATCH V3 00/11] telemetry: add u32 value type and hex integer string API Bruce Richardson
2022-12-12 11:02     ` Morten Brørup
2022-12-12 11:20       ` Bruce Richardson
2022-12-12 12:03         ` Morten Brørup
2022-12-12 12:16           ` Bruce Richardson
2022-12-13 11:00             ` Morten Brørup
2022-12-13 17:12             ` Bruce Richardson
2022-12-13  3:02           ` lihuisong (C)
2022-12-13 10:15 ` [PATCH V4 0/9] telemetry: fix data truncation and conversion error and add hex integer API Huisong Li
2022-12-13 10:15   ` [PATCH V4 1/9] telemetry: move to header to controllable range Huisong Li
2022-12-13 17:10     ` Bruce Richardson
2022-12-14  2:44       ` lihuisong (C)
2022-12-13 10:15   ` [PATCH V4 2/9] ethdev: fix possible data truncation and conversion error Huisong Li
2022-12-13 10:15   ` [PATCH V4 3/9] mempool: " Huisong Li
2022-12-13 10:15   ` [PATCH V4 4/9] cryptodev: fix possible data " Huisong Li
2022-12-13 10:15   ` [PATCH V4 5/9] mem: possible data truncation and " Huisong Li
2022-12-13 10:15   ` [PATCH V4 6/9] telemetry: refactor mapping between value and array type Huisong Li
2022-12-13 16:57     ` Bruce Richardson
2022-12-14  2:46       ` lihuisong (C)
2022-12-13 10:15   ` [PATCH V4 7/9] telemetry: support adding integer value as hexadecimal Huisong Li
2022-12-13 17:09     ` Bruce Richardson
2022-12-14  2:44       ` lihuisong (C)
2022-12-14  7:28         ` Morten Brørup
2022-12-14 12:17           ` lihuisong (C)
2022-12-13 10:15   ` [PATCH V4 8/9] test: add test cases for adding hex integer value API Huisong Li
2022-12-13 10:15   ` [PATCH V4 9/9] ethdev: display capability values in hexadecimal format Huisong Li
2022-12-14 12:32 ` [PATCH V5 0/8] telemetry: fix data truncation and conversion error and add hex integer API Huisong Li
2022-12-14 12:32   ` [PATCH V5 1/8] telemetry: move to header to controllable range Huisong Li
2022-12-14 12:32   ` [PATCH V5 2/8] ethdev: fix possible data truncation and conversion error Huisong Li
2022-12-14 12:32   ` [PATCH V5 3/8] mempool: " Huisong Li
2022-12-14 12:32   ` [PATCH V5 4/8] cryptodev: fix possible data " Huisong Li
2022-12-14 12:32   ` [PATCH V5 5/8] mem: possible data truncation and " Huisong Li
2022-12-14 13:00     ` Morten Brørup
2022-12-15  1:11       ` lihuisong (C)
2022-12-15  7:04         ` Morten Brørup
2022-12-15  7:56           ` lihuisong (C)
2022-12-14 12:32   ` [PATCH V5 6/8] telemetry: support adding integer value as hexadecimal Huisong Li
2022-12-14 15:38     ` Bruce Richardson
2022-12-15  1:32       ` lihuisong (C)
2022-12-14 12:32   ` [PATCH V5 7/8] test: add test cases for adding hex integer value API Huisong Li
2022-12-14 12:32   ` [PATCH V5 8/8] ethdev: display capability values in hexadecimal format Huisong Li
2022-12-15 10:31 ` [PATCH V6 0/8] telemetry: fix data truncation and conversion error and add hex integer API Huisong Li
2022-12-15 10:31   ` [PATCH V6 1/8] telemetry: move to header to controllable range Huisong Li
2022-12-15 10:31   ` [PATCH V6 2/8] ethdev: fix possible data truncation and conversion error Huisong Li
2022-12-15 10:31   ` [PATCH V6 3/8] mempool: " Huisong Li
2022-12-15 10:31   ` [PATCH V6 4/8] cryptodev: fix possible data " Huisong Li
2022-12-15 10:31   ` [PATCH V6 5/8] mem: possible data truncation and " Huisong Li
2022-12-15 10:31   ` [PATCH V6 6/8] telemetry: support adding integer value as hexadecimal Huisong Li
2022-12-15 10:46     ` Bruce Richardson
2022-12-15 11:27       ` lihuisong (C)
2022-12-15 12:00         ` Morten Brørup
2022-12-15 12:15           ` Bruce Richardson
2022-12-15 12:24             ` Morten Brørup
2022-12-15 12:45               ` lihuisong (C)
2022-12-15 12:52                 ` Morten Brørup [this message]
2022-12-15 13:08                   ` Bruce Richardson
2022-12-16  1:15                     ` lihuisong (C)
2022-12-15 10:31   ` [PATCH V6 7/8] test: add test cases for adding hex integer value API Huisong Li
2022-12-15 10:31   ` [PATCH V6 8/8] ethdev: display capability values in hexadecimal format Huisong Li
2022-12-16  1:54 ` [PATCH V7 0/8] telemetry: fix data truncation and conversion error and add hex integer API Huisong Li
2022-12-16  1:54   ` [PATCH V7 1/8] telemetry: move to header to controllable range Huisong Li
2022-12-16  1:54   ` [PATCH V7 2/8] ethdev: fix possible data truncation and conversion error Huisong Li
2022-12-16  1:54   ` [PATCH V7 3/8] mempool: " Huisong Li
2022-12-16  1:54   ` [PATCH V7 4/8] cryptodev: fix possible data " Huisong Li
2022-12-16  1:54   ` [PATCH V7 5/8] mem: possible data truncation and " Huisong Li
2022-12-16  1:54   ` [PATCH V7 6/8] telemetry: support adding integer value as hexadecimal Huisong Li
2022-12-16  1:54   ` [PATCH V7 7/8] test: add test cases for adding hex integer value API Huisong Li
2022-12-16  9:31     ` Bruce Richardson
2022-12-19  7:04       ` lihuisong (C)
2022-12-16  1:54   ` [PATCH V7 8/8] ethdev: display capability values in hexadecimal format Huisong Li
2022-12-19  7:06 ` [PATCH V8 0/8] telemetry: fix data truncation and conversion error and add hex integer API Huisong Li
2022-12-19  7:06   ` [PATCH V8 1/8] telemetry: move to header to controllable range Huisong Li
2022-12-19  7:06   ` [PATCH V8 2/8] ethdev: fix possible data truncation and conversion error Huisong Li
2022-12-19  7:06   ` [PATCH V8 3/8] mempool: " Huisong Li
2022-12-19  7:06   ` [PATCH V8 4/8] cryptodev: fix possible data " Huisong Li
2022-12-19  7:06   ` [PATCH V8 5/8] mem: possible data truncation and " Huisong Li
2022-12-19  7:06   ` [PATCH V8 6/8] telemetry: support adding integer value as hexadecimal Huisong Li
2022-12-19  9:19     ` Bruce Richardson
2022-12-19  7:06   ` [PATCH V8 7/8] test: add test cases for adding hex integer value API Huisong Li
2022-12-19  7:06   ` [PATCH V8 8/8] ethdev: display capability values in hexadecimal format Huisong Li
2023-01-16 12:06   ` [PATCH V8 0/8] telemetry: fix data truncation and conversion error and add hex integer API lihuisong (C)
2023-01-30 10:39     ` lihuisong (C)
2023-02-05 20:43   ` Thomas Monjalon

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=98CBD80474FA8B44BF855DF32C47DC35D875AF@smartserver.smartshare.dk \
    --to=mb@smartsharesystems.com \
    --cc=andrew.rybchenko@oktetlabs.ru \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=fengchengwen@huawei.com \
    --cc=huangdaode@huawei.com \
    --cc=lihuisong@huawei.com \
    --cc=liudongdong3@huawei.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;
as well as URLs for NNTP newsgroup(s).