Skip to content

Commit f918c09

Browse files
committed
rawToChar version passing ASAN/UBSAN
1 parent b54fa88 commit f918c09

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/core.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,17 @@ static SEXP rawOneString(unsigned char *bytes, R_xlen_t nbytes, R_xlen_t *np) {
110110
SEXP rawToChar(unsigned char *buf, const size_t sz) {
111111

112112
SEXP out;
113-
const char *cbuf = (const char *) buf;
114-
size_t slen = strnlen(cbuf, sz);
115-
if (sz - slen > 1) {
113+
int i, j;
114+
for (i = 0, j = -1; i < sz; i++) if (buf[i]) j = i; else break;
115+
if (sz - i > 1) {
116116
Rf_warning("data could not be converted to a character string");
117117
out = Rf_allocVector(RAWSXP, sz);
118118
memcpy(STDVEC_DATAPTR(out), buf, sz);
119119
return out;
120120
}
121121

122122
PROTECT(out = Rf_allocVector(STRSXP, 1));
123-
SET_STRING_ELT(out, 0, Rf_mkCharLenCE(cbuf, slen, CE_NATIVE));
123+
SET_STRING_ELT(out, 0, Rf_mkCharLenCE((const char *) buf, j + 1, CE_NATIVE));
124124

125125
UNPROTECT(1);
126126
return out;

0 commit comments

Comments
 (0)