-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1665 from rstudio/readline-segfault
Fix segfault from `readline()`; avoid unnecessary string copy
- Loading branch information
Showing
3 changed files
with
18 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |