Skip to content

Commit b08f27a

Browse files
committed
safer and more efficient memory loads
1 parent b64f12f commit b08f27a

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Updates
44

5+
* Safer and more efficient memory reads for 'next' serialization corrects for CRAN UBSAN-clang check errors.
56
* Upgrades bundled 'mbedtls' to v3.6.0 LTS.
67

78
# nanonext 0.13.5

README.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Or the slow channel release from CRAN:
7373
install.packages("nanonext")
7474
```
7575

76-
Or the latest development build from R-universe:
76+
Or the development build from R-universe:
7777

7878
```{r universe, eval=FALSE}
7979
install.packages("nanonext", repos = "https://shikokuchuo.r-universe.dev")

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Or the slow channel release from CRAN:
8888
install.packages("nanonext")
8989
```
9090

91-
Or the latest development build from R-universe:
91+
Or the development build from R-universe:
9292

9393
``` r
9494
install.packages("nanonext", repos = "https://shikokuchuo.r-universe.dev")
@@ -211,7 +211,7 @@ v1.7.3 and ‘libmbedtls’ v3.6.0 will be automatically compiled from the
211211
package sources during installation.
212212

213213
For previous R versions, pre-compiled ‘libnng’ v1.7.3 and ‘libmbedtls’
214-
v3.5.2 libraries are downloaded and used for installation instead.
214+
v3.6.0 libraries are downloaded and used for installation instead.
215215

216216
### Acknowledgements and Links
217217

src/core.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ void nano_serialize_next(nano_buf *buf, const SEXP object) {
201201
buf->buf[0] = 0x7;
202202
buf->buf[1] = registered;
203203
buf->buf[2] = special_bit;
204-
buf->cur += registered ? 16 : 4;
204+
205+
const int reg = (int) registered;
206+
buf->cur += reg ? 12 : 4;
205207

206208
struct R_outpstream_st output_stream;
207209

@@ -216,18 +218,18 @@ void nano_serialize_next(nano_buf *buf, const SEXP object) {
216218
NANONEXT_SERIAL_VER,
217219
NULL,
218220
nano_write_bytes,
219-
registered ? nano_inHook : NULL,
220-
registered ? CAR(nano_klassString) : R_NilValue
221+
reg ? nano_inHook : NULL,
222+
reg ? CAR(nano_klassString) : R_NilValue
221223
);
222224

223225
R_Serialize(object, &output_stream);
224226

225-
if (registered && TAG(nano_refHook) != R_NilValue) {
226-
uint64_t cursor = (uint64_t) buf->cur;
227-
memcpy(buf->buf + 8, &cursor, sizeof(uint64_t));
227+
if (reg && TAG(nano_refHook) != R_NilValue) {
228+
const uint64_t cursor = (uint64_t) buf->cur;
229+
memcpy(buf->buf + 4, &cursor, sizeof(uint64_t));
228230
SEXP call, out;
229231

230-
if (registered == 1) {
232+
if (reg == 1) {
231233

232234
PROTECT(call = Rf_lcons(CAR(nano_refHook), Rf_cons(TAG(nano_refHook), R_NilValue)));
233235
PROTECT(out = R_UnwindProtect(eval_safe, call, rl_reset, NULL, NULL));
@@ -302,7 +304,7 @@ void nano_serialize_xdr(nano_buf *buf, const SEXP object) {
302304

303305
SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
304306

305-
int registered = (int) buf[1];
307+
const int reg = (int) buf[1];
306308
uint64_t offset;
307309
size_t cur;
308310
SEXP reflist;
@@ -316,26 +318,26 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
316318
cur = 0;
317319
goto resume;
318320
case 0x7:
319-
if (registered) {
320-
offset = *(uint64_t *) (buf + 8);
321+
if (reg) {
322+
memcpy(&offset, buf + 4, sizeof(uint64_t));
321323
if (offset) {
322324
SEXP raw, call;
323-
if (registered == 1) {
325+
if (reg == 1) {
324326
PROTECT(raw = Rf_allocVector(RAWSXP, sz - offset));
325327
memcpy(STDVEC_DATAPTR(raw), buf + offset, sz - offset);
326328
PROTECT(call = Rf_lcons(CADR(nano_refHook), Rf_cons(raw, R_NilValue)));
327329
reflist = Rf_eval(call, R_GlobalEnv);
328330
SET_TAG(nano_refHook, reflist);
329331
UNPROTECT(2);
330332
} else {
331-
R_xlen_t llen = *(R_xlen_t *) (buf + offset);
333+
R_xlen_t llen, xlen;
334+
memcpy(&llen, buf + offset, sizeof(R_xlen_t));
332335
cur = offset + sizeof(R_xlen_t);
333336
PROTECT(reflist = Rf_allocVector(VECSXP, llen));
334337
SEXP out;
335338
SEXP func = CADR(nano_refHook);
336-
R_xlen_t xlen;
337339
for (R_xlen_t i = 0; i < llen; i++) {
338-
xlen = *(R_xlen_t *) (buf + cur);
340+
memcpy(&xlen, buf + cur, sizeof(R_xlen_t));
339341
cur += sizeof(R_xlen_t);
340342
PROTECT(raw = Rf_allocVector(RAWSXP, xlen));
341343
memcpy(STDVEC_DATAPTR(raw), buf + cur, xlen);
@@ -350,7 +352,7 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
350352

351353
}
352354
}
353-
cur = 16;
355+
cur = 12;
354356
goto resume;
355357
}
356358
offset = 0;

tests/tests.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ nanotesterr(transact(sess), "ncurlSession")
384384
nanotestw(is_error_value(ncurl_session("https://i")))
385385
nanotesterr(ncurl_aio("https://", tls = "wrong"), "valid TLS")
386386
nanotesterr(ncurl("https://www.cam.ac.uk/", tls = "wrong"), "valid TLS")
387+
nanotestxp(etls <- tls_config())
388+
nanotesterr(stream(dial = "wss://127.0.0.1:5555", textframes = TRUE, tls = etls))
387389
nanotesterr(stream(dial = "wss://127.0.0.1:5555"))
388390
nanotesterr(stream(dial = "errorValue3"), "argument")
389391
nanotesterr(stream(dial = "inproc://notsup"), "Not supported")
@@ -553,7 +555,3 @@ nanotestw(dial(s, url = "tls+tcp://.", tls = tls, error = FALSE) > 0)
553555
nanotestw(listen(s, url = "tls+tcp://.", tls = tls, error = FALSE) > 0)
554556
nanotestz(close(s1))
555557
nanotestz(close(s))
556-
nanotestxp(tls <- tls_config())
557-
nanotesterr(stream(dial = "wss://127.0.0.1:5555", textframes = TRUE, tls = tls))
558-
nanotestxp(s <- socket(listen = "wss://127.0.0.1:5557", tls = tls))
559-
nanotestz(close(s))

0 commit comments

Comments
 (0)