diff options
author | David Rientjes <rientjes@google.com> | 2009-08-26 17:29:20 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-08-26 23:06:52 -0400 |
commit | a30b595d2ca6d39e784a1bed5f2b35f3d7a03af7 (patch) | |
tree | 101d1cdf6088a36a09ad5470a062331a74ab657d | |
parent | 054b2b13ccba4876a1ce98a7ede7dab7d6893d01 (diff) |
flex_array: fix get function for elements in base starting at non-zero
If all array elements fit into the base structure and data is copied using
flex_array_put() starting at a non-zero index, flex_array_get() will fail
to return the data.
This fixes the bug by only checking for NULL parts when all elements do
not fit in the base structure when flex_array_get() is used. Otherwise,
fa_element_to_part_nr() will always be 0 since there are no parts
structures needed and such element may never have been put. Thus, it will
remain NULL due to the kzalloc() of the base.
Additionally, flex_array_put() now only checks for a NULL part when all
elements do not fit in the base structure. This is otherwise unnecessary
since the base structure is guaranteed to exist (or we would have already
hit a NULL pointer).
Signed-off-by: David Rientjes <rientjes@google.com>
Acked-by: Dave Hansen <dave@linux.vnet.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | lib/flex_array.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/flex_array.c b/lib/flex_array.c index 08f1636d296a..e73c691aec36 100644 --- a/lib/flex_array.c +++ b/lib/flex_array.c | |||
@@ -198,10 +198,11 @@ int flex_array_put(struct flex_array *fa, int element_nr, void *src, gfp_t flags | |||
198 | return -ENOSPC; | 198 | return -ENOSPC; |
199 | if (elements_fit_in_base(fa)) | 199 | if (elements_fit_in_base(fa)) |
200 | part = (struct flex_array_part *)&fa->parts[0]; | 200 | part = (struct flex_array_part *)&fa->parts[0]; |
201 | else | 201 | else { |
202 | part = __fa_get_part(fa, part_nr, flags); | 202 | part = __fa_get_part(fa, part_nr, flags); |
203 | if (!part) | 203 | if (!part) |
204 | return -ENOMEM; | 204 | return -ENOMEM; |
205 | } | ||
205 | dst = &part->elements[index_inside_part(fa, element_nr)]; | 206 | dst = &part->elements[index_inside_part(fa, element_nr)]; |
206 | memcpy(dst, src, fa->element_size); | 207 | memcpy(dst, src, fa->element_size); |
207 | return 0; | 208 | return 0; |
@@ -257,11 +258,12 @@ void *flex_array_get(struct flex_array *fa, int element_nr) | |||
257 | 258 | ||
258 | if (element_nr >= fa->total_nr_elements) | 259 | if (element_nr >= fa->total_nr_elements) |
259 | return NULL; | 260 | return NULL; |
260 | if (!fa->parts[part_nr]) | ||
261 | return NULL; | ||
262 | if (elements_fit_in_base(fa)) | 261 | if (elements_fit_in_base(fa)) |
263 | part = (struct flex_array_part *)&fa->parts[0]; | 262 | part = (struct flex_array_part *)&fa->parts[0]; |
264 | else | 263 | else { |
265 | part = fa->parts[part_nr]; | 264 | part = fa->parts[part_nr]; |
265 | if (!part) | ||
266 | return NULL; | ||
267 | } | ||
266 | return &part->elements[index_inside_part(fa, element_nr)]; | 268 | return &part->elements[index_inside_part(fa, element_nr)]; |
267 | } | 269 | } |