All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] xfsdump: Judge the return value of malloc function
@ 2022-09-17  9:16 ` zhanchengbin
  2022-09-19  9:42   ` Carlos Maiolino
  0 siblings, 1 reply; 2+ messages in thread
From: zhanchengbin @ 2022-09-17  9:16 UTC (permalink / raw)
  To: cem; +Cc: linfeilong, liuzhiqiang26, linux-xfs

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



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] xfsdump: Judge the return value of malloc function
  2022-09-17  9:16 ` [PATCH v2] xfsdump: Judge the return value of malloc function zhanchengbin
@ 2022-09-19  9:42   ` Carlos Maiolino
  0 siblings, 0 replies; 2+ messages in thread
From: Carlos Maiolino @ 2022-09-19  9:42 UTC (permalink / raw)
  To: zhanchengbin; +Cc: linfeilong, liuzhiqiang26, linux-xfs

On Sat, Sep 17, 2022 at 05:16:09PM +0800, zhanchengbin wrote:
> Add judgment on the return value of malloc function.
> 
> Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>

If you are sending a new version, please, keep a short changelog, so, people can
understand why you're sending a new version.

Thanks.

-- 
Carlos Maiolino

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-19  9:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <yheUla9csTdQLfCMUYXUn7lO9OEuieknMbKuPyGsUnT6AfIlgd9CViWYzi7pvgpE8BxLcqMEVvbiJaodODRO1A==@protonmail.internalid>
2022-09-17  9:16 ` [PATCH v2] xfsdump: Judge the return value of malloc function zhanchengbin
2022-09-19  9:42   ` Carlos Maiolino

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.