Skip to contentSkip to conversation

erofs-utils: lib: fix missing NULL checks after strdup() in tarerofs_parse_tar_header()

PATCH New Utkal Singh 1 participant 1 message Mar 14 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: fix missing NULL checks after strdup() in tarerofs_parse_tar_header()

strdup() calls for eh.path and eh.link had no NULL check. On memory allocation failure, eh.path or eh.link would silently become NULL and get dereferenced later, causing a crash.

Add NULL checks after each strdup() call and return -ENOMEM via the existing 'out' cleanup label on failure.

---
 lib/tar.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/tar.c +12 -2
index 26461f8..0963821 100644
@@ -719,10 +719,20 @@ int tarerofs_parse_tar(struct erofs_importer *im, struct erofs_tarfile *tar)
 	int ckksum, ret, rem, j;
 
 	root->dev = tar->dev;
-	if (eh.path)
+	if (eh.path) {
 		eh.path = strdup(eh.path);
-	if (eh.link)
+		if (!eh.path) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
+	if (eh.link) {
 		eh.link = strdup(eh.link);
+		if (!eh.link) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
 	init_list_head(&eh.xattrs);
 
 restart:
Signature · 1 line
2.43.0