Skip to content

Add a nil guard to prevent crashes when a link's media type cannot be parsed - #324

Open
leonardr wants to merge 5 commits into
readium:developfrom
bookshop-org:bookshop/v0.15.1
Open

Add a nil guard to prevent crashes when a link's media type cannot be parsed#324
leonardr wants to merge 5 commits into
readium:developfrom
bookshop-org:bookshop/v0.15.1

Conversation

@leonardr

Copy link
Copy Markdown

This branch fixes a problem I've seen in real EPUBs (sample available on request) which causes a crash on load.

The problem is triggered by a link in the OPF with a media type that causes mediatype.New to error; in my case it was "application/application/x-font-ttf". The Link object ends up with a nil MediaType. There are a number of places in the code that call functions on link.MediaType without a nil guard, which causes the crash..

In this branch I added nil guards everywhere in the code I think is relevant. The end result should be that a link with a nil media type is not anything in particular: not a bitmap, not HTML, etc.

An alternative implementation would be to define a sentinel "unknown" MediaType to stand in for the case where mediatype.New can't parse the actual media type. Then we can guarantee mediatype.New always returns a MediaType and the nil guards won't be necessary. That's a more systematic change, so before working on it, I want to understand your general policy on having go-toolkit handle invalid data in EPUBs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant