Skip to content

Rcpp::String silently drops embedded NUL bytes #916

@kevinushey

Description

@kevinushey

For example:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
SEXP embeddedNul() {
  std::string hasNullByte("abc\0abc", 7);
  String converted(hasNullByte);
  return wrap(converted);
}

/*** R
embeddedNul()
*/

Sourcing this gives me:

> embeddedNul()
[1] "abc"

I would hope for either:

  1. An error, since we shouldn't silently lose data following the NUL byte; or
  2. A conversion to e.g. a raw character vector, preserving the embedded NUL.

It's worth stating that attempting to do this on the R side would give an error, e.g.

data <- as.raw(c(97, 98, 99, 0, 97, 98, 99))
rawToChar(data)

giving

> data <- as.raw(c(97, 98, 99, 0, 97, 98, 99))
> data
[1] 61 62 63 00 61 62 63
> rawToChar(data)
Error in rawToChar(data) : embedded nul in string: 'abc\0abc'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions