Skip to content

Commit

Permalink
use 'Rf_mkCharLenCE() as appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Oct 25, 2018
1 parent a669a19 commit 8c26ace
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2018-10-25 Kevin Ushey <kevinushey@gmail.com>

* inst/include/Rcpp/String.h: Use Rf_mkCharLenCE() as appropriate
* inst/unitTests/cpp/String.cpp: Add unit tests
* inst/unitTests/runit.String.R: Add unit tests

2018-10-12 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Date, Version): Roll minor version
Expand Down
2 changes: 2 additions & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
\item The constructor for \code{NumericMatrix(not_init(n,k))} was
corrected (Romain in \ghpr{904}, Dirk in \ghpr{905}, and also
Romain in \ghpr{908} fixing \ghpr{907}).
\item Rcpp::String no longer silently drops embedded NUL bytes
in strings -- instead, an R error is signaled. (\ghit{916})
}
\item Changes in Rcpp Deployment:
\itemize{
Expand Down
11 changes: 7 additions & 4 deletions inst/include/Rcpp/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ namespace Rcpp {

inline SEXP get_sexp() const {
RCPP_STRING_DEBUG_1("String::get_sexp const (valid = %d) ", valid);
return valid ? data : Rf_mkCharCE(buffer.c_str(), enc);
return valid ? data : Rf_mkCharLenCE(buffer.c_str(), buffer.size(), enc);
}

inline SEXP get_sexp() {
Expand Down Expand Up @@ -395,9 +395,12 @@ namespace Rcpp {
enc = encoding;

if (valid) {
data = Rcpp_ReplaceObject(data, Rf_mkCharCE(Rf_translateCharUTF8(data), encoding));
const void* vmax = vmaxget();
const char* translated = Rf_translateCharUTF8(data);
data = Rcpp_ReplaceObject(data, Rf_mkCharCE(translated, encoding));
vmaxset(vmax);
} else {
data = Rf_mkCharCE(buffer.c_str(), encoding);
data = Rf_mkCharLenCE(buffer.c_str(), buffer.size(), encoding);
Rcpp_PreserveObject(data);
valid = true;
}
Expand Down Expand Up @@ -469,7 +472,7 @@ namespace Rcpp {
inline void setData() {
RCPP_STRING_DEBUG("setData");
if (!valid) {
data = Rf_mkCharCE(buffer.c_str(), enc);
data = Rf_mkCharLenCE(buffer.c_str(), buffer.size(), enc);
Rcpp_PreserveObject(data);
valid = true;
}
Expand Down
6 changes: 6 additions & 0 deletions inst/unitTests/cpp/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,9 @@ String test_String_ctor_encoding2() {
y.set_encoding(CE_UTF8);
return y;
}

// [[Rcpp::export]]
String test_String_embeddedNul() {
std::string bad("abc\0abc", 7);
return String(bad);
}
4 changes: 4 additions & 0 deletions inst/unitTests/runit.String.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,8 @@ if (.runThisTest) {
checkEquals(Encoding(test_String_ctor_encoding2()), "UTF-8")
}

test.String.embeddedNul <- function() {
checkException(test_String_embeddedNul())
}

}

0 comments on commit 8c26ace

Please sign in to comment.