Skip to content

Commit

Permalink
lib/rand: fix bug with non uint64_t aligned random buffer fill
Browse files Browse the repository at this point in the history
Now that we honor the compression percentage, we can easily
get buffer fills that are not aligned to uint64_t. Make
sure that __fill_random_buf() handles this correctly.

Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
axboe committed Dec 5, 2014
1 parent a893c26 commit 6780906
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,26 @@ void init_rand_seed(struct frand_state *state, unsigned int seed)

void __fill_random_buf(void *buf, unsigned int len, unsigned long seed)
{
long *ptr = buf;
void *ptr = buf;

while ((void *) ptr - buf < len) {
*ptr = seed;
ptr++;
while (len) {
if (len >= sizeof(int64_t)) {
*((int64_t *) ptr) = seed;
ptr += sizeof(int64_t);
len -= sizeof(int64_t);
} else if (len >= sizeof(int32_t)) {
*((int32_t *) ptr) = seed;
ptr += sizeof(int32_t);
len -= sizeof(int32_t);
} else if (len >= sizeof(int16_t)) {
*((int16_t *) ptr) = seed;
ptr += sizeof(int16_t);
len -= sizeof(int16_t);
} else {
*((int8_t *) ptr) = seed;
ptr += sizeof(int8_t);
len -= sizeof(int8_t);
}
seed *= GOLDEN_RATIO_PRIME;
seed >>= 3;
}
Expand Down Expand Up @@ -146,10 +161,14 @@ void __fill_random_buf_percentage(unsigned long seed, void *buf,
__fill_random_buf(buf, this_len, seed);

len -= this_len;
if (!len)
break;
buf += this_len;

if (this_len > len)
this_len = len;
else if (len - this_len <= sizeof(long))
this_len = len;

if (pbytes)
fill_pattern(buf, this_len, pattern, pbytes);
Expand Down

0 comments on commit 6780906

Please sign in to comment.