Skip to content

Commit

Permalink
fix error where multiple blank lines cause panic while parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vetinari committed Aug 5, 2016
1 parent bdac54f commit 94b60a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 5 additions & 1 deletion ldif/ldif.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
"encoding/base64"
"errors"
"fmt"
"gopkg.in/ldap.v2"
"io"
"io/ioutil"
"net/url"

"gopkg.in/ldap.v2"
// "os"
"strconv"
"strings"
Expand Down Expand Up @@ -98,6 +99,9 @@ func Unmarshal(r io.Reader, l *LDIF) (err error) {
if len(line) == 0 && err == io.EOF {
return nil
}
if len(line) == 0 && len(lines) == 0 {
continue
}
lines = append(lines, line)
entry, perr := l.parseEntry(lines)
if perr != nil {
Expand Down
29 changes: 27 additions & 2 deletions ldif/ldif_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ldif_test

import (
"gopkg.in/ldap.v2/ldif"
"io/ioutil"
"os"
"testing"

"gopkg.in/ldap.v2/ldif"
)

var ldifRFC2849Example = `version: 1
Expand Down Expand Up @@ -181,4 +182,28 @@ func TestLDIFURL(t *testing.T) {
}
}

// vim: ts=4 sw=4 noexpandtab nolist
var ldifMultiBlankLines = `# Organization Units
dn: ou=users,dc=example,dc=com
objectClass: organizationalUnit
objectClass: top
ou: users
# searches for above empty line for dn but fails and errors out in this PR
# Even though this is a valid LDIF file for ldapadd
dn: ou=groups,dc=example,dc=com
objectClass: organizationalUnit
objectClass: top
ou: groups
`

func TestLDIFMultiBlankLines(t *testing.T) {
l, err := ldif.Parse(ldifMultiBlankLines)
if err != nil {
t.Errorf("Failed to parse LDIF: %s", err)
}
ou := l.Entries[1].Entry.GetAttributeValue("ou")
if ou != "groups" {
t.Errorf("wrong ou in second entry: %s", ou)
}
}

0 comments on commit 94b60a2

Please sign in to comment.