Skip to content

Commit 1d93521

Browse files
committed
RC7; base64 error messages, aio error code
1 parent 6c7ec3f commit 1d93521

File tree

4 files changed

+26
-41
lines changed

4 files changed

+26
-41
lines changed

NEWS.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@
88

99
*Please review the following potentially breaking changes, and only update when ready:*
1010

11+
* The package now ensures stability of return types:
12+
+ `socket()` and `stream()` will now error rather than return an 'errorValue'. The error value is included in the error message.
13+
+ `send_aio()` and `recv_aio()` now always return an integer 'errorValue' at `$result` and `$data` respectively.
14+
+ `recv()` and `recv_aio()` now return an integer 'errorValue' at each of `$raw` and `$data` when 'keep.raw' is set to TRUE.
15+
+ `ncurl()` now returns an integer 'errorValue' at each of `$status`, `$headers`, `$raw` and `$data` for both sync and async. Where redirects are not followed, the address is now returned as a character string at `$data`.
1116
* For functions that send and receive messages i.e. `send()`, `send_aio()`, `recv()`, `recv_aio()` and `ncurl()`, 'errorValues' are now returned silently without an accompanying warning. Use `is_error_value()` to explicitly check for errors.
12-
* Functions `socket()` and `stream()` will now error instead of returning an 'errorValue' to ensure stability of return types. The error value is included in the error message.
1317
* `nano_init()` is deprecated due to the above change in behaviour.
1418
* `send()` no longer has a '...' argument. This has had no effect since 0.6.0, but will now error if additional arguments are provided (please check and remove previous uses of the argument 'echo'). Also no longer returns invisibly for consistency with `recv()`.
15-
* `recv()` and `recv_aio()` now return an integer 'errorValue' at each of `$raw` and `$data` when 'keep.raw' is set to TRUE, ensuring stability of return types.
16-
* `ncurl()` now returns an integer 'errorValue' at each of `$status`, `$headers`, `$raw` and `$data` for both sync and async, ensuring stability of return types. Where 'follow' is set to FALSE, or for all async requests, redirect addresses are now returned as a character string at `$data`.
17-
* `nano()` now creates a nano object with method `$context_open()` for applicable protocols. Opening a context will attach a context at `$context` and a `$context_close()` method. When a context is active, all object methods apply to the context instead of the socket. Method `$socket_setopt()` renamed to `$setopt()` as it can be used on the socket or active context as applicable.
1819
* `listen()` and `dial()` now only take a socket as argument; for nano objects, the `$listen()` and `$dial()` methods must be used instead.
20+
* `nano()` now creates a nano object with method `$context_open()` for applicable protocols. Opening a context will attach a context at `$context` and a `$context_close()` method. When a context is active, all object methods apply to the context instead of the socket. Method `$socket_setopt()` renamed to `$setopt()` as it can be used on the socket or active context as applicable.
1921
* Non-logical values supplied to logical arguments will now error: this is documented for each function where this is applicable.
2022

2123
*Other changes:*
2224

2325
* Fixes bug introduced in 0.6.0 where Aios returning 'errorValues' are not cached with the class, returning only integer values when accessed subsequently.
24-
* Fixes potential crash when `base64dec()` encounters invalid input data.
26+
* Fixes potential crash when `base64dec()` encounters invalid input data. Error messages have been revised to be more accurate.
2527
* Fixes the `$` method for 'recvAio' objects for when the object has been stopped using `stop_aio()`.
2628
* Using the `$listen()` or `$dial()` methods of a nano object specifying 'autostart = FALSE' now attaches the `$listener_start()` or `$dialer_start()` method for the most recently added listener/dialer.
2729
* `device()` no longer prompts for confirmation in interactive environments - as device creation is only successful when binding 2 raw mode sockets, there is little scope for accidental use.
@@ -31,8 +33,6 @@
3133

3234
# nanonext 0.6.0
3335

34-
*The nanonext 0.6 series incorporates significant advances in performance and stability over previous releases*
35-
3636
#### New Features
3737

3838
* Implements `base64enc()` and `base64dec()` base64 encoding and decoding using the 'Mbed TLS' library.
@@ -116,8 +116,6 @@
116116

117117
# nanonext 0.5.0
118118

119-
*nanonext is now considered substantially feature-complete and API-stable*
120-
121119
#### New Features
122120

123121
* `$context()` method added for creating new contexts from nano Objects using supported protocols (i.e. req, rep, sub, surveyor, respondent) - this replaces the `context()` function for nano Objects.

src/aio.c

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,23 +184,10 @@ static SEXP mk_error_haio(const int xc, SEXP env) {
184184

185185
}
186186

187-
static SEXP mk_error_data(const int xc) {
187+
static SEXP mk_error_list(const int xc, const int data) {
188188

189189
SEXP out, err;
190-
const char *names[] = {"data", ""};
191-
PROTECT(out = Rf_mkNamed(VECSXP, names));
192-
err = Rf_ScalarInteger(xc);
193-
Rf_classgets(err, nano_error);
194-
SET_VECTOR_ELT(out, 0, err);
195-
UNPROTECT(1);
196-
return out;
197-
198-
}
199-
200-
static SEXP mk_error_result(const int xc) {
201-
202-
SEXP out, err;
203-
const char *names[] = {"result", ""};
190+
const char *names[] = {data ? "data" : "result", ""};
204191
PROTECT(out = Rf_mkNamed(VECSXP, names));
205192
err = Rf_ScalarInteger(xc);
206193
Rf_classgets(err, nano_error);
@@ -440,13 +427,13 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
440427

441428
if ((xc = nng_msg_alloc(&msg, 0))) {
442429
R_Free(saio);
443-
return mk_error_result(xc);
430+
return mk_error_list(xc, 0);
444431
}
445432
if ((xc = nng_msg_append(msg, dp, xlen)) ||
446433
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
447434
nng_msg_free(msg);
448435
R_Free(saio);
449-
return mk_error_result(xc);
436+
return mk_error_list(xc, 0);
450437
}
451438

452439
nng_aio_set_msg(saio->aio, msg);
@@ -472,14 +459,14 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
472459

473460
if ((xc = nng_msg_alloc(&msg, 0))) {
474461
R_Free(saio);
475-
return mk_error_result(xc);
462+
return mk_error_list(xc, 0);
476463
}
477464

478465
if ((xc = nng_msg_append(msg, dp, xlen)) ||
479466
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
480467
nng_msg_free(msg);
481468
R_Free(saio);
482-
return mk_error_result(xc);
469+
return mk_error_list(xc, 0);
483470
}
484471

485472
nng_aio_set_msg(saio->aio, msg);
@@ -509,14 +496,14 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {
509496
if ((xc = nng_aio_alloc(&iaio->aio, iaio_complete, iaio))) {
510497
R_Free(iov);
511498
R_Free(iaio);
512-
return mk_error_result(xc);
499+
return mk_error_list(xc, 0);
513500
}
514501

515502
if ((xc = nng_aio_set_iov(iaio->aio, 1u, iov))) {
516503
nng_aio_free(iaio->aio);
517504
R_Free(iov);
518505
R_Free(iaio);
519-
return mk_error_result(xc);
506+
return mk_error_list(xc, 0);
520507
}
521508

522509
nng_aio_set_timeout(iaio->aio, dur);
@@ -571,7 +558,7 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP keep, SEXP bytes, SEX
571558

572559
if ((xc = nng_aio_alloc(&raio->aio, raio_complete, raio))) {
573560
R_Free(raio);
574-
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
561+
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
575562
}
576563

577564
nng_aio_set_timeout(raio->aio, dur);
@@ -592,7 +579,7 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP keep, SEXP bytes, SEX
592579

593580
if ((xc = nng_aio_alloc(&raio->aio, raio_complete, raio))) {
594581
R_Free(raio);
595-
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
582+
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
596583
}
597584

598585
nng_aio_set_timeout(raio->aio, dur);
@@ -619,15 +606,15 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP keep, SEXP bytes, SEX
619606
R_Free(iov->iov_buf);
620607
R_Free(iov);
621608
R_Free(iaio);
622-
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
609+
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
623610
}
624611

625612
if ((xc = nng_aio_set_iov(iaio->aio, 1u, iov))) {
626613
nng_aio_free(iaio->aio);
627614
R_Free(iov->iov_buf);
628615
R_Free(iov);
629616
R_Free(iaio);
630-
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
617+
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
631618
}
632619

633620
nng_aio_set_timeout(iaio->aio, dur);
@@ -918,14 +905,14 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
918905

919906
if ((xc = nng_msg_alloc(&msg, 0))) {
920907
R_Free(saio);
921-
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
908+
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
922909
}
923910

924911
if ((xc = nng_msg_append(msg, dp, xlen)) ||
925912
(xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
926913
nng_msg_free(msg);
927914
R_Free(saio);
928-
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
915+
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
929916
}
930917

931918
nng_aio_set_msg(saio->aio, msg);
@@ -942,7 +929,7 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
942929

943930
if ((xc = nng_aio_alloc(&raio->aio, raio_complete, raio))) {
944931
R_Free(raio);
945-
return kpr ? mk_error_recv(xc) : mk_error_data(xc);
932+
return kpr ? mk_error_recv(xc) : mk_error_list(xc, 1);
946933
}
947934

948935
nng_aio_set_timeout(raio->aio, dur);

src/tls.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ SEXP rnng_base64enc(SEXP x, SEXP convert) {
320320
unsigned char output[olen];
321321
xc = mbedtls_base64_encode(output, olen, &olen, hash.buf, hash.sz);
322322
if (xc)
323-
Rf_error("invalid input");
323+
Rf_error("write buffer insufficient");
324324

325325
if (LOGICAL(convert)[0]) {
326326

@@ -351,11 +351,11 @@ SEXP rnng_base64dec(SEXP x, SEXP convert) {
351351

352352
xc = mbedtls_base64_decode(NULL, 0, &olen, hash.buf, hash.sz);
353353
if (xc == MBEDTLS_ERR_BASE64_INVALID_CHARACTER)
354-
Rf_error("invalid input");
354+
Rf_error("input is not valid base64");
355355
unsigned char output[olen];
356356
xc = mbedtls_base64_decode(output, olen, &olen, hash.buf, hash.sz);
357357
if (xc)
358-
Rf_error("invalid input");
358+
Rf_error("write buffer insufficient");
359359

360360
if (LOGICAL(convert)[0]) {
361361

tests/tests.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,5 @@ nanotest(base64enc("test") == "dGVzdA==")
329329
nanotest(base64dec(base64enc("test")) == "test")
330330
nanotest(is.raw(base64enc(data.frame(), convert = FALSE)))
331331
nanotest(is.integer(unserialize(base64dec(base64enc(c(1L, 2L)), convert = FALSE))))
332-
nanotesterr(base64dec("__"), "invalid input")
332+
nanotesterr(base64dec("__"), "not valid base64")
333333

0 commit comments

Comments
 (0)