From 2d43ae29e115a73e2e89563d24b6bb5070f01316 Mon Sep 17 00:00:00 2001 From: marcel Date: Sun, 2 Mar 2014 03:34:06 +0000 Subject: [PATCH] Improve upon previous commit: 1. Check return of mmap(2) (*) 2. Avoid FD leak when fstat fails. 3. Fix style(9). (*) Pointed out by jmg@ git-svn-id: http://svn0.us-east.freebsd.org/base/head/contrib/telnet@262679 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- telnetd/telnetd.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/telnetd/telnetd.c b/telnetd/telnetd.c index 454a377..2d31903 100644 --- a/telnetd/telnetd.c +++ b/telnetd/telnetd.c @@ -926,14 +926,15 @@ telnet(int f, int p, char *host) if (hostinfo && *IM) putf(IM, ptyibuf2); if (IF && if_fd != -1) { - if(fstat (if_fd, &statbuf)!=-1) { - if (statbuf.st_size > 0) { - if_buf = (char *) mmap (0, statbuf.st_size, PROT_READ, 0, if_fd, 0); - putf(if_buf, ptyibuf2); - munmap (if_buf, statbuf.st_size); + if (fstat(if_fd, &statbuf) != -1 && statbuf.st_size > 0) { + if_buf = (char *) mmap (0, statbuf.st_size, + PROT_READ, 0, if_fd, 0); + if (if_buf != MAP_FAILED) { + putf(if_buf, ptyibuf2); + munmap(if_buf, statbuf.st_size); + } } close (if_fd); - } } if (pcc)