Skip to content

Commit 1ff8552

Browse files
committed
oval: allow empty date attr
Signed-off-by: Hank Donnay <[email protected]>
1 parent f0d8695 commit 1ff8552

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

oval/date_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package oval
33
import (
44
"encoding/xml"
55
"os"
6+
"strings"
67
"testing"
78
"time"
89

@@ -124,3 +125,15 @@ func TestUbuntuDates(t *testing.T) {
124125
}
125126
}
126127
}
128+
129+
func TestPointlessElement(t *testing.T) {
130+
const doc = xml.Header + `<div><issued date=""/></div>`
131+
var got struct {
132+
Date Date `xml:"issued"`
133+
}
134+
135+
rd := strings.NewReader(doc)
136+
if err := xml.NewDecoder(rd).Decode(&got); err != nil {
137+
t.Error(err)
138+
}
139+
}

oval/types.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ type Date struct {
111111
var (
112112
_ xml.Unmarshaler = (*Date)(nil)
113113
_ xml.UnmarshalerAttr = (*Date)(nil)
114+
115+
emptyValue = (time.Time{}).Add(1)
114116
)
115117

116118
// UnmarshalXML implements xml.Unmarshaler.
@@ -126,10 +128,17 @@ func (d *Date) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
126128
if err := dec.DecodeElement(&s, &start); err != nil {
127129
return err
128130
}
129-
// If the date is set but an empty string is the inner element, then the
130-
// date was set by an attr.
131-
if s == "" && !d.Date.IsZero() {
131+
switch {
132+
case d.Date.Equal(emptyValue):
133+
// If we set the date to this sentinel value, then this element is
134+
// pointless but we need to not return an error.
135+
d.Date = time.Time{}
136+
return nil
137+
case s == "" && !d.Date.IsZero():
138+
// If the date is set but an empty string is the inner element, then the
139+
// date was set by an attr.
132140
return nil
141+
default:
133142
}
134143
var err error
135144
// Try a variety of formats, because everything is terrible.
@@ -156,6 +165,12 @@ func (d *Date) UnmarshalXMLAttr(attr xml.Attr) error {
156165
if attr.Name.Local != name.Local {
157166
return xml.UnmarshalError(fmt.Sprintf("unexpected attr : %v", attr))
158167
}
168+
// We want to allow for an empty value, because some vendors can't be
169+
// bothered to remove empty entities from their database.
170+
if attr.Value == "" {
171+
d.Date = emptyValue
172+
return nil
173+
}
159174
var err error
160175
d.Date, err = time.Parse(dsfmt, attr.Value)
161176
if err != nil {

0 commit comments

Comments
 (0)