fix #H6867 - mail buffer overrun
authorPatR <rankin@nethack.org>
Mon, 19 Feb 2018 19:59:14 +0000 (11:59 -0800)
committerPatR <rankin@nethack.org>
Mon, 19 Feb 2018 19:59:14 +0000 (11:59 -0800)
commit859ef823d6ae78401ae12c5628e6811a675a47b0
treed0df95be00c1e073efc9e141927cdcc5e24df18d
parent7a0ad0ff21958924805cf8d0419d73a4e42d97d6
fix #H6867 - mail buffer overrun

Web contact report of a github pull request.  A previous fix from
same user dealt with potential crash caused by freeing mailbox data
when the mailbox came from getenv("MAIL").  getenv() doesn't return
a value obtained by malloc so freeing it was bad.  The fix was to
allocate memory to hold a copy of getenv("MAIL") so that free() was
valid.  Unfortunately it didn't allocate enough space to hold the
terminating '\0' so potentially corrupted malloc/free bookkeeping
data.  And the alloc+copy was being performed every time the mailbox
was checked, resulting in leaked memory from the previous check (if
MAIL came from player's environment).  Fortunately the recheck only
takes place after new mail is actually detected and reported to the
player so the leak was probably small for most folks.

This compiles for the set of conditionals that apply to me (after
taking out -DNOMAIL that the hints put in my Makefile) but I can't
test that it actually works since mail is never delivered to this
machine.
doc/fixes36.1
src/mail.c