[Devel] [PATCH] dm-qcow2: fix a clang build error in the bitmap extension parser

Dmitry Sepp dmitry.sepp at virtuozzo.com
Mon Nov 17 18:59:08 MSK 2025


The error looks as follows:

/root/vzkernel.vzs/drivers/md/dm-qcow2-target.c:798:4: error: expected expression
  798 |                         struct Qcow2BitmapHeaderExt bitmaps_ext;
      |                         ^
/root/vzkernel.vzs/drivers/md/dm-qcow2-target.c:800:26: error: use of undeclared identifier 'bitmaps_ext'
  800 |                         if (ext.len != sizeof(bitmaps_ext)) {
      |                                               ^
/root/vzkernel.vzs/drivers/md/dm-qcow2-target.c:805:24: error: use of undeclared identifier 'bitmaps_ext'
  805 |                         if (offset + sizeof(bitmaps_ext) > end) {
      |                                             ^
...
and more bitmaps_ext related errors follow.

Yes, clang does accept declarations in the middle of code. I think it was not allowed only by some older C standards like C89 but in general it is a valid thing to do. In contrast, declaring variables inside a case block AFAIK is strictly incorrect. So not sure why gcc allows it.

Regarding the fix, I checked the kernel code before submitting this change and found numerous places with curly brackets used to introduce a new scope inside a case block. So in my eyes it's fine.

Best,
Dmitry

________________________________
From: Alexey Kuznetsov <kuznet at virtuozzo.com>
Sent: Monday, November 17, 2025 3:05 PM
To: Dmitry Sepp <dmitry.sepp at virtuozzo.com>
Cc: Konstantin Khorenko <khorenko at virtuozzo.com>; Pavel Tikhomirov <ptikhomirov at virtuozzo.com>; devel at openvz.org <devel at openvz.org>
Subject: Re: [Devel] [PATCH] dm-qcow2: fix a clang build error in the bitmap extension parser

Ack!

I suffer from the same problem with gcc 10. BTW what error clang reports?

But I have a question though. Could you check: does this clang accept
declarations
in the middle of code? gcc 10 looks buggy: it accepts declarations
in the middle of code, but does not accept this one as it insists label must
be followed by non-declaration statement, which sounds utterly stupid when
declations in the middle are accepted :-)

Anyway, was not this a severe violation of coding style? checkpatch
does not complain, so that I guess decalalrtions in the middle of code
are not considered as a bug any longer. Which is sad.

BTW I would consider instead of adding ugly stray {} just to scope,
(which is _entirely_ nice, just looks ugly) to use declaration like this:

                switch (ext.magic) {
                      struct Qcow2BitmapHeaderExt bitmaps_ext;

                case QCOW2_EXT_MAGIC_BITMAPS:

It is syntactically correct in within any C, in line with natural desire to
have all the declarations at beginning of scope. Though looks unusual.



On Mon, Nov 17, 2025 at 9:11 PM Dmitry Sepp <dmitry.sepp at virtuozzo.com> wrote:
>
> Clang is less forgiving than gcc and does not allow to declare variables in the
> middle of a case block.
>
> Fix the issue by correctly intoducing a new scope.
>
> The issue has been observed on:
> clang version 17.0.6 ( 17.0.6-5.vl9)
> Target: x86_64-redhat-linux-gnu
> Thread model: posix
>
> Fixes: 75034a7628f6 ("dm-qcow2: parse bitmap extension")
>
> Signed-off-by: Dmitry Sepp <dmitry.sepp at virtuozzo.com>
>
> Feature: dm-qcow2: block device over QCOW2 files driver
> Signed-off-by: Dmitry Sepp <dmitry.sepp at virtuozzo.com>
> ---
>  drivers/md/dm-qcow2-target.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
> index 90f315315976..bf0b3728790d 100644
> --- a/drivers/md/dm-qcow2-target.c
> +++ b/drivers/md/dm-qcow2-target.c
> @@ -794,7 +794,7 @@ static int qcow2_parse_extensions(struct dm_target *ti, struct qcow2 *qcow2,
>                 offset += sizeof(ext);
>
>                 switch (ext.magic) {
> -               case QCOW2_EXT_MAGIC_BITMAPS:
> +               case QCOW2_EXT_MAGIC_BITMAPS: {
>                         struct Qcow2BitmapHeaderExt bitmaps_ext;
>
>                         if (ext.len != sizeof(bitmaps_ext)) {
> @@ -814,6 +814,7 @@ static int qcow2_parse_extensions(struct dm_target *ti, struct qcow2 *qcow2,
>                                 return ret;
>                         }
>                         break;
> +               }
>                 case QCOW2_EXT_MAGIC_END:
>                         return 0;
>                 default:
> --
> 2.51.0
>
> _______________________________________________
> Devel mailing list
> Devel at openvz.org
> https://lists.openvz.org/mailman/listinfo/devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openvz.org/pipermail/devel/attachments/20251117/e983a05a/attachment-0001.html>


More information about the Devel mailing list