Skip to content

Commit a3bce49

Browse files
committed
Merge pull request #59 from trestletech/master
Handle nil packets when getting LDAPResultCode
2 parents 07a7330 + ef676ed commit a3bce49

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

error.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ var LDAPResultCodeMap = map[uint8]string{
9999
}
100100

101101
func getLDAPResultCode(packet *ber.Packet) (code uint8, description string) {
102-
if len(packet.Children) >= 2 {
102+
if packet == nil {
103+
return ErrorUnexpectedResponse, "Empty packet"
104+
} else if len(packet.Children) >= 2 {
103105
response := packet.Children[1]
106+
if response == nil {
107+
return ErrorUnexpectedResponse, "Empty response in packet"
108+
}
104109
if response.ClassType == ber.ClassApplication && response.TagType == ber.TypeConstructed && len(response.Children) >= 3 {
105110
// Children[1].Children[2] is the diagnosticMessage which is guaranteed to exist as seen here: https://tools.ietf.org/html/rfc4511#section-4.1.9
106111
return uint8(response.Children[0].Value.(int64)), response.Children[2].Value.(string)

error_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ldap
2+
3+
import (
4+
"testing"
5+
6+
"gopkg.in/asn1-ber.v1"
7+
)
8+
9+
// TestNilPacket tests that nil packets don't cause a panic.
10+
func TestNilPacket(t *testing.T) {
11+
// Test for nil packet
12+
code, _ := getLDAPResultCode(nil)
13+
if code != ErrorUnexpectedResponse {
14+
t.Errorf("Should have an 'ErrorUnexpectedResponse' error in nil packets, got: %v", code)
15+
}
16+
17+
// Test for nil result
18+
kids := []*ber.Packet{
19+
&ber.Packet{}, // Unused
20+
nil, // Can't be nil
21+
}
22+
pack := &ber.Packet{Children: kids}
23+
code, _ = getLDAPResultCode(pack)
24+
25+
if code != ErrorUnexpectedResponse {
26+
t.Errorf("Should have an 'ErrorUnexpectedResponse' error in nil packets, got: %v", code)
27+
}
28+
29+
}

0 commit comments

Comments
 (0)