diff options
| author | Aaditya Singh <aadityavksingh@gmail.com> | 2026-02-21 18:13:54 +0000 |
|---|---|---|
| committer | Warner Losh <imp@FreeBSD.org> | 2026-04-16 06:05:21 +0000 |
| commit | 50c1240ebfaf920ad12f05eb16d00f8b5b9d72e0 (patch) | |
| tree | 9f3d50dbf0ce36eb29d7d31d74e572c7e18fc36d | |
| parent | ee12645ec737eb940554769af2c275524ea20d37 (diff) | |
mkimg: Fix parsing of filenames containing colons
When using PART_KIND_FILE (-p type:=filename), mkimg uses a colon
to separate an optional offset (e.g., filename:offset).
strsep() was being used to split the string at the first colon.
This caused failures when the filename itself contained a colon
(e.g., "th:is").
This patch uses stat() to check if the entire string exists as a
file. If so, use it directly without splitting.
If the full string is not a valid file, fall back to splitting
at the right-most colon using strrchr().
Uses errc() to fail and exit immediately when an existing directory
is input instead of a file in PART_KIND_FILE mode.
PR: 257960
Signed-off-by: Aaditya Singh <aadityavksingh@gmail.com>
Reviewed by: jlduran
Pull Request: https://github.com/freebsd/freebsd-src/pull/2041
| -rw-r--r-- | usr.bin/mkimg/mkimg.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/usr.bin/mkimg/mkimg.c b/usr.bin/mkimg/mkimg.c index c625b49dc29a..4a288d66be81 100644 --- a/usr.bin/mkimg/mkimg.c +++ b/usr.bin/mkimg/mkimg.c @@ -446,6 +446,8 @@ mkimg(void) { FILE *fp; struct part *part; + struct stat sb; + char *p; lba_t block, blkoffset; uint64_t bytesize, byteoffset; char *size, *offset; @@ -468,12 +470,28 @@ mkimg(void) /* Look for an offset. Set size too if we can. */ switch (part->kind) { case PART_KIND_SIZE: - case PART_KIND_FILE: offset = part->contents; size = strsep(&offset, ":"); - if (part->kind == PART_KIND_SIZE && - expand_number(size, &bytesize) == -1) + if (expand_number(size, &bytesize) == -1) error = errno; + break; + case PART_KIND_FILE: + size = part->contents; + if (stat(part->contents, &sb) == 0) { + if (S_ISDIR(sb.st_mode)) { + errc(EX_IOERR, EISDIR, "partition %d", + part->index + 1); + } + offset = NULL; + } else { + p = strrchr(part->contents, ':'); + if (p != NULL) { + *p = '\0'; + offset = p + 1; + } else { + offset = NULL; + } + } if (offset != NULL) { if (*offset != '+') abs_offset = true; |
