[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...

From: Thomas Schmitt
Subject: Re: disk/mdraid1x_linux.c:181:15: warning: array subscript ...
Date: Tue, 24 Mar 2020 18:54:43 +0100


i wrote:
> >                (char *) &sb.dev_roles - (char *) sb
> >                + grub_le_to_cpu32 (sb.dev_number) * sizeof(grub_uint16_t)

PGNet Dev wrote:
> grub-core/disk/mdraid1x_linux.c:183:6: error: cannot convert to a
> pointer type

My fault. I forgot the "&" before "sb".

  (char *) &sb.dev_roles - (char *) &sb

I invested time in examining the C riddle, not in testing my proposal
by at least some dummy.

Now this compiles for me without complaint by gcc -Wall

 struct {
   char a_array[10];
   uint16_t dev_roles[0];
 } sb;

 printf("%u\n", (unsigned int) (((char *) &sb.dev_roles - (char *) &sb)
                                + 2 * sizeof(uint16_t)));

Running this program yields 14 as result. The same as with the equivalent
of the old expression

 printf("%u\n", (unsigned int) ((char *) &sb.dev_roles[2] - (char *) &sb));

> not sure I'm reading your intent from your post,

My observation is that not "dev_roles[0]" is to blame for the warning, but
rather the computation which involves taking the address of an array
element while not a single one is allocated.
The resulting number is used as offset in a file, not in the sparsely
allocated "struct grub_raid_super_1x sb".

My proposal is to avoid "[...]" in the course of the computation.
This should be valid for both ways to express an open ended struct:
"dev_roles[0]" and "dev_roles[]".

Have a nice day :)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]