Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault from readline(); avoid unnecessary string copy #1665

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

- Fixed segfault encountered when running Python finalizer (#1663, #1664)

- Fixed segfault encountered in RStudio when rapidly switching
between R and Python chunks in a Quarto document (#1665).

# reticulate 1.39.0

- Python background threads can now run in parallel with the R session (#1641).
Expand Down
4 changes: 2 additions & 2 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,12 @@ BEGIN_RCPP
END_RCPP
}
// readline
SEXP readline(const std::string& prompt);
SEXP readline(const char* prompt);
RcppExport SEXP _reticulate_readline(SEXP promptSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const std::string& >::type prompt(promptSEXP);
Rcpp::traits::input_parameter< const char* >::type prompt(promptSEXP);
rcpp_result_gen = Rcpp::wrap(readline(prompt));
return rcpp_result_gen;
END_RCPP
Expand Down
27 changes: 13 additions & 14 deletions src/readline.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@

#include <R.h>
#include <Rinternals.h>

#include <string>
#include <cstring> // for strlen, strchr
#define READLINE_BUFFER_SIZE (8192)
extern "C" int R_ReadConsole(const char*, unsigned char*, int, int);

// [[Rcpp::export]]
SEXP readline(const std::string& prompt)
SEXP readline(const char* prompt)
{
// read user input (ensure null termination)
char buffer[READLINE_BUFFER_SIZE];
R_ReadConsole(prompt.c_str(), (unsigned char*) buffer, READLINE_BUFFER_SIZE, 1);
buffer[READLINE_BUFFER_SIZE - 1] = '\0';
if (R_ReadConsole(prompt, (unsigned char*) buffer, READLINE_BUFFER_SIZE, 1) == 0)
return R_NilValue;

// construct resulting string
std::string result(buffer, buffer + strlen(buffer));
buffer[READLINE_BUFFER_SIZE - 1] = '\0'; // Ensure null termination

// truncate to location of inserted newline. if not found, assume
// the user canceled input with e.g. R_EOF
std::string::size_type index = result.find('\n');
if (index == std::string::npos)
return R_NilValue;
// Find the location of the newline character, if any
char* newline_pos = strchr(buffer, '\n');
if (newline_pos == nullptr)
return R_NilValue; // If no newline found, assume user canceled

// Determine length up to the newline (excluding the trailing newline)
size_t input_length = newline_pos - buffer;

// return result (leaving out trailing newline)
SEXP resultSEXP = PROTECT(Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(resultSEXP, 0, Rf_mkCharLen(buffer, index));
SET_STRING_ELT(resultSEXP, 0, Rf_mkCharLen(buffer, input_length));
UNPROTECT(1);
return resultSEXP;
}
Loading