Skip to content

Commit

Permalink
flex_arrays: allow zero length flex arrays
Browse files Browse the repository at this point in the history
Just like kmalloc will allow one to allocate a 0 length segment of memory
flex arrays should do the same thing.  It should bomb if you try to use
something, but it should at least allow the allocation.

This is needed because when SELinux switched to using flex_arrays in 2.6.38
the inability to allocate a 0 length array resulted in SELinux policy load
returning -ENOSPC when previously it worked.

Based-on-patch-by: Steffen Klassert <[email protected]>
Signed-off-by: Eric Paris <[email protected]>
Tested-by: Chris Richards <[email protected]>
Cc: [email protected] [2.6.38+]
  • Loading branch information
eparis committed Apr 28, 2011
1 parent 5d30b10 commit bf69d41
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/flex_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,16 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
unsigned int end;
struct flex_array_part *part;

if (!start && !nr_elements)
return 0;
if (start >= fa->total_nr_elements)
return -ENOSPC;
if (!nr_elements)
return 0;

end = start + nr_elements - 1;

if (start >= fa->total_nr_elements || end >= fa->total_nr_elements)
if (end >= fa->total_nr_elements)
return -ENOSPC;
if (elements_fit_in_base(fa))
return 0;
Expand Down Expand Up @@ -346,6 +353,8 @@ int flex_array_shrink(struct flex_array *fa)
int part_nr;
int ret = 0;

if (!fa->total_nr_elements)
return 0;
if (elements_fit_in_base(fa))
return ret;
for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
Expand Down

0 comments on commit bf69d41

Please sign in to comment.