[PATCH phosphor-event v5 2/2] Fixed Multiple associations occasionally geting truncated

OpenBMC Patches openbmc-patches at stwcx.xyz
Thu Mar 17 10:10:30 AEDT 2016


From: Chris Austen <austenc at us.ibm.com>

After additional testing it was found that the association list gets truncated
leaving you with only one item.  Upon code review it was realized that the
use of strtok manipulated a cached string and tokenized the string with NULLs.

Changed to use a temp buffer for strtok to manipulate and switched to
strtok_r since it is a best practice to ensure things are thread safe.
---
 event_messaged_sdbus.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/event_messaged_sdbus.c b/event_messaged_sdbus.c
index 907ac53..fc5c0a2 100644
--- a/event_messaged_sdbus.c
+++ b/event_messaged_sdbus.c
@@ -99,7 +99,7 @@ static int prop_message_assoc(sd_bus *bus,
 	messageEntry_t *m = (messageEntry_t*) userdata;
 	event_record_t *rec;
 	char *p;
-	char *token;
+	char *token, *saveptr;
 
 	rec = message_record_open(m->em, m->logid);
 	if (!rec) {
@@ -110,9 +110,13 @@ static int prop_message_assoc(sd_bus *bus,
 		return -1;
 	}
 
-	p = rec->association;
+	/* strtok manipulates a string.  That means if any caching was done   */
+	/* then the original string would get messed up.  To avoid that since */
+	/* I know there is caching done simple make a copy and mess with that */
+	p = strdup(rec->association);
+
+	token = strtok_r(p, " ", &saveptr);
 
-	token = strtok(p, " ");
 	if (token) {
 		r = sd_bus_message_open_container(reply, 'a', "(sss)");
 		if (r < 0) {
@@ -120,18 +124,19 @@ static int prop_message_assoc(sd_bus *bus,
 		}
 
 		while(token) {
-
 			r = sd_bus_message_append(reply, "(sss)", "fru", "event", token);
 			if (r < 0) {
 				fprintf(stderr,"Error adding properties for %s to reply %s\n", token, strerror(-r));
 			}
 
-			token = strtok(NULL, " ");
+			token = strtok_r(NULL, " ", &saveptr);
 		}
 
 		r = sd_bus_message_close_container(reply);
 	}
 
+	free(p);
+
 	return r;
 }
 
@@ -148,12 +153,12 @@ static int prop_message(sd_bus *bus,
 	messageEntry_t *m = (messageEntry_t*) userdata;
 	char *p;
 	struct tm *tm_info;
-	char buffer[32];
+	char buffer[36];
 	event_record_t *rec;
 
 	rec = message_record_open(m->em, m->logid);
 	if (!rec) {
-		fprintf(stderr,"Warning missing evnet log for %lx\n", m->logid);
+		fprintf(stderr,"Warning missing event log for %lx\n", m->logid);
 		sd_bus_error_set(error,
 			SD_BUS_ERROR_FILE_NOT_FOUND,
 			"Could not find log file");
@@ -282,7 +287,9 @@ static int method_accept_test_message(sd_bus_message *m,
 
 	syslog(LOG_NOTICE, "%s %s (%s)", rec.severity, rec.message, rec.association);
 	logid = message_create_new_log_event(em, &rec);
-	send_log_to_dbus(em, logid);
+
+	if (logid)
+		send_log_to_dbus(em, logid);
 
 	return sd_bus_reply_method_return(m, "q", logid);
 }
-- 
2.7.1




More information about the openbmc mailing list