Skip to content

Commit

Permalink
Fix passing long environment variables to the debugger (#87)
Browse files Browse the repository at this point in the history
Replace the command buffer with a std::string to support reading
arbitrary command line lengths. This is required in projects like
GStreamer that setup long environment variables like GST_PLUGIN_PATH
with several directories on it.

microsoft/vscode-cpptools#8411
microsoft/vscode-cpptools#6874
  • Loading branch information
ylatuya committed Oct 17, 2023
1 parent 50b5630 commit 4fe9c66
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
41 changes: 12 additions & 29 deletions src/MICmnStreamStdin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifdef _WIN32
#include "Platform.h"
#endif
#include <iostream>
#include <string.h>

// In-house headers:
Expand All @@ -28,7 +29,7 @@
// Throws: None.
//--
CMICmnStreamStdin::CMICmnStreamStdin()
: m_strPromptCurrent("(gdb)"), m_bShowPrompt(true), m_pCmdBuffer(nullptr) {}
: m_strPromptCurrent("(gdb)"), m_bShowPrompt(true) {}

//++
// Details: CMICmnStreamStdin destructor.
Expand Down Expand Up @@ -61,9 +62,7 @@ bool CMICmnStreamStdin::Initialize() {
MI::ModuleInit<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
MI::ModuleInit<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);

if (bOk) {
m_pCmdBuffer = new char[m_constBufferSize];
} else {
if (!bOk) {
CMIUtilString strInitError(CMIUtilString::Format(
MIRSRC(IDS_MI_INIT_ERR_STREAMSTDIN), errMsg.c_str()));
SetErrorDescription(strInitError);
Expand Down Expand Up @@ -94,11 +93,6 @@ bool CMICmnStreamStdin::Shutdown() {

ClrErrorDescription();

if (m_pCmdBuffer != nullptr) {
delete[] m_pCmdBuffer;
m_pCmdBuffer = nullptr;
}

bool bOk = MIstatus::success;
CMIUtilString errMsg;

Expand Down Expand Up @@ -186,33 +180,22 @@ bool CMICmnStreamStdin::GetEnablePrompt() const { return m_bShowPrompt; }
const char *CMICmnStreamStdin::ReadLine(CMIUtilString &vwErrMsg) {
vwErrMsg.clear();

// Read user input
const char *pText = ::fgets(&m_pCmdBuffer[0], m_constBufferSize, stdin);
if (pText == nullptr) {
std::getline(std::cin, m_pCmdString);

if (std::cin.eof()) {
#ifdef _MSC_VER
// Was Ctrl-C hit?
// On Windows, Ctrl-C gives an ERROR_OPERATION_ABORTED as error on the
// command-line.
// The end-of-file indicator is also set, so without this check we will exit
// next if statement.
// command-line and the end-of-file indicator is also set.
if (::GetLastError() == ERROR_OPERATION_ABORTED)
return nullptr;
#endif
if (::feof(stdin)) {
const bool bForceExit = true;
CMIDriver::Instance().SetExitApplicationFlag(bForceExit);
} else if (::ferror(stdin) != 0)
vwErrMsg = ::strerror(errno);
const bool bForceExit = true;
CMIDriver::Instance().SetExitApplicationFlag(bForceExit);
} else if (std::cin.fail()) {
vwErrMsg = ::strerror(errno);
return nullptr;
}

// Strip off new line characters
for (char *pI = m_pCmdBuffer; *pI != '\0'; pI++) {
if ((*pI == '\n') || (*pI == '\r')) {
*pI = '\0';
break;
}
}

return pText;
return m_pCmdString.c_str();
}
3 changes: 1 addition & 2 deletions src/MICmnStreamStdin.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ class CMICmnStreamStdin : public CMICmnBase,
CMIUtilString m_strPromptCurrent; // Command line prompt as shown to the user
bool m_bShowPrompt; // True = Yes prompt is shown/output to the user (stdout),
// false = no prompt
static const int m_constBufferSize = 2048;
char *m_pCmdBuffer;
std::string m_pCmdString;
};

0 comments on commit 4fe9c66

Please sign in to comment.