File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -99,8 +99,13 @@ var LDAPResultCodeMap = map[uint8]string{
9999}
100100
101101func 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 )
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments