From 9d47cb0c38137581a46951fb7c6fa2a1dc8c24cf Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Mon, 26 Dec 2011 18:42:05 +0100
Subject: [PATCH] pwdx: re-create invalid process id check

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 pwdx.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/pwdx.c b/pwdx.c
index 2743040a..b4aa689c 100644
--- a/pwdx.c
+++ b/pwdx.c
@@ -35,6 +35,23 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
 	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
+int check_pid_argument(char *input)
+{
+	int skip = 0;
+	long pid;
+	char *end = NULL;
+
+	if (!strncmp("/proc/", input, 6))
+		skip = 6;
+	pid = strtol(input + skip, &end, 10);
+
+	if (errno || input + skip == end || (end && *end))
+		return 1;
+	if (pid < 1)
+		return 1;
+	return 0;
+}
+
 int main(int argc, char *argv[])
 {
 	char ch;
@@ -78,6 +95,9 @@ int main(int argc, char *argv[])
 		/* Constant 10 is the length of strings "/proc/" + "/cwd" + 1 */
 		char buf[10 + strlen(argv[i]) + 1];
 
+		if (check_pid_argument(argv[i]))
+			errx(EXIT_FAILURE, _("invalid process id: %s"),
+			     argv[i]);
 		/*
 		 * At this point, all arguments are in the form
 		 * /proc/NNNN or NNNN, so a simple check based on