Re: [PATCH v3 1/6] initramfs_test: add fill_cpio() format parameter
From: Petr Mladek
Date: Thu Mar 26 2026 - 12:38:19 EST
On Mon 2026-03-23 15:54:17, Andy Shevchenko wrote:
> From: David Disseldorp <ddiss@xxxxxxx>
>
> fill_cpio() uses sprintf() to write out the in-memory cpio archive from
> an array of struct initramfs_test_cpio. This change allows callers to
> override the cpio sprintf() format string so that future tests can
> intentionally corrupt the header with non-hex values.
>
> --- a/init/initramfs_test.c
> +++ b/init/initramfs_test.c
> @@ -27,7 +27,10 @@ struct initramfs_test_cpio {
> char *data;
> };
>
> -static size_t fill_cpio(struct initramfs_test_cpio *cs, size_t csz, char *out)
> +#define CPIO_HDR_FMT "%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%s"
> +
> +static size_t fill_cpio(struct initramfs_test_cpio *cs, size_t csz, char *fmt,
> + char *out)
This should be "const char *fmt" as pointed out by
https://sashiko.dev/#/patchset/20260323150054.3587083-1-andriy.shevchenko%40linux.intel.com
> {
> int i;
> size_t off = 0;
> @@ -38,9 +41,7 @@ static size_t fill_cpio(struct initramfs_test_cpio *cs, size_t csz, char *out)
> size_t thislen;
>
> /* +1 to account for nulterm */
> - thislen = sprintf(pos, "%s"
> - "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x"
> - "%s",
> + thislen = sprintf(pos, fmt,
> c->magic, c->ino, c->mode, c->uid, c->gid, c->nlink,
> c->mtime, c->filesize, c->devmajor, c->devminor,
> c->rdevmajor, c->rdevminor, c->namesize, c->csum,
The compiler is not longer able to check whether the number/type of
parameters match the given printf format. It was poitned out by
Sashiko.dev AI and I agree that it makes it harder to maintain.
A solution might be to split this sprintf out into a macro.
Or add some "bool inject_ox" parameter and keep the format
hardcoded here.
I did not found any other problems ;-)
Best Regards,
Petr