Skip to content

Commit 573e17f

Browse files
authored
Additional safety for zero-length R vectors (#121)
1 parent f768a13 commit 573e17f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/core.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,14 @@ SEXP nano_decode(unsigned char *buf, const size_t sz, const uint8_t mod, SEXP ho
473473
data = Rf_allocVector(RAWSXP, sz);
474474
break;
475475
case 9:
476-
data = nano_raw_char(buf, sz);
477-
return data;
476+
return nano_raw_char(buf, sz);
478477
default:
479-
data = nano_unserialize(buf, sz, hook);
480-
return data;
478+
return nano_unserialize(buf, sz, hook);
481479
}
482480

483-
memcpy(NANO_DATAPTR(data), buf, sz);
481+
if (sz)
482+
memcpy(NANO_DATAPTR(data), buf, sz);
483+
484484
return data;
485485

486486
}
@@ -633,9 +633,12 @@ SEXP rnng_marker_set(SEXP x) {
633633

634634
SEXP rnng_marker_read(SEXP x) {
635635

636-
unsigned char *buf = (unsigned char *) NANO_DATAPTR(x);
637-
638-
return Rf_ScalarLogical(TYPEOF(x) == RAWSXP && XLENGTH(x) > 12 && buf[0] == 0x7 && buf[3] == 0x1);
636+
int res = 0;
637+
if (TYPEOF(x) == RAWSXP && XLENGTH(x) > 12) {
638+
unsigned char *buf = (unsigned char *) DATAPTR_RO(x);
639+
res = buf[0] == 0x7 && buf[3] == 0x1;
640+
}
641+
return Rf_ScalarLogical(res);
639642

640643
}
641644

@@ -648,10 +651,11 @@ SEXP rnng_header_set(SEXP x) {
648651

649652
SEXP rnng_header_read(SEXP x) {
650653

651-
unsigned char *buf = (unsigned char *) NANO_DATAPTR(x);
652654
int res = 0;
653-
if (TYPEOF(x) == RAWSXP && XLENGTH(x) > 12 && buf[0] == 0x7) {
654-
memcpy(&res, buf + 4, sizeof(int));
655+
if (TYPEOF(x) == RAWSXP && XLENGTH(x) > 12) {
656+
unsigned char *buf = (unsigned char *) DATAPTR_RO(x);
657+
if (buf[0] == 0x7)
658+
memcpy(&res, buf + 4, sizeof(int));
655659
}
656660
return Rf_ScalarInteger(res);
657661

src/tls.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ SEXP rnng_random(SEXP n, SEXP convert) {
101101
out = nano_hash_char(buf, sz);
102102
} else {
103103
out = Rf_allocVector(RAWSXP, sz);
104-
memcpy(NANO_DATAPTR(out), buf, sz);
104+
if (sz)
105+
memcpy(NANO_DATAPTR(out), buf, sz);
105106
}
106107

107108
return out;

0 commit comments

Comments
 (0)