lkml.org 
[lkml]   [2019]   [Dec]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH 03/10] efi/libstub: use a helper to iterate over a EFI handle array
On Sat, Dec 14, 2019 at 08:40:57PM +0000, Ard Biesheuvel wrote:
> On Sat, 14 Dec 2019 at 21:33, Arvind Sankar <nivedita@alum.mit.edu> wrote:
> >
> > On Sat, Dec 14, 2019 at 06:57:28PM +0100, Ard Biesheuvel wrote:
> > > Iterating over a EFI handle array is a bit finicky, since we have
> > > to take mixed mode into account, where handles are only 32-bit
> > > while the native efi_handle_t type is 64-bit.
> > >
> > > So introduce a helper, and replace the various occurrences of
> > > this pattern.
> > >
> > > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > > ---
> > >
> > > +#define for_each_efi_handle(handle, array, size, i) \
> > > + for (i = 1, handle = efi_is_64bit() \
> > > + ? (efi_handle_t)(unsigned long)((u64 *)(array))[0] \
> > > + : (efi_handle_t)(unsigned long)((u32 *)(array))[0]; \
> > > + i++ <= (size) / (efi_is_64bit() ? sizeof(efi_handle_t) \
> > > + : sizeof(u32)); \
> > > + handle = efi_is_64bit() \
> > > + ? (efi_handle_t)(unsigned long)((u64 *)(array))[i] \
> > > + : (efi_handle_t)(unsigned long)((u32 *)(array))[i])
> > > +
> > > /*
> > > * The UEFI spec and EDK2 reference implementation both define EFI_GUID as
> > > * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment
> > > --
> > > 2.17.1
> > >
> >
> > This would access one past the array, no? Eg if the array has one
> > handle, i is incremented to 2 the first time the condition is checked,
> > then the loop increment will access array[2] before the condition is
> > checked again. There seem to be at least a couple of other for_each
> > macros that might have similar issues.
> >
>
> Indeed.
>
> > How about the below instead?
> >
> > #define for_each_efi_handle(handle, array, size, i) \
> > for (i = 0; \
> > (i < (size) / (efi_is_64bit() ? sizeof(efi_handle_t) \
> > : sizeof(u32))) && \
> > ((handle = efi_is_64bit() \
> > ? ((efi_handle_t *)(array))[i] \
> > : (efi_handle_t)(unsigned long)((u32 *)(array))[i]), 1);\
> > i++)
> >
>
> Yeah, that looks correct to me, but perhaps we can come up with
> something slightly more readable? :-)
> (Not saying my code was better in that respect)

:) The idiom of the && with , operator is copied from for_each_bvec in bvec.h.

Perhaps more readable with helper macros:

#define __efi_handle_size (efi_is_64bit() ? sizeof(efi_handle_t) \
: sizeof(u32))
#define __efi_handle_elem(array, i) \
(efi_is_64bit() ? ((efi_handle_t *)(array))[i] \
: (efi_handle_t)(uintptr_t)((u32 *)(array))[i])

#define for_each_efi_handle(handle, array, size, i) \
for (i = 0; i < (size) / __efi_handle_size && \
(handle = __efi_handle_elem(array, i), 1); i++)

\
 
 \ /
  Last update: 2019-12-14 22:08    [W:0.284 / U:0.220 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site