Skip to contentSkip to conversation

erofs-utils: mkfs: add missing `errno = 0` before strto[u]l

PATCH New Gao Xiang 1 participant 3 messages Feb 14, 2025 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/3 erofs-utils: mkfs: add missing `errno = 0` before strto[u]l

strtoull(3) says:

```
Since strtoul() can legitimately return 0 or ULONG_MAX (ULLONG_MAX for
strtoull()) on both success and failure, the calling program should set
errno to 0 before the call, and then determine if an error occurred by
checking whether errno has a nonzero value after the call.
```

Otherwise, --workers= could exit with invalid worker number.

Fixes 7894301e1a80 ("erofs-utils: mkfs: add `--workers=#` parameter")
Fixes 0132cb5ea7d0 ("erofs-utils: mkfs: add `--zfeature-bits` option")
Fixes 7550a30c332c ("erofs-utils: enable incremental builds")
---
 mkfs/main.c | 3 +++
 1 file changed, 3 insertions(+)
mkfs/main.c +3 -0
index a0fce35..9d6a0f2 100644
@@ -817,6 +817,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		case 520: {
 			unsigned int processors;
 
+			errno = 0;
 			cfg.c_mt_workers = strtoul(optarg, &endptr, 0);
 			if (errno || *endptr != '\0') {
 				erofs_err("invalid worker number %s", optarg);
@@ -831,6 +832,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		}
 #endif
 		case 521:
+			errno = 0;
 			i = strtol(optarg, &endptr, 0);
 			if (errno || *endptr != '\0') {
 				erofs_err("invalid zfeature bits %s", optarg);
@@ -847,6 +849,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 			} else if (!strcmp(optarg, "rvsp")) {
 				dataimport_mode = EROFS_MKFS_DATA_IMPORT_RVSP;
 			} else {
+				errno = 0;
 				dataimport_mode = strtol(optarg, &endptr, 0);
 				if (errno || *endptr != '\0') {
 					erofs_err("invalid --%s=%s",
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 2/3 erofs-utils: lib: get rid of tmpfile()

Currently, tmpfile() is problematic:

- It uses FILE * instead of fd;
- It may not leverage $TMPDIR, see __gen_tempfd():
https://sourceware.org/git?p=glibc.git;a=blob;f=stdio-common/tmpfile.c;hb=glibc-2.41

Switch to erofs_tmpfile() throughout the codebase.

---
 configure.ac              |  1 -
 include/erofs/fragments.h |  2 +-
 include/erofs/io.h        |  2 +
 lib/blobchunk.c           | 44 +++++++++---------
 lib/fragments.c           | 94 +++++++++++++++++++--------------------
 lib/io.c                  | 22 +++++++++
 lib/liberofs_private.h    |  2 +
 lib/xattr.c               | 23 ++++++----
 8 files changed, 110 insertions(+), 80 deletions(-)
configure.ac +0 -1
index c9b4acf..6e1e7a1 100644
@@ -281,7 +281,6 @@ AC_CHECK_FUNCS(m4_flatten([
 	strrchr
 	strtoull
 	sysconf
-	tmpfile64
 	utimensat]))
 
 # Detect maximum block size if necessary
include/erofs/fragments.h +1 -1
index 14a1b4a..ccfdd9b 100644
@@ -22,7 +22,7 @@ int z_erofs_pack_file_from_fd(struct erofs_inode *inode, int fd, u32 tofcrc);
 int z_erofs_pack_fragments(struct erofs_inode *inode, void *data,
 			   unsigned int len, u32 tofcrc);
 int erofs_flush_packed_inode(struct erofs_sb_info *sbi);
-FILE *erofs_packedfile(struct erofs_sb_info *sbi);
+int erofs_packedfile(struct erofs_sb_info *sbi);
 
 int erofs_packedfile_init(struct erofs_sb_info *sbi, bool fragments_mkfs);
 void erofs_packedfile_exit(struct erofs_sb_info *sbi);
include/erofs/io.h +2 -0
index d9b33d2..3179ea1 100644
@@ -49,6 +49,8 @@ struct erofs_vfile {
 	};
 };
 
+ssize_t __erofs_io_write(int fd, const void *buf, size_t len);
+
 int erofs_io_fstat(struct erofs_vfile *vf, struct stat *buf);
 ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf, u64 pos, size_t len);
 int erofs_io_fsync(struct erofs_vfile *vf);
lib/blobchunk.c +22 -22

106 lines not on this page · raw message

lib/fragments.c +47 -47

209 lines not on this page · raw message

lib/io.c +22 -0
index dacf8dc..b6eb22a 100644
@@ -26,6 +26,28 @@
 #define EROFS_MODNAME	"erofs_io"
 #include "erofs/print.h"
 
+ssize_t __erofs_io_write(int fd, const void *buf, size_t len)
+{
+	ssize_t ret, written = 0;
+
+	do {
+		ret = write(fd, buf, len);
+		if (ret <= 0) {
+			if (!ret)
+				break;
+			if (errno != EINTR) {
+				erofs_err("failed to write: %s", strerror(errno));
+				return -errno;
+			}
+			ret = 0;
+		}
+		buf += ret;
+		written += ret;
+	} while (written < len);
+
+	return written;
+}
+
 int erofs_io_fstat(struct erofs_vfile *vf, struct stat *buf)
 {
 	if (__erofs_unlikely(cfg.c_dry_run)) {
lib/liberofs_private.h +2 -0
index 0eeca3c..ebd9e70 100644
@@ -23,3 +23,5 @@ static inline void *memrchr(const void *s, int c, size_t n)
 	return NULL;
 }
 #endif
+
+int erofs_tmpfile(void);
lib/xattr.c +14 -9
index 063b01a..cb44786 100644
@@ -811,21 +811,22 @@ static int comp_shared_xattr_item(const void *a, const void *b)
 
 int erofs_xattr_flush_name_prefixes(struct erofs_sb_info *sbi)
 {
-	FILE *f = erofs_packedfile(sbi);
+	int fd = erofs_packedfile(sbi);
 	struct ea_type_node *tnode;
-	off_t offset;
+	s64 offset;
+	int err;
 
 	if (!ea_prefix_count)
 		return 0;
-	offset = ftello(f);
+	offset = lseek(fd, 0, SEEK_CUR);
 	if (offset < 0)
 		return -errno;
-	if (offset > UINT32_MAX)
-		return -EOVERFLOW;
-
 	offset = round_up(offset, 4);
-	if (fseek(f, offset, SEEK_SET))
+	if (lseek(fd, offset, SEEK_SET) < 0)
 		return -errno;
+
+	if ((offset >> 2) > UINT32_MAX)
+		return -EOVERFLOW;
 	sbi->xattr_prefix_start = (u32)offset >> 2;
 	sbi->xattr_prefix_count = ea_prefix_count;
 
@@ -846,10 +847,14 @@ int erofs_xattr_flush_name_prefixes(struct erofs_sb_info *sbi)
 		       infix_len);
 		len = sizeof(struct erofs_xattr_long_prefix) + infix_len;
 		u.s.size = cpu_to_le16(len);
-		if (fwrite(&u.s, sizeof(__le16) + len, 1, f) != 1)
+		err = __erofs_io_write(fd, &u.s, sizeof(__le16) + len);
+		if (err != sizeof(__le16) + len) {
+			if (err < 0)
+				return -errno;
 			return -EIO;
+		}
 		offset = round_up(offset + sizeof(__le16) + len, 4);
-		if (fseek(f, offset, SEEK_SET))
+		if (lseek(fd, offset, SEEK_SET) < 0)
 			return -errno;
 	}
 	erofs_sb_set_fragments(sbi);
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 3/3 erofs-utils: lib: shorten EROFS_FRAGMENT_INMEM_SZ_MAX

In reply to Gao Xiang

EROFS_CONFIG_COMPR_MAX_SZ (currently 4MiB) is so large that could cause OOM kill for small machines.

---
 lib/fragments.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/fragments.c +1 -1
index 3e97f14..758fb87 100644
@@ -31,7 +31,7 @@ struct erofs_fragment_dedupe_item {
 	u8			data[];
 };
 
-#define EROFS_FRAGMENT_INMEM_SZ_MAX	EROFS_CONFIG_COMPR_MAX_SZ
+#define EROFS_FRAGMENT_INMEM_SZ_MAX	(128 * 1024)
 #define EROFS_TOF_HASHLEN		16
 
 #define FRAGMENT_HASHSIZE		65536
Signature · 1 line
2.43.5