Skip to content

Commit 150cdf6

Browse files
committed
flex_arrays: allow zero length flex arrays
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 <steffen.klassert@secunet.com> Signed-off-by: Eric Paris <eparis@redhat.com> Tested-by: Chris Richards <gizmo@giz-works.com> Cc: stable@kernel.org [2.6.38+]
1 parent 5a3ea87 commit 150cdf6

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎lib/flex_array.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,16 @@ int flex_array_prealloc(struct flex_array *fa, unsigned int start,
253253
unsigned int end;
254254
struct flex_array_part *part;
255255

256+
if (!start && !nr_elements)
257+
return 0;
258+
if (start >= fa->total_nr_elements)
259+
return -ENOSPC;
260+
if (!nr_elements)
261+
return 0;
262+
256263
end = start + nr_elements - 1;
257264

258-
if (start >= fa->total_nr_elements || end >= fa->total_nr_elements)
265+
if (end >= fa->total_nr_elements)
259266
return -ENOSPC;
260267
if (elements_fit_in_base(fa))
261268
return 0;
@@ -346,6 +353,8 @@ int flex_array_shrink(struct flex_array *fa)
346353
int part_nr;
347354
int ret = 0;
348355

356+
if (!fa->total_nr_elements)
357+
return 0;
349358
if (elements_fit_in_base(fa))
350359
return ret;
351360
for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {

0 commit comments

Comments
 (0)