When ranging over an array of structures, the iterand is reused. So storing a
authorChris Jones <christian.jones@sri.com>
Thu, 05 Jan 2012 14:25:40 -0700
changeset 54 b345e0473430
parent 53 4434108fdab0
child 55 ce9f9f7108c8
When ranging over an array of structures, the iterand is reused. So storing a pointer to it will cause trouble down the road.
roster.go
--- a/roster.go	Tue Jan 03 16:56:28 2012 -0700
+++ b/roster.go	Thu Jan 05 14:25:40 2012 -0700
@@ -50,8 +50,8 @@
 			return false
 		}
 		cl.roster = make(map[string] *RosterItem, len(rq.Item))
-		for _, item := range(rq.Item) {
-			cl.roster[item.Jid] = &item
+		for i, item := range(rq.Item) {
+			cl.roster[item.Jid] = &rq.Item[i]
 		}
 		ch <- nil
 		return false
@@ -95,11 +95,11 @@
 func (cl *Client) maybeUpdateRoster(st Stanza) {
 	rq, ok := st.GetNested().(*RosterQuery)
 	if st.GetName() == "iq" && st.GetType() == "set" && ok {
-		for _, item := range(rq.Item) {
+		for i, item := range(rq.Item) {
 			if item.Subscription == "remove" {
 				cl.roster[item.Jid] = nil
 			} else {
-				cl.roster[item.Jid] = &item
+				cl.roster[item.Jid] = &rq.Item[i]
 			}
 		}
 	}