Skip to content

Commit

Permalink
make safe random() and tls_config()
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Dec 30, 2023
1 parent 078f7e3 commit 57ddda4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: nanonext
Type: Package
Title: NNG (Nanomsg Next Gen) Lightweight Messaging Library
Version: 0.11.0.9001
Version: 0.11.0.9002
Description: R binding for NNG (Nanomsg Next Gen), a successor to ZeroMQ. NNG is
a socket library implementing 'Scalability Protocols', a reliable,
high-performance standard for common communications patterns including
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# nanonext 0.11.0.9001 (development)
# nanonext 0.11.0.9002 (development)

#### New Features

Expand Down
3 changes: 3 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ msleep <- function(time) invisible(.Call(rnng_sleep, time))
#' @details If 'n' is non-integer, it will be coerced to integer; if a vector,
#' only the first element will be used.
#'
#' The maximum value of 'n' is limited by the entropy collected and too
#' large a value will result in an insufficient buffer error.
#'
#' @note Results obtained are independent of and do not alter the state of R's
#' own pseudo-random number generators.
#'
Expand Down
3 changes: 3 additions & 0 deletions man/random.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static SEXP nano_inHook(SEXP x, SEXP fun) {

static SEXP nano_outHook(SEXP x, SEXP fun) {

const long int i = atol(CHAR(STRING_ELT(x, 0))) - 1;
const long int i = atol(CHAR(*(SEXP *) STDVEC_DATAPTR(x))) - 1;

return VECTOR_ELT(fun, i);

Expand Down Expand Up @@ -1347,8 +1347,8 @@ SEXP rnng_stats_get(SEXP object, SEXP stat) {
SEXP rnng_strcat(SEXP a, SEXP b) {

SEXP out;
const char *ap = CHAR(STRING_ELT(a, 0));
const char *bp = CHAR(STRING_ELT(b, 0));
const char *ap = CHAR(*(SEXP *) STDVEC_DATAPTR(a));
const char *bp = CHAR(*(SEXP *) STDVEC_DATAPTR(b));
const size_t alen = strlen(ap);
const size_t blen = strlen(bp);

Expand Down
23 changes: 13 additions & 10 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,17 @@ SEXP rnng_status_code(SEXP x) {

SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {

const nng_tls_auth_mode mod = LOGICAL(auth)[0] ? NNG_TLS_AUTH_MODE_REQUIRED : NNG_TLS_AUTH_MODE_OPTIONAL;
const nng_tls_auth_mode mod = *(int *) STDVEC_DATAPTR(auth) ? NNG_TLS_AUTH_MODE_REQUIRED : NNG_TLS_AUTH_MODE_OPTIONAL;
R_xlen_t usefile;
nng_tls_config *cfg;
int xc;
const char *crl, *file, *key, *pss;
SEXP xp;

if ((usefile = Rf_xlength(client)) > 0) {
const char *file = CHAR(STRING_ELT(client, 0));
file = CHAR(STRING_ELT(client, 0));
if (usefile > 1)
crl = CHAR(STRING_ELT(client, 1));
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_CLIENT)))
goto exitlevel1;
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
Expand All @@ -684,14 +687,15 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
if ((xc = nng_tls_config_ca_file(cfg, file)))
goto exitlevel2;
} else {
const char *crl = CHAR(STRING_ELT(client, 1));
if ((xc = nng_tls_config_ca_chain(cfg, file, strncmp(crl, "", 1) ? crl : NULL)))
goto exitlevel2;
}

} else if ((usefile = Rf_xlength(server)) > 0) {
const char *file = CHAR(STRING_ELT(server, 0));
const char *pss = pass != R_NilValue ? CHAR(STRING_ELT(pass, 0)) : NULL;
file = CHAR(STRING_ELT(server, 0));
pss = pass != R_NilValue ? CHAR(STRING_ELT(pass, 0)) : NULL;
if (usefile > 1)
key = CHAR(STRING_ELT(server, 1));
if ((xc = nng_tls_config_alloc(&cfg, NNG_TLS_MODE_SERVER)))
goto exitlevel1;
if ((xc = nng_tls_config_auth_mode(cfg, mod)))
Expand All @@ -702,7 +706,6 @@ SEXP rnng_tls_config(SEXP client, SEXP server, SEXP pass, SEXP auth) {
if ((xc = nng_tls_config_cert_key_file(cfg, file, pss)))
goto exitlevel2;
} else {
const char *key = CHAR(STRING_ELT(server, 1));
if ((xc = nng_tls_config_own_cert(cfg, file, key, pss)))
goto exitlevel2;
}
Expand Down Expand Up @@ -747,12 +750,12 @@ SEXP rnng_random(SEXP n, SEXP convert) {
case INTSXP:
case LGLSXP:
sz = INTEGER(n)[0];
break;
if (sz >= 0) break;
case REALSXP:
sz = Rf_asInteger(n);
break;
if (sz >= 0) break;
default:
Rf_error("'n' must be integer or coercible to integer");
Rf_error("'n' must be a non-negative integer or coercible to such");
}

SEXP out;
Expand All @@ -772,7 +775,7 @@ SEXP rnng_random(SEXP n, SEXP convert) {
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);

if (LOGICAL(convert)[0]) {
if (*(int *) STDVEC_DATAPTR(convert)) {
out = nano_hashToChar(buf, sz);
} else {
out = Rf_allocVector(RAWSXP, sz);
Expand Down

0 comments on commit 57ddda4

Please sign in to comment.