star-sp

Random number generators and distributions
git clone git://git.meso-star.fr/star-sp.git
Log | Files | Refs | README | LICENSE

commit f88cad538db924b7f3a1d0db26c83b80c15efd6d
parent 3fe8e5bb0f18a1220bad5a0056649ebe0415af28
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue,  1 Dec 2020 09:42:08 +0100

Fix the rng_kiss_read function

Deserialized data were wrong if data to read exceed the initial
temporary buffer capacity.

Diffstat:
Msrc/ssp_rng.c | 10+++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/ssp_rng.c b/src/ssp_rng.c @@ -107,18 +107,22 @@ rng_kiss_read(void* data, FILE* file) { struct rng_kiss* rng = (struct rng_kiss*)data; struct darray_char buf; + size_t offset = 0; + size_t bufsz = 64; res_T res = RES_OK; int err; ASSERT(data && file); darray_char_init(&mem_default_allocator, &buf); - res = darray_char_resize(&buf, 64); + res = darray_char_resize(&buf, bufsz); if(res != RES_OK) goto error; - while(fgets(darray_char_data_get(&buf), (int)darray_char_size_get(&buf), file)) { + while(fgets(darray_char_data_get(&buf)+offset, (int)(bufsz-offset), file)) { /* Ensure that the whole line is read */ if(strrchr(darray_char_cdata_get(&buf), '\n')) break; - res = darray_char_resize(&buf, darray_char_size_get(&buf) * 2); + offset = bufsz-1/*Null char*/; + bufsz *= 2; + res = darray_char_resize(&buf, bufsz); if(res != RES_OK) goto error; }