From: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
To: Zhu Jun <zhujun2@cmss.chinamobile.com>
Cc: <wbg@kernel.org>, <linux-iio@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] tools/counter: Reject invalid watch fields before narrowing
Date: Fri, 20 Nov 2026 18:40:31 +0100 [thread overview]
Message-ID: <32296215-1101-453e-bdb5-617c2a32df0b@foss.st.com> (raw)
In-Reply-To: <20261120061932.98210-1-zhujun2@cmss.chinamobile.com>
On 11/20/26 07:19, Zhu Jun wrote:
> The channel, component ID and parent fields are all eight bits wide.
> The current parser accepts trailing characters and silently narrows
> values above 255, which can select the wrong counter watch. Parse
> these fields before assignment and only accept a value in range.
>
> Fixes: 1e73427f6635 ("tools/counter: add a flexible watch events tool")
> Signed-off-by: Zhu Jun <zhujun2@cmss.chinamobile.com>
> ---
Hi Zhu
Acked-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
Best Regards,
Thanks,
Fabrice
> tools/counter/counter_watch_events.c | 51 ++++++++++------------------
> 1 file changed, 18 insertions(+), 33 deletions(-)
>
> diff --git a/tools/counter/counter_watch_events.c b/tools/counter/counter_watch_events.c
> index 15e21b0c5ffd..432a0cb9d300 100644
> --- a/tools/counter/counter_watch_events.c
> +++ b/tools/counter/counter_watch_events.c
> @@ -13,6 +13,7 @@
> #include <linux/kernel.h>
> #include <stdlib.h>
> #include <stdio.h>
> +#include <stdint.h>
> #include <string.h>
> #include <sys/ioctl.h>
> #include <unistd.h>
> @@ -288,44 +289,28 @@ int main(int argc, char **argv)
> watches[i].event = ret;
> break;
> case WATCH_CHANNEL:
> - if (!value) {
> - fprintf(stderr, "Invalid chan=<number>\n");
> - rc = EXIT_FAILURE;
> - goto err_free_watches;
> - }
> - watches[i].channel = strtoul(value, NULL, 10);
> - if (errno) {
> - perror("strtoul failed: chan=<number>\n");
> - rc = EXIT_FAILURE;
> - goto err_free_watches;
> - }
> - break;
> case WATCH_ID:
> - if (!value) {
> - fprintf(stderr, "Invalid id=<number>\n");
> - rc = EXIT_FAILURE;
> - goto err_free_watches;
> - }
> - watches[i].component.id = strtoul(value, NULL, 10);
> - if (errno) {
> - perror("strtoul failed: id=<number>\n");
> - rc = EXIT_FAILURE;
> - goto err_free_watches;
> - }
> - break;
> - case WATCH_PARENT:
> - if (!value) {
> - fprintf(stderr, "Invalid parent=<number>\n");
> - rc = EXIT_FAILURE;
> - goto err_free_watches;
> - }
> - watches[i].component.parent = strtoul(value, NULL, 10);
> - if (errno) {
> - perror("strtoul failed: parent=<number>\n");
> + case WATCH_PARENT: {
> + long num;
> + char *end;
> +
> + errno = 0;
> + num = value ? strtol(value, &end, 10) : -1;
> + if (!value || end == value || *end || errno ||
> + num < 0 || num > UINT8_MAX) {
> + fprintf(stderr, "Invalid value for %s\n",
> + counter_watch_subopts[ret]);
> rc = EXIT_FAILURE;
> goto err_free_watches;
> }
> + if (ret == WATCH_CHANNEL)
> + watches[i].channel = num;
> + else if (ret == WATCH_ID)
> + watches[i].component.id = num;
> + else
> + watches[i].component.parent = num;
> break;
> + }
> default:
> fprintf(stderr, "Unknown suboption '%s'\n", value);
> rc = EXIT_FAILURE;
>
>
prev parent reply other threads:[~2026-11-20 17:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 6:19 Zhu Jun
2026-11-20 17:40 ` Fabrice Gasnier [this message]
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=32296215-1101-453e-bdb5-617c2a32df0b@foss.st.com \
--to=fabrice.gasnier@foss.st.com \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=wbg@kernel.org \
--cc=zhujun2@cmss.chinamobile.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®