All of lore.kernel.org
 help / color / mirror / Atom feed
From: zhanchengbin <zhanchengbin1@huawei.com>
To: <cem@kernel.org>
Cc: linfeilong <linfeilong@huawei.com>, <liuzhiqiang26@huawei.com>,
	<linux-xfs@vger.kernel.org>
Subject: [PATCH v2] xfsdump: Judge the return value of malloc function
Date: Sat, 17 Sep 2022 17:16:09 +0800	[thread overview]
Message-ID: <d63e28c3-b265-07f0-3483-ca93a47322d9@huawei.com> (raw)

Add judgment on the return value of malloc function.

Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
---
  common/inventory.c    |  4 ++++
  inventory/inv_stobj.c |  4 ++++
  invutil/invidx.c      | 24 ++++++++++++++++++++++++
  invutil/invutil.c     | 12 ++++++++++++
  4 files changed, 44 insertions(+)

diff --git a/common/inventory.c b/common/inventory.c
index 6ffe9fe..0de7f6f 100644
--- a/common/inventory.c
+++ b/common/inventory.c
@@ -364,6 +364,10 @@ inv_stream_open(

  	/* XXX yukk... make the token descriptors not pointers */
  	stok = (inv_stmtoken_t) malloc(sizeof(invt_strdesc_entry_t));
+	if (stok == NULL) {
+		fprintf(stderr, "%s: internal memory error: stok\n", g_programName);
+		exit(1);
+	}

  	stok->md_sesstok = tok;
  	stok->md_lastmfile = 0;
diff --git a/inventory/inv_stobj.c b/inventory/inv_stobj.c
index e2e8767..5a6786a 100644
--- a/inventory/inv_stobj.c
+++ b/inventory/inv_stobj.c
@@ -1015,6 +1015,10 @@ stobj_unpack_sessinfo(
  	assert (bufp);

  	tmpbuf = (char *)malloc(bufsz);
+	if (tmpbuf == NULL) {
+		fprintf(stderr, "internal memory error: tmpbuf\n");
+		exit(1);
+	}

  	/* first make sure that the magic cookie at the beginning is right.
  	   this isn't null-terminated */
diff --git a/invutil/invidx.c b/invutil/invidx.c
index 5874e8d..5e4c79c 100644
--- a/invutil/invidx.c
+++ b/invutil/invidx.c
@@ -363,15 +363,27 @@ read_stobj_info(int fd, int idx, invt_seshdr_t 
**out_hdr,

      lseek(fd, STOBJ_OFFSET(idx, 0), SEEK_SET);
      hdr = malloc(sizeof(*hdr));
+    if (hdr == NULL) {
+        fprintf(stderr, "%s: internal memory error: hdr\n", g_programName);
+        exit(1);
+    }
      read_n_bytes(fd, (char *)hdr, sizeof(*hdr), "stobj file");

      lseek(fd, hdr->sh_sess_off, SEEK_SET);
      ses = malloc(sizeof(*ses));
+    if (ses == NULL) {
+        fprintf(stderr, "%s: internal memory error: ses\n", g_programName);
+        exit(1);
+    }
      read_n_bytes(fd, (char *)ses, sizeof(*ses), "stobj file");

      if(ses->s_cur_nstreams > 0) {
  	lseek(fd, hdr->sh_streams_off, SEEK_SET);
  	strms = malloc(sizeof(*strms) * ses->s_cur_nstreams);
+	if (strms == NULL) {
+	    fprintf(stderr, "%s: internal memory error: strms\n", g_programName);
+    	    exit(1);
+	}
  	read_n_bytes(fd, (char *)strms, sizeof(*strms) * ses->s_cur_nstreams, 
"stobj file");

  	nmfiles = 0;
@@ -381,6 +393,10 @@ read_stobj_info(int fd, int idx, invt_seshdr_t 
**out_hdr,

  	if(nmfiles > 0) {
  	    mfiles = malloc(sizeof(*mfiles) * nmfiles);
+	    if (mfiles == NULL) {
+		fprintf(stderr, "%s: internal memory error: mfiles\n", g_programName);
+	        exit(1);
+            }
  	    read_n_bytes(fd, (char *)mfiles, sizeof(*mfiles) * nmfiles, 
"stobj file");
  	}
  	else {
@@ -552,6 +568,10 @@ insert_stobj_into_stobjfile(int invidx_fileidx, 
char *filename, int fd,

      /* for seshdr: malloc buffer, copy new entry into buffer, read 
file entries into buffer, write the lot out */
      buf = malloc(((sescnt.ic_curnum - pos) + 1) * sizeof(invt_seshdr_t));
+    if (buf == NULL) {
+	fprintf(stderr, "%s: internal memory error: buf\n", g_programName);
+	exit(1);
+    }
      memmove(buf, hdr, sizeof(invt_seshdr_t));
      lseek(fd, STOBJ_OFFSET(pos, 0), SEEK_SET);
      read_n_bytes(fd, buf + sizeof(invt_seshdr_t), (sescnt.ic_curnum - 
pos) * sizeof(invt_seshdr_t), "stobj file");
@@ -561,6 +581,10 @@ insert_stobj_into_stobjfile(int invidx_fileidx, 
char *filename, int fd,

      /* for session: malloc buffer, copy new entry into buffer, read 
file entries into buffer, write the lot out */
      buf = malloc(((sescnt.ic_curnum - pos) + 1) * sizeof(invt_session_t));
+    if (buf == NULL) {
+	fprintf(stderr, "%s: internal memory error: buf\n", g_programName);
+	exit(1);
+    }
      memmove(buf, ses, sizeof(invt_session_t));
      lseek(fd, STOBJ_OFFSET(sescnt.ic_maxnum, pos), SEEK_SET);
      read_n_bytes(fd, buf + sizeof(invt_session_t), (sescnt.ic_curnum - 
pos) * sizeof(invt_session_t), "stobj file");
diff --git a/invutil/invutil.c b/invutil/invutil.c
index 0d27a0b..304edc0 100644
--- a/invutil/invutil.c
+++ b/invutil/invutil.c
@@ -409,6 +409,10 @@ GetNameOfStobj (char *inv_path, char *filename)

      str = basename(filename);
      name = (char *) malloc(strlen(inv_path) + 1  + strlen(str) + 1);
+    if (name == NULL) {
+	fprintf(stderr, "%s: internal memory error: name\n", g_programName);
+	exit(1);
+    }
      strcpy(name, inv_path);
      strcat(name, "/");
      strcat(name, str);
@@ -425,6 +429,10 @@ GetNameOfInvIndex (char *inv_path, uuid_t uuid)
      uuid_unparse(uuid, str);
      name = (char *) malloc(strlen(inv_path) + 1  + strlen(str)
  			     + strlen(INV_INVINDEX_PREFIX) + 1);
+    if (name == NULL) {
+	fprintf(stderr, "%s: internal memory error: name\n", g_programName);
+	exit(1);
+    }
      strcpy(name, inv_path);
      strcat(name, "/");
      strcat(name, str);
@@ -440,6 +448,10 @@ GetFstabFullPath(char *inv_path)

      fstabname = (char *) malloc(strlen(inv_path) + 1 /* one for the "/" */
  				   + strlen("fstab") + 1);
+    if (fstabname == NULL) {
+	fprintf(stderr, "%s: internal memory error: fstabname\n", g_programName);
+	exit(1);
+    }
      strcpy(fstabname, inv_path);
      strcat(fstabname, "/");
      strcat(fstabname, "fstab");
-- 
2.33.0



             reply	other threads:[~2022-09-17  9:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <yheUla9csTdQLfCMUYXUn7lO9OEuieknMbKuPyGsUnT6AfIlgd9CViWYzi7pvgpE8BxLcqMEVvbiJaodODRO1A==@protonmail.internalid>
2022-09-17  9:16 ` zhanchengbin [this message]
2022-09-19  9:42   ` [PATCH v2] xfsdump: Judge the return value of malloc function Carlos Maiolino

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=d63e28c3-b265-07f0-3483-ca93a47322d9@huawei.com \
    --to=zhanchengbin1@huawei.com \
    --cc=cem@kernel.org \
    --cc=linfeilong@huawei.com \
    --cc=linux-xfs@vger.kernel.org \
    --cc=liuzhiqiang26@huawei.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.