mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: shouc <scf@ieee.org>
To: linux-kernel@vger.kernel.org
Cc: ch0.han@lge.com, akpm@linux-foundation.org
Subject: [PATCH] page_owner_sort.c: Fix potential nullptr dereferencing
Date: Fri, 21 May 2021 08:38:03 -0700	[thread overview]
Message-ID: <20210521153803.GA537901@shou-ws> (raw)

"malloc" may return a null pointer when OOM. This is a patch for catching all the OOM cases that may lead to nullptr dereferencing.
---
 tools/vm/page_owner_sort.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/tools/vm/page_owner_sort.c b/tools/vm/page_owner_sort.c
index 85eb65ea16d3..894a929d2053 100644
--- a/tools/vm/page_owner_sort.c
+++ b/tools/vm/page_owner_sort.c
@@ -11,10 +11,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
 #include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
 #include <string.h>
 
 struct block_list {
@@ -72,6 +69,10 @@ static void add_list(char *buf, int len)
 		exit(1);
 	}
 	list[list_size].txt = malloc(len+1);
+	if (list[list_size].txt == NULL) {
+		printf("Out of memory\n");
+		exit(1);
+	}
 	list[list_size].len = len;
 	list[list_size].num = 1;
 	memcpy(list[list_size].txt, buf, len);
@@ -133,6 +134,11 @@ int main(int argc, char **argv)
 
 	list2 = malloc(sizeof(*list) * list_size);
 
+	if (list2 == NULL) {
+		printf("Out of memory\n");
+		exit(1);
+	}
+
 	printf("culling\n");
 
 	for (i = count = 0; i < list_size; i++) {
@@ -149,5 +155,7 @@ int main(int argc, char **argv)
 	for (i = 0; i < count; i++)
 		fprintf(fout, "%d times:\n%s\n", list2[i].num, list2[i].txt);
 
+	fclose(fin);
+	fclose(fout);
 	return 0;
 }
-- 
2.27.0


                 reply	other threads:[~2021-05-21 15:38 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=20210521153803.GA537901@shou-ws \
    --to=scf@ieee.org \
    --cc=akpm@linux-foundation.org \
    --cc=ch0.han@lge.com \
    --cc=linux-kernel@vger.kernel.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

all inboxes | Powered by JetHome®