All of lore.kernel.org
 help / color / mirror / Atom feed
From: leigh@solinno.co.uk
To: xen-devel@lists.xenproject.org
Cc: andrew.cooper3@citrix.com, anthony.perard@citrix.com,
	slack@rabbit.lu, Leigh Brown <leigh@solinno.co.uk>
Subject: [PATCH v3 1/4] tools/misc: xenwatchdogd: add parse_secs()
Date: Thu, 11 Apr 2024 19:20:20 +0100	[thread overview]
Message-ID: <20240411182023.56309-2-leigh@solinno.co.uk> (raw)
In-Reply-To: <20240411182023.56309-1-leigh@solinno.co.uk>

From: Leigh Brown <leigh@solinno.co.uk>

Create a new parse_secs() function to parse the timeout and sleep
parameters. This ensures that non-numeric parameters are not
accidentally treated as numbers.

Signed-off-by: Leigh Brown <leigh@solinno.co.uk>
---
 tools/misc/xenwatchdogd.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/tools/misc/xenwatchdogd.c b/tools/misc/xenwatchdogd.c
index 112b706357..9fa772e49f 100644
--- a/tools/misc/xenwatchdogd.c
+++ b/tools/misc/xenwatchdogd.c
@@ -49,6 +49,18 @@ static void catch_usr1(int sig)
     done = true;
 }
 
+static int parse_secs(const char *arg, const char *what)
+{
+    char *endptr;
+    unsigned long val;
+
+    val = strtoul(arg, &endptr, 0);
+    if (val > INT_MAX || *endptr)
+	errx(EXIT_FAILURE, "invalid %s: '%s'", what, arg);
+
+    return val;
+}
+
 int main(int argc, char **argv)
 {
     int id;
@@ -64,16 +76,11 @@ int main(int argc, char **argv)
     if (h == NULL)
 	err(EXIT_FAILURE, "xc_interface_open");
 
-    t = strtoul(argv[1], NULL, 0);
-    if (t == ULONG_MAX)
-	err(EXIT_FAILURE, "strtoul");
+    t = parse_secs(argv[1], "timeout");
 
     s = t / 2;
-    if (argc == 3) {
-	s = strtoul(argv[2], NULL, 0);
-	if (s == ULONG_MAX)
-	    err(EXIT_FAILURE, "strtoul");
-    }
+    if (argc == 3)
+	s = parse_secs(argv[2], "sleep");
 
     if (signal(SIGHUP, &catch_exit) == SIG_ERR)
 	err(EXIT_FAILURE, "signal");
-- 
2.39.2



  reply	other threads:[~2024-04-11 18:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11 18:20 [PATCH v3 0/4] xenwatchdogd bugfixes and enhancements leigh
2024-04-11 18:20 ` leigh [this message]
2024-04-22 16:59   ` [PATCH v3 1/4] tools/misc: xenwatchdogd: add parse_secs() Anthony PERARD
2024-04-11 18:20 ` [PATCH v3 2/4] tools/misc: xenwatchdogd enhancements leigh
2024-04-22 17:10   ` Anthony PERARD
2024-04-11 18:20 ` [PATCH v3 3/4] tools/misc: Add xenwatchdogd.c copyright notice leigh
2024-04-22 17:05   ` Anthony PERARD
2024-04-11 18:20 ` [PATCH v3 4/4] docs/man: Add xenwatchdog manual page leigh
2024-04-22 17:13   ` Anthony PERARD

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=20240411182023.56309-2-leigh@solinno.co.uk \
    --to=leigh@solinno.co.uk \
    --cc=andrew.cooper3@citrix.com \
    --cc=anthony.perard@citrix.com \
    --cc=slack@rabbit.lu \
    --cc=xen-devel@lists.xenproject.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.