Skip to content

Commit

Permalink
simplify random() errors
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 1, 2024
1 parent bf9228c commit 912b141
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,21 +760,24 @@ SEXP rnng_random(SEXP n, SEXP convert) {

SEXP out;
unsigned char buf[sz];
char errbuf[1024];
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
const char *pers = "r-nanonext-rng";

mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg);

if ((xc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen(pers))) ||
(xc = mbedtls_ctr_drbg_random(&ctr_drbg, buf, sz)))
goto exitlevel1;
xc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen(pers));
if (xc == 0) {
xc = mbedtls_ctr_drbg_random(&ctr_drbg, buf, sz);
}

mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);

if (xc)
Rf_error("error generating random bytes");

if (*(int *) STDVEC_DATAPTR(convert)) {
out = nano_hashToChar(buf, sz);
} else {
Expand All @@ -784,10 +787,4 @@ SEXP rnng_random(SEXP n, SEXP convert) {

return out;

exitlevel1:
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
mbedtls_strerror(xc, errbuf, sizeof(errbuf));
Rf_error("%d | %s", xc, errbuf);

}

0 comments on commit 912b141

Please sign in to comment.