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

OpenBMC Patches openbmc-patches at stwcx.xyz
Sat Mar 12 09:40: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 | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/event_messaged_sdbus.c b/event_messaged_sdbus.c
index 907ac53..ff9fd5e 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 */
+	asprintf(&p, "%s", 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;
 }
 
-- 
2.7.1




More information about the openbmc mailing list