From c4ba2bde70797adb27690efc033f0c4898668547 Mon Sep 17 00:00:00 2001 From: Stefan 'psYchotic' Zwanenburg Date: Wed, 3 Feb 2010 19:25:39 +0100 Subject: [PATCH] Changed every call to sscanf to include the %n directive. This is useful mostly for scanning numeric values, as a trailing non-numeric character is ignored by *scanf's. So what needs to be done is: sscanf(string, "%d%n", &someint, &read_chars) && read_chars == strlen(string). --- ss.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ss.c b/ss.c index b1ca2db..91b9393 100644 --- a/ss.c +++ b/ss.c @@ -195,10 +195,11 @@ int help_cmd(char **argv) { */ int exit_cmd(char **argv) { int exit_status; + int read_chars; if (argv[0] == NULL) { return (3 << 8); } else if (argv[0] != NULL && argv[1] == NULL) { - if (!sscanf(argv[0], "%d", &exit_status)) { + if (!sscanf(argv[0], "%d%n", &exit_status, &read_chars) || read_chars != strlen(argv[0])) { fprintf(stderr, "Could not parse the argument ('%s') as an integer.\n", argv[0]); return 1; } else { @@ -220,12 +221,13 @@ int exit_cmd(char **argv) { */ int kill_cmd(char **argv) { int i; + int read_chars; pid_t pid; if (argv[0] == NULL) { printf("The 'kill' builtin takes at least one argument (a PID).\n"); } else { for (i = 0; argv[i] != NULL; i++) { - if (sscanf(argv[i], "%d", &pid)) { + if (sscanf(argv[i], "%d%n", &pid, &read_chars) && read_chars == strlen(argv[i])) { if (kill(pid, SIGTERM)) { fprintf(stderr, "Couldn't kill the process with PID %d: %s\n", pid, strerror(errno)); } else { -- 2.11.4.GIT