mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Maninder Singh <maninder1.s@samsung.com>
To: ard.biesheuvel@linaro.org, catalin.marinas@arm.com, bp@suse.de,
	deller@gmx.de, will.deacon@arm.com, tony.luck@intel.com
Cc: linux-kernel@vger.kernel.org, ajeet.y@samsung.com,
	pankaj.m@samsung.com, Maninder Singh <maninder1.s@samsung.com>,
	Vaneet Narang <v.narang@samsung.com>
Subject: [PATCH 1/2] scripts/sortextable: make close happens only if file is opened
Date: Thu, 11 Aug 2016 09:52:31 +0530	[thread overview]
Message-ID: <1470889351-11159-1-git-send-email-maninder1.s@samsung.com> (raw)

1. Currently code is doing close even if it is failed to open,
   which is wrong so fixing the same.
2. if file open is failed just return from there, no need of
   next operations.
3. Use mmap address only after validation.

Issue is reported by static checker tool.

Reported-by: Ajeet Yadav <ajeet.y@samsung.com>
Signed-off-by: Maninder Singh <maninder1.s@samsung.com>
Signed-off-by: Vaneet Narang <v.narang@samsung.com>
---
 scripts/sortextable.c |   13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/scripts/sortextable.c b/scripts/sortextable.c
index f453b7c..30b4e7c 100644
--- a/scripts/sortextable.c
+++ b/scripts/sortextable.c
@@ -70,7 +70,6 @@ cleanup(void)
 {
 	if (!mmap_failed)
 		munmap(ehdr_curr, sb.st_size);
-	close(fd_map);
 }
 
 static void __attribute__((noreturn))
@@ -91,7 +90,13 @@ static void *mmap_file(char const *fname)
 	void *addr;
 
 	fd_map = open(fname, O_RDWR);
-	if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
+
+	if (fd_map < 0) {
+		perror("open");
+		fail_file();
+		return MAP_FAILED;
+	}
+	if (fstat(fd_map, &sb) < 0) {
 		perror(fname);
 		fail_file();
 	}
@@ -106,6 +111,7 @@ static void *mmap_file(char const *fname)
 		fprintf(stderr, "Could not mmap file: %s\n", fname);
 		fail_file();
 	}
+	close(fd_map);
 	return addr;
 }
 
@@ -270,6 +276,9 @@ do_file(char const *const fname)
 	table_sort_t custom_sort;
 	Elf32_Ehdr *ehdr = mmap_file(fname);
 
+	if (ehdr == MAP_FAILED)
+		return;
+
 	ehdr_curr = ehdr;
 	switch (ehdr->e_ident[EI_DATA]) {
 	default:
-- 
1.7.9.5

                 reply	other threads:[~2016-08-11  4:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1470889351-11159-1-git-send-email-maninder1.s@samsung.com \
    --to=maninder1.s@samsung.com \
    --cc=ajeet.y@samsung.com \
    --cc=ard.biesheuvel@linaro.org \
    --cc=bp@suse.de \
    --cc=catalin.marinas@arm.com \
    --cc=deller@gmx.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pankaj.m@samsung.com \
    --cc=tony.luck@intel.com \
    --cc=v.narang@samsung.com \
    --cc=will.deacon@arm.com \
    /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

all inboxes | Powered by JetHome®