Skip to content

Commit

Permalink
Fix segfault when pub date is mssing
Browse files Browse the repository at this point in the history
  • Loading branch information
giulianopz committed Jul 16, 2023
1 parent 6a8e6a1 commit 6217565
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func NewFeed(url, title string) *Feed {
func (f *Feed) GetItemsOrderedByDate() []*Item {

slices.SortFunc(f.Items, func(a, b *Item) bool {
if a.PubDate == util.NoPubDate || b.PubDate == util.NoPubDate {
return strings.Compare(a.Title, b.Title) <= -1
}
return a.PubDate.After(b.PubDate)
})
return f.Items
Expand Down Expand Up @@ -154,7 +157,12 @@ func (c *Cache) AddFeed(parsedFeed *gofeed.Feed, url string) error {

newFeed := NewFeed(url, title)
for _, parsedItem := range parsedFeed.Items {
cachedItem := NewItem(parsedItem.Title, parsedItem.Link, *parsedItem.PublishedParsed)

pubDate := util.NoPubDate
if parsedItem.PublishedParsed != nil {
pubDate = *parsedItem.PublishedParsed
}
cachedItem := NewItem(parsedItem.Title, parsedItem.Link, pubDate)
newFeed.Items = append(newFeed.Items, cachedItem)
}
c.feeds = append(c.feeds, newFeed)
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import (
"golang.org/x/exp/slices"
)

var NoPubDate time.Time = time.Date(1001, 1, 1, 1, 1, 1, 1, time.UTC)

func RenderArticleRow(pubDate time.Time, title string) string {
if pubDate == NoPubDate {
return fmt.Sprintf("%-20s %s", "N/A", title)
}
return fmt.Sprintf("%-20s %s", pubDate.Format("2006-January-02"), title)
}

Expand Down

0 comments on commit 6217565

Please sign in to comment.