Skip to contentSkip to conversation

mmap.2: improve example

PATCH In review Rahul Bedarkar 2 participants 4 messages Jun 2, 2016 – Jun 3, 2016 3 messages not in the archive Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmap.2: improve example

free resources on error paths and at the end after use.

---
 man2/mmap.2 | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
man2/mmap.2 +15 -3
index 0f2f277..cede984 100644
@@ -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
Signature · 6 lines
1.8.3.2

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1464890187-4141-1-git-send-email-rahulbedarkar89-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

Hello Rahul,

Quoted 51 lines · Rahul Bedarkar

free resources on error paths and at the end after use.

---
 man2/mmap.2 | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
man2/mmap.2 +12 -3
index 0f2f277..cede984 100644
@@ -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);

man2/mmap.2 main(int argc, char *argv[]) · line 818

Quoted 10 lines
man2/mmap.2 +3 -0
+        close(fd);
         fprintf(stderr, "partial write");
         exit(EXIT_FAILURE);
     }
 
+    munmap(addr, length + offset \- pa_offset);
+    close(fd);
     exit(EXIT_SUCCESS);
 }
 .fi

Given that handle_error() calls exit(), and that triggers close() on all FDs and munmap() on all mappings, I'm not sure this patch improves things. Can you say some more on why you think it is needed?

Cheers,

Michael

Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 06bc8862-f4b2-c024-3852-e1db3d2ae5fb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw

Hello Michael,

Quoted 7 lines · Michael Kerrisk (man-pages)

Hello Rahul,

Given that handle_error() calls exit(), and that triggers close() on all FDs and munmap() on all mappings, I'm not sure this patch improves things. Can you say some more on why you think it is needed?

I think it is always better to free resources after use to avoid memory leaks later. But doing that in such
simple example might be debatable.

May be at-least at the end we should call munmap() and close(). That will cover munmap() usage as well.

Regards,

Rahul
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CA+NV+Vn9_9eDLvNWhgH7pLFyf+Bjd-fU6Cu0POb3vF-Qb-CnTQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#6 lore raw
Quoted 7 lines · Rahul Bedarkar

Hello Michael,

Quoted 3 lines · Michael Kerrisk (man-pages)

Hello Rahul,

Quoted 8 lines
Quoted 4 lines

Given that handle_error() calls exit(), and that triggers close() on all FDs and munmap() on all mappings, I'm not sure this patch improves things. Can you say some more on why you think it is needed?

I think it is always better to free resources after use to avoid memory leaks later. But doing that in such
simple example might be debatable.

Yep.

Quoted 2 lines

May be at-least at the end we should call munmap() and close(). That will cover munmap() usage as well.

Yes, by way of example, I think it doesn't hurt to add the munmap() call especially. Added.

Thanks for the input.

Cheers,

Michael

Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html