Skip to content

Commit 3dfa23b

Browse files
committed
vvfat: Fix array_remove_slice()
array_remove_slice() calls array_roll() with array->next - 1 as the destination index. This is only correct for count == 1, otherwise we're writing past the end of the array. array->next - count would be correct. However, this is the only place ever calling array_roll(), so this rather complicated operation isn't even necessary. Fix the problem and simplify the code by replacing it with a single memmove() call. array_roll() can now be removed. Reported-by: Nathan Huckleberry <[email protected]> Signed-off-by: Kevin Wolf <[email protected]> Message-Id: <[email protected]> Reviewed-by: Eric Blake <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent c79e243 commit 3dfa23b

File tree

1 file changed

+5
-37
lines changed

1 file changed

+5
-37
lines changed

block/vvfat.c

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -140,48 +140,16 @@ static inline void* array_insert(array_t* array,unsigned int index,unsigned int
140140
return array->pointer+index*array->item_size;
141141
}
142142

143-
/* this performs a "roll", so that the element which was at index_from becomes
144-
* index_to, but the order of all other elements is preserved. */
145-
static inline int array_roll(array_t* array,int index_to,int index_from,int count)
146-
{
147-
char* buf;
148-
char* from;
149-
char* to;
150-
int is;
151-
152-
if(!array ||
153-
index_to<0 || index_to>=array->next ||
154-
index_from<0 || index_from>=array->next)
155-
return -1;
156-
157-
if(index_to==index_from)
158-
return 0;
159-
160-
is=array->item_size;
161-
from=array->pointer+index_from*is;
162-
to=array->pointer+index_to*is;
163-
buf=g_malloc(is*count);
164-
memcpy(buf,from,is*count);
165-
166-
if(index_to<index_from)
167-
memmove(to+is*count,to,from-to);
168-
else
169-
memmove(from,from+is*count,to-from);
170-
171-
memcpy(to,buf,is*count);
172-
173-
g_free(buf);
174-
175-
return 0;
176-
}
177-
178143
static inline int array_remove_slice(array_t* array,int index, int count)
179144
{
180145
assert(index >=0);
181146
assert(count > 0);
182147
assert(index + count <= array->next);
183-
if(array_roll(array,array->next-1,index,count))
184-
return -1;
148+
149+
memmove(array->pointer + index * array->item_size,
150+
array->pointer + (index + count) * array->item_size,
151+
(array->next - index - count) * array->item_size);
152+
185153
array->next -= count;
186154
return 0;
187155
}

0 commit comments

Comments
 (0)