Skip to contentSkip to conversation

erofs-utils: tar: handle empty filenames correctly

PATCH Superseded Gao Xiang 1 participant 2 messages Feb 27, 2025 – Feb 28, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: tar: handle empty filenames correctly

Tar entries with empty filenames are unusual but shouldn’t cause a crash. Handle this by following tar behavior:
substitute . for the empty filenames.

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")
---
 lib/tar.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
lib/tar.c +7 -2
index 2ea3858..0f21a60 100644
@@ -826,8 +826,13 @@ out_eot:
 		memcpy(path + j, th->name, sizeof(th->name));
 		path[j + sizeof(th->name)] = '\0';
 		j = strlen(path);
-		while (path[j - 1] == '/')
-			path[--j] = '\0';
+		if (__erofs_unlikely(!j)) {
+			path[0] = '.';
+			path[1] = '\0';
+		} else {
+			while (path[j - 1] == '/')
+				path[--j] = '\0';
+		}
 	}
 
 	dataoff = tar->offset;
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH v2 1/1 erofs-utils: tar: handle empty filenames correctly

Tar entries with empty filenames are unusual but shouldn't cause a crash. Handle this by following tar behavior:
substitute . for the empty filenames.

Reproducible image (base64-encoded gzipped blob):
H4sICL2XwGcAA3Rlc3RfcmVhZF9mb3JtYXRfdGFyX2VtcHR5X2ZpbGVuYW1lLnRhcgBjY KA9MDAwMDc3VQDShuamBiAaBGA0hGNoaGBgZGJsaAZUaADiGDIomNLBbQylxSWJRUCnlG Tm4lVXnpGamoNHHtVTClR14ygYBaNgFNAAAAAE6urMAAYAAA==

Fixes 95d315fd7958 ("erofs-utils: introduce tarerofs")

---
changes since v1:
- add print message for this case;
- add a reproduciable blob in the commit message.

 lib/tar.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/tar.c +8 -2
index 2ea3858..941fad2 100644
@@ -826,8 +826,14 @@ out_eot:
 		memcpy(path + j, th->name, sizeof(th->name));
 		path[j + sizeof(th->name)] = '\0';
 		j = strlen(path);
-		while (path[j - 1] == '/')
-			path[--j] = '\0';
+		if (__erofs_unlikely(!j)) {
+			erofs_info("substituting '.' for empty filename");
+			path[0] = '.';
+			path[1] = '\0';
+		} else {
+			while (path[j - 1] == '/')
+				path[--j] = '\0';
+		}
 	}
 
 	dataoff = tar->offset;
Signature · 1 line
2.43.5