Skip to contentSkip to conversation

xfsdump: Judge the return value of malloc function

PATCH In review zhanchengbin 2 participants 2 messages Sep 17, 2022 – Sep 19, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfsdump: Judge the return value of malloc function

Add judgment on the return value of malloc function.

---
 common/inventory.c    |  4 ++++
 inventory/inv_stobj.c |  4 ++++
 invutil/invidx.c      | 24 ++++++++++++++++++++++++
 invutil/invutil.c     | 12 ++++++++++++
 4 files changed, 44 insertions(+)
common/inventory.c +4 -0
index 6ffe9fe..0de7f6f 100644
@@ -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;
inventory/inv_stobj.c +4 -0
index e2e8767..5a6786a 100644
@@ -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 */
invutil/invidx.c +12 -0
index 5874e8d..5e4c79c 100644
@@ -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");

invutil/invutil.c +12 -0
index 0d27a0b..304edc0 100644
@@ -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");
Signature · 1 line
2.33.0
#1 lore raw
Quoted 3 lines · zhanchengbin

Add judgment on the return value of malloc function.

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

Thanks.

Signature · 1 line
Carlos Maiolino