From: Rahul Bedarkar <rahulbedarkar89-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Cc: linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
Rahul Bedarkar
<rahulbedarkar89-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Subject: [PATCH 1/1] mmap.2: improve example
Date: Thu, 2 Jun 2016 23:26:27 +0530 [thread overview]
Message-ID: <1464890187-4141-1-git-send-email-rahulbedarkar89@gmail.com> (raw)
free resources on error paths and at the end after use.
Signed-off-by: Rahul Bedarkar <rahulbedarkar89-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
man2/mmap.2 | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/man2/mmap.2 b/man2/mmap.2
index 0f2f277..cede984 100644
--- a/man2/mmap.2
+++ b/man2/mmap.2
@@ -765,8 +765,10 @@ main(int argc, char *argv[])
if (fd == \-1)
handle_error("open");
- if (fstat(fd, &sb) == \-1) /* To obtain file size */
+ if (fstat(fd, &sb) == \-1) { /* To obtain file size */
+ close(fd);
handle_error("fstat");
+ }
offset = atoi(argv[2]);
pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) \- 1);
@@ -774,6 +776,7 @@ main(int argc, char *argv[])
if (offset >= sb.st_size) {
fprintf(stderr, "offset is past end of file\\n");
+ close(fd);
exit(EXIT_FAILURE);
}
@@ -789,18 +792,27 @@ main(int argc, char *argv[])
addr = mmap(NULL, length + offset \- pa_offset, PROT_READ,
MAP_PRIVATE, fd, pa_offset);
- if (addr == MAP_FAILED)
+ if (addr == MAP_FAILED) {
+ close(fd);
handle_error("mmap");
+ }
s = write(STDOUT_FILENO, addr + offset \- pa_offset, length);
if (s != length) {
- if (s == \-1)
+ if (s == \-1) {
+ munmap(addr, length + offset \- pa_offset);
+ close(fd);
handle_error("write");
+ }
+ munmap(addr, length + offset \- pa_offset);
+ close(fd);
fprintf(stderr, "partial write");
exit(EXIT_FAILURE);
}
+ munmap(addr, length + offset \- pa_offset);
+ close(fd);
exit(EXIT_SUCCESS);
}
.fi
--
1.8.3.2
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next reply other threads:[~2016-06-02 17:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-02 17:56 Rahul Bedarkar [this message]
[not found] ` <1464890187-4141-1-git-send-email-rahulbedarkar89-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-06-02 23:29 ` [PATCH 1/1] mmap.2: improve example Michael Kerrisk (man-pages)
[not found] ` <06bc8862-f4b2-c024-3852-e1db3d2ae5fb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2016-06-03 7:46 ` Rahul Bedarkar
[not found] ` <CA+NV+Vn9_9eDLvNWhgH7pLFyf+Bjd-fU6Cu0POb3vF-Qb-CnTQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-06-03 11:55 ` Michael Kerrisk (man-pages)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1464890187-4141-1-git-send-email-rahulbedarkar89@gmail.com \
--to=rahulbedarkar89-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
--cc=linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox