Module Name:    src
Committed By:   roy
Date:           Mon Oct 12 16:54:43 UTC 2020

Modified Files:
        src/usr.sbin/powerd: powerd.c

Log Message:
powerd: Stop crashing if entries are not found in the dictionary

If the first three are not found, log a diagnostic and do nothing.
If the latter two are not found, set them to NULL rather than garbage.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/usr.sbin/powerd/powerd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/powerd/powerd.c
diff -u src/usr.sbin/powerd/powerd.c:1.19 src/usr.sbin/powerd/powerd.c:1.20
--- src/usr.sbin/powerd/powerd.c:1.19	Sun Jun  7 05:54:00 2020
+++ src/usr.sbin/powerd/powerd.c	Mon Oct 12 16:54:43 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: powerd.c,v 1.19 2020/06/07 05:54:00 thorpej Exp $	*/
+/*	$NetBSD: powerd.c,v 1.20 2020/10/12 16:54:43 roy Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -325,18 +325,33 @@ dispatch_power_event_state_change(int fd
 			    __func__, error);
 		return;
 	}
-	
+
 	if (debug) {
 		buf = prop_dictionary_externalize(dict);
 		printf("%s", buf);
 		free(buf);
 	}
 
-	prop_dictionary_get_string(dict, "powerd-script-name", &argv[0]);
-	prop_dictionary_get_string(dict, "driver-name", &argv[1]);
-	prop_dictionary_get_string(dict, "powerd-event-name", &argv[2]);
-	prop_dictionary_get_string(dict, "sensor-name", &argv[3]);
-	prop_dictionary_get_string(dict, "state-description", &argv[4]);
+	/* First three arguments are not optional. */
+	if (!prop_dictionary_get_string(dict, "powerd-script-name", &argv[0])) {
+		powerd_log(LOG_ERR, "dict does not have powerd-script-name");
+		return;
+	}
+	if (!prop_dictionary_get_string(dict, "driver-name", &argv[1])) {
+		powerd_log(LOG_ERR, "dict does not have driver-name");
+		return;
+	}
+	if (!prop_dictionary_get_string(dict, "powerd-event-name", &argv[2])) {
+		powerd_log(LOG_ERR, "dict does not have powerd-event-name");
+		return;
+	}
+
+	/* These arguments are optional. */
+	if (!prop_dictionary_get_string(dict, "sensor-name", &argv[3]))
+		argv[3] = NULL;
+	if (!prop_dictionary_get_string(dict, "state-description", &argv[4]))
+		argv[4] = NULL;
+
 	argv[5] = NULL;
 
 	run_script(argv);

Reply via email to