Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check for nil link to avoid runtime panic #95 #103

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ type Atom struct {

func newAtomEntry(i *Item) *AtomEntry {
id := i.Id

link := i.Link
if link == nil {
link = &Link{}
}
if len(id) == 0 {
// if there's no id set, try to create one, either from data or just a uuid
if len(i.Link.Href) > 0 && (!i.Created.IsZero() || !i.Updated.IsZero()) {
if len(link.Href) > 0 && (!i.Created.IsZero() || !i.Updated.IsZero()) {
dateStr := anyTimeFormat("2006-01-02", i.Updated, i.Created)
host, path := i.Link.Href, "/invalid.html"
if url, err := url.Parse(i.Link.Href); err == nil {
host, path := link.Href, "/invalid.html"
if url, err := url.Parse(link.Href); err == nil {
host, path = url.Host, url.Path
}
id = fmt.Sprintf("tag:%s,%s:%s", host, dateStr, path)
Expand All @@ -108,13 +111,13 @@ func newAtomEntry(i *Item) *AtomEntry {
name, email = i.Author.Name, i.Author.Email
}

link_rel := i.Link.Rel
link_rel := link.Rel
if link_rel == "" {
link_rel = "alternate"
}
x := &AtomEntry{
Title: i.Title,
Links: []AtomLink{{Href: i.Link.Href, Rel: link_rel, Type: i.Link.Type}},
Links: []AtomLink{{Href: link.Href, Rel: link_rel, Type: link.Type}},
Id: id,
Updated: anyTimeFormat(time.RFC3339, i.Updated, i.Created),
}
Expand Down Expand Up @@ -142,12 +145,16 @@ func newAtomEntry(i *Item) *AtomEntry {
// create a new AtomFeed with a generic Feed struct's data
func (a *Atom) AtomFeed() *AtomFeed {
updated := anyTimeFormat(time.RFC3339, a.Updated, a.Created)
link := a.Link
if link == nil {
link = &Link{}
}
feed := &AtomFeed{
Xmlns: ns,
Title: a.Title,
Link: &AtomLink{Href: a.Link.Href, Rel: a.Link.Rel},
Link: &AtomLink{Href: link.Href, Rel: link.Rel},
Subtitle: a.Description,
Id: a.Link.Href,
Id: link.Href,
Updated: updated,
Rights: a.Copyright,
}
Expand Down
52 changes: 52 additions & 0 deletions feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,58 @@ func TestFeedSorted(t *testing.T) {
}
}

func TestFeedNil(t *testing.T) {
now, err := time.Parse(time.RFC3339, "2013-01-16T21:52:35-05:00")
if err != nil {
t.Error(err)
}
tz := time.FixedZone("EST", -5*60*60)
now = now.In(tz)

feed := &Feed{
Title: "jmoiron.net blog",
Link: nil,
Description: "discussion about tech, footie, photos",
Author: nil,
Created: now,
Copyright: "This work is copyright © Benjamin Button",
}

feed.Items = []*Item{
{
Title: "Limiting Concurrency in Go",
Link: nil,
Description: "A discussion on controlled parallelism in golang",
Author: nil,
Created: now,
Content: `<p>Go's goroutines make it easy to make <a href="http://collectiveidea.com/blog/archives/2012/12/03/playing-with-go-embarrassingly-parallel-scripts/">embarrassingly parallel programs</a>, but in many &quot;real world&quot; cases resources can be limited and attempting to do everything at once can exhaust your access to them.</p>`,
}}

if _, err := feed.ToAtom(); err != nil {
t.Errorf("unexpected error encoding Atom: %v", err)
}
var buf bytes.Buffer
if err := feed.WriteAtom(&buf); err != nil {
t.Errorf("unexpected error writing Atom: %v", err)
}

if _, err := feed.ToRss(); err != nil {
t.Errorf("unexpected error encoding RSS: %v", err)
}
buf.Reset()
if err := feed.WriteRss(&buf); err != nil {
t.Errorf("unexpected error writing RSS: %v", err)
}

if _, err := feed.ToJSON(); err != nil {
t.Errorf("unexpected error encoding JSON: %v", err)
}
buf.Reset()
if err := feed.WriteJSON(&buf); err != nil {
t.Errorf("unexpected error writing JSON: %v", err)
}
}

var jsonOutputHub = `{
"version": "https://jsonfeed.org/version/1",
"title": "feed title",
Expand Down
6 changes: 5 additions & 1 deletion rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ func (r *Rss) RssFeed() *RssFeed {
image = &RssImage{Url: r.Image.Url, Title: r.Image.Title, Link: r.Image.Link, Width: r.Image.Width, Height: r.Image.Height}
}

var href string
if r.Link != nil {
href = r.Link.Href
}
channel := &RssFeed{
Title: r.Title,
Link: r.Link.Href,
Link: href,
Description: r.Description,
ManagingEditor: author,
PubDate: pub,
Expand Down