~samwhited/xmpp

c07b516d9be8d2f3cf61b639d4accad0af2eab90 — Sam Whited 5 years ago bcd0724
jid: Fix panic when comparing with nil JID

Also when unmarshaling an empty attr
2 files changed, 9 insertions(+), 0 deletions(-)

M jid/jid.go
M jid/jid_test.go
M jid/jid.go => jid/jid.go +6 -0
@@ 271,6 271,9 @@ func (j *JID) String() string {

// Equal performs an octet-for-octet comparison with the given JID.
func (j *JID) Equal(j2 *JID) bool {
	if j == nil || j2 == nil {
		return j == j2
	}
	return j.Localpart() == j2.Localpart() &&
		j.Domainpart() == j2.Domainpart() && j.Resourcepart() == j2.Resourcepart()
}


@@ 323,6 326,9 @@ func (j *JID) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
// UnmarshalXMLAttr satisfies the xml.UnmarshalerAttr interface and unmarshals
// an XML attribute into a valid JID (or returns an error).
func (j *JID) UnmarshalXMLAttr(attr xml.Attr) error {
	if attr.Value == "" {
		return nil
	}
	jid, err := Parse(attr.Value)
	j.localpart = jid.localpart
	j.domainpart = jid.domainpart

M jid/jid_test.go => jid/jid_test.go +3 -0
@@ 142,6 142,9 @@ func TestEqual(t *testing.T) {
		{m, MustParse("mercutio@example.net/nope"), false},
		{m, MustParse("mercutio@e.com/test"), false},
		{m, MustParse("m@example.net/test"), false},
		{(*JID)(nil), (*JID)(nil), true},
		{m, (*JID)(nil), false},
		{(*JID)(nil), m, false},
	} {
		switch {
		case test.eq && !test.j1.Equal(test.j2):