Skip to content

Commit

Permalink
Fix asserts & short-read bug in isaac_seed (#2870)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblum committed Jul 25, 2012
1 parent 4378e7e commit 60d682b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/rt/rust_builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ rand_seed() {
rust_vec *v = (rust_vec *) task->kernel->malloc(vec_size<uint8_t>(size),
"rand_seed");
v->fill = v->alloc = size;
isaac_seed(task->kernel, (uint8_t*) &v->data);
isaac_seed(task->kernel, (uint8_t*) &v->data, size);
return v;
}

Expand Down
15 changes: 10 additions & 5 deletions src/rt/rust_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ inline size_t get_box_size(size_t body_size, size_t body_align) {

// Initialization helpers for ISAAC RNG

inline void isaac_seed(rust_kernel* kernel, uint8_t* dest)
inline void isaac_seed(rust_kernel* kernel, uint8_t* dest, size_t size)
{
size_t size = sizeof(ub4) * RANDSIZ;
#ifdef __WIN32__
HCRYPTPROV hProv;
kernel->win32_require
Expand All @@ -144,8 +143,14 @@ inline void isaac_seed(rust_kernel* kernel, uint8_t* dest)
#else
int fd = open("/dev/urandom", O_RDONLY);
assert(fd > 0);
assert(read(fd, dest, size) == (int) size);
assert(close(fd) == 0);
size_t amount = 0;
do {
ssize_t ret = read(fd, dest+amount, size-amount);
assert(ret >= 0);
amount += (size_t)ret;
} while (amount < size);
int ret = close(fd);
assert(ret == 0);
#endif
}

Expand All @@ -167,7 +172,7 @@ isaac_init(rust_kernel *kernel, randctx *rctx, rust_vec_box* user_seed)
seed = (seed + 0x7ed55d16) + (seed << 12);
}
} else {
isaac_seed(kernel, (uint8_t*) &rctx->randrsl);
isaac_seed(kernel, (uint8_t*) &rctx->randrsl, sizeof(rctx->randrsl));
}

randinit(rctx, 1);
Expand Down

0 comments on commit 60d682b

Please sign in to comment.