oduwsdl / MemGator

A Memento Aggregator CLI and Server in Go
https://memgator.cs.odu.edu/api.html
MIT License
55 stars 11 forks source link

502 errors caused by invalid timemap #49

Closed ikreymer closed 8 years ago

ikreymer commented 8 years ago

Presumably this is caused by an invalid timemap, causes the response to 502

panic: runtime error: index out of range

goroutine 6 [running]:
main.extractMementos(0xc820504f00, 0xc820508e10)
        /go/src/github.com/oduwsdl/memgator/main.go:187 +0xd4c
main.fetchTimemap(0xc8200112c0, 0x13, 0xc820017b10, 0x7, 0xc820010f20, 0x13, 0xc82000b3e0, 0x2a, 0xc820010f00, 0x20, ...)
        /go/src/github.com/oduwsdl/memgator/main.go:273 +0x1357
created by main.aggregateTimemap
        /go/src/github.com/oduwsdl/memgator/main.go:374 +0xa0a

goroutine 1 [IO wait]:
ibnesayeed commented 8 years ago

This is not necessarily due to an invalid TimeMap, rather it's because of a case that was not taken care of. The panic is raised due to the following lines of the code:

kv := regs["kvaldlm"].Split(attr, 2)
linkmap[kv[0]] = kv[1]

In the above lines, a link attribute is split in name and value (0 and 1 indexes of an array respectively), then the value is added to a dictionary with the name as the key. It is possible that there is an attribute that does not have a value. This style of attributes is common in HTML attributes such as async, readonly, selected, and disabled etc. In such a case, existing code will have nothing for the value of kv[1] which will cause this panic. Here is a test case that illustrates this issue.

However, the fix is simple. We just need to check if the kv slice has at least two elements before assigning it, otherwise ignore the attribute.