This is the mail archive of the
systemtap@sourceware.org
mailing list for the systemtap project.
Re: An enhancement of -p option checking so as to accept only one digital number
Hi Mark,
On Mon, May 18, 2009 at 6:08 PM, Mark Wielaard <mjw@redhat.com> wrote:
> Hi Sunzen,
> I think it is a fine enhancement. It will probably help someone when
> starting out with stap and getting the option arguments wrong.
Thanks, i also think so. It will get more bullet proof.
> If you have time to do it generically please do. But if you just fix it option
> at a time that seems fine too. Assuming it isn't really lots of
> duplicate code, which it doesn't seem to be, just one line per option
> processed.
I prefer to fix one option at a time, and use strtoul(). The blow
patch is just for -p option. Patches for other options are basically
similar.
Fix: Enhance -p option checking so as to just accept valid number
--
diff --git a/main.cxx b/main.cxx
index 1ac5dd5..39d835d 100644
--- a/main.cxx
+++ b/main.cxx
@@ -469,6 +469,7 @@ main (int argc, char * const argv [])
while (true)
{
int long_opt;
+ char * num_endptr;
#define LONG_OPT_KELF 1
#define LONG_OPT_KMAP 2
#define LONG_OPT_IGNORE_VMLINUX 3
@@ -518,8 +519,8 @@ main (int argc, char * const argv [])
cerr << "Listing (-l) mode implies pass 2." << endl;
usage (s, 1);
}
- s.last_pass = atoi (optarg);
- if (s.last_pass < 1 || s.last_pass > 5)
+ s.last_pass = (int)strtoul(optarg, &num_endptr, 10);
+ if (*num_endptr != '\0' || s.last_pass < 1 || s.last_pass > 5)
{
cerr << "Invalid pass number (should be 1-5)." << endl;
usage (s, 1);
--
--
sunzen
<<freedom & enjoyment>>