Fix double-free crash if EOF immediately follows </MAP>.
commit25da8085b3c2d572fa2a00d343eb2cbb66609899
authorKalle Olavi Niemitalo <kon@iki.fi>
Wed, 31 Dec 2008 20:06:49 +0000 (31 20:06 +0000)
committerKalle Olavi Niemitalo <Kalle@Astalo.kon.iki.fi>
Wed, 31 Dec 2008 20:15:44 +0000 (31 20:15 +0000)
tree72a82baaaea1aa14e3e11127906949fb648010ee
parentd668b3b6aa215a9ff6a501c2888faa85cdea44b6
Fix double-free crash if EOF immediately follows </MAP>.

look_for_link() used to return 0 both when it found the closing </MAP>
tag, and when it hit the end of the file.  In the first case, it also
added *menu to the memory_list; in the second case, it did not.  The
caller get_image_map() supposedly distinguished between these cases by
checking whether pos >= eof, and freed *menu separately if so.

However, if the </MAP> was at the very end of the HTML file, so that
not even a newline followed it, then look_for_link() left pos == eof
even though it had found the </MAP> and added *menu to the memory_list.
This made get_image_map() misinterpret the result and mem_free(*menu)
even though *menu had already been freed as part of the memory_list;
thus the crash.

To fix this, make look_for_link() return -1 instead of 0 if it hits
EOF without finding the </MAP>.  Then make get_image_map() check the
return value instead of comparing pos to eof.  And add a test case,
although not an automated one.

Alternatively, look_for_link() could have been changed to decrement
pos between finding the </MAP> and returning 0.  Then, the pos >= eof
comparison in get_image_map() would have been false.  That scheme
would however have been a bit more difficult to understand and
maintain, I think.

Reported by Paul B. Mahol.
(cherry picked from commit a2404407ce9b687ef7db12b520909c7f4aeffe91)
NEWS
src/document/html/parser.c
test/imgmap2.html [new file with mode: 0644]