iand/gedcom

Parser crashed in case of a single spouse in a family record

ligurio opened this issue · 2 comments

Example of Golang source to reproduce bug:

package main

import (
        "bytes"
        "github.com/iand/gedcom"
        "io/ioutil"
)

func main() {
        data, _ := ioutil.ReadFile("fam.ged")
        d := gedcom.NewDecoder(bytes.NewReader(data))
        g, _ := d.Decode()

        for _, rec := range g.Family {
                println(rec.Xref, rec.Husband.Xref, rec.Wife.Xref)
        }
}

Sample of GEDCOM file (fam.ged):

0 @F4@ FAM
1 HUSB @I2@
1 MARR
2 DATE
0 @I2@ INDI
1 NAME Man
1 SEX F
0 @I3@ INDI
1 NAME Woman
1 SEX M
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x21c0]

goroutine 1 [running]:
panic(0xb42c0, 0xc42000a1b0)
        /usr/local/Cellar/go/1.7.4_2/libexec/src/runtime/panic.go:500 +0x1a1
main.main()
        /Users/sergeyb/source/gopath/src/github.com/ligurio/gedcom2sql/repro.go:25 +0x180

Hi! Any progress with issue?

Below is a piece of code which successfully trigger as issue:

package main
                                                                                                         
import (                  
        "bytes"                       
        "fmt"                                       
        "github.com/iand/gedcom"                                                                         
)        
 
const (
        data = `
0 HEAD
1 DEST PAF
0 @I1@ INDI
1 NAME Person1
1 SEX M
0 @I2@ INDI
1 NAME Person2
1 SEX F
0 @F1@ FAM
1 HUSB @I1@
1 WIFE @I2@
0 @F2@ FAM
1 WIFE @I2@
`
)

func main() {

        d := gedcom.NewDecoder(bytes.NewReader([]byte(data)))
        g, _ := d.Decode()
        for _, rec := range g.Family {
                        fmt.Printf("%s\n", rec.Xref)
                        fmt.Printf("%s, %s, %s\n", rec.Xref, rec.Husband.Xref, rec.Wife.Xref)
        }
}
~$ go build fam.go
~$ ./fam
F1            
F1, I1, I2
F2        
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x49af4c]
          
goroutine 1 [running]:
main.main()
        /home/sergeyb/source/gopath/src/github.com/ligurio/gedcom-tools/fam.go:33 +0x1fc

It would be good to fill absent data in structure by NULL during parsing.

iand commented

Sorry it's taken so long to respond to this. I don't think this is an bug. The panic occurs because you are calling .Husband.Xref on a family record that does not have an entry for the husband. The husband field is nil to represent an unknown value.