From b2c08d1d412aafed706fb829c24ec67feefa4b4e Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Thu, 3 May 2012 20:08:27 +0200
Subject: [PATCH] sysctl: avoid crash when -p file has unexpected input

Commit e01765d30b7c4767868d5379fbfb36d1dc15eba4 added is_deprecated(), which
crashed when user had input without without /proc/sys/ subdirectory.  The below
should illustrate how exactly the bug got to be trickered.

$ cat ~/foo
cause = crash
$ ./sysctl -p ~/foo
Segmentation fault

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

diff --git a/sysctl.c b/sysctl.c
index 92351706..cfe13985 100644
--- a/sysctl.c
+++ b/sysctl.c
@@ -359,6 +359,8 @@ static int WriteSetting(const char *setting)
 	const char *equals;
 	char *tmpname;
 	char *outname;
+	char *last_dot;
+
 	FILE *fp;
 	struct stat ts;
 
@@ -396,7 +398,8 @@ static int WriteSetting(const char *setting)
 	outname[equals - name] = 0;
 	/* change / to . */
 	slashdot(outname, '/', '.');
-	if(is_deprecated(strrchr(outname, '.') + 1)) {
+	last_dot = strrchr(outname, '.');
+	if (last_dot != NULL && is_deprecated(last_dot + 1)) {
 		xwarnx(_("%s is deprecated, value not set"), outname);
 		goto out;
         }
-- 
2.40.0