Skip to content

Commit

Permalink
oval: allow empty date attr
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <[email protected]>
  • Loading branch information
hdonnay committed Apr 28, 2020
1 parent f0d8695 commit 1ff8552
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
13 changes: 13 additions & 0 deletions oval/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oval
import (
"encoding/xml"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -124,3 +125,15 @@ func TestUbuntuDates(t *testing.T) {
}
}
}

func TestPointlessElement(t *testing.T) {
const doc = xml.Header + `<div><issued date=""/></div>`
var got struct {
Date Date `xml:"issued"`
}

rd := strings.NewReader(doc)
if err := xml.NewDecoder(rd).Decode(&got); err != nil {
t.Error(err)
}
}
21 changes: 18 additions & 3 deletions oval/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ type Date struct {
var (
_ xml.Unmarshaler = (*Date)(nil)
_ xml.UnmarshalerAttr = (*Date)(nil)

emptyValue = (time.Time{}).Add(1)
)

// UnmarshalXML implements xml.Unmarshaler.
Expand All @@ -126,10 +128,17 @@ func (d *Date) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
if err := dec.DecodeElement(&s, &start); err != nil {
return err
}
// If the date is set but an empty string is the inner element, then the
// date was set by an attr.
if s == "" && !d.Date.IsZero() {
switch {
case d.Date.Equal(emptyValue):
// If we set the date to this sentinel value, then this element is
// pointless but we need to not return an error.
d.Date = time.Time{}
return nil
case s == "" && !d.Date.IsZero():
// If the date is set but an empty string is the inner element, then the
// date was set by an attr.
return nil
default:
}
var err error
// Try a variety of formats, because everything is terrible.
Expand All @@ -156,6 +165,12 @@ func (d *Date) UnmarshalXMLAttr(attr xml.Attr) error {
if attr.Name.Local != name.Local {
return xml.UnmarshalError(fmt.Sprintf("unexpected attr : %v", attr))
}
// We want to allow for an empty value, because some vendors can't be
// bothered to remove empty entities from their database.
if attr.Value == "" {
d.Date = emptyValue
return nil
}
var err error
d.Date, err = time.Parse(dsfmt, attr.Value)
if err != nil {
Expand Down

0 comments on commit 1ff8552

Please sign in to comment.