Skip to content

Commit 4fe9c66

Browse files
authored
Fix passing long environment variables to the debugger (#87)
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
1 parent 50b5630 commit 4fe9c66

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

src/MICmnStreamStdin.cpp

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifdef _WIN32
1111
#include "Platform.h"
1212
#endif
13+
#include <iostream>
1314
#include <string.h>
1415

1516
// In-house headers:
@@ -28,7 +29,7 @@
2829
// Throws: None.
2930
//--
3031
CMICmnStreamStdin::CMICmnStreamStdin()
31-
: m_strPromptCurrent("(gdb)"), m_bShowPrompt(true), m_pCmdBuffer(nullptr) {}
32+
: m_strPromptCurrent("(gdb)"), m_bShowPrompt(true) {}
3233

3334
//++
3435
// Details: CMICmnStreamStdin destructor.
@@ -61,9 +62,7 @@ bool CMICmnStreamStdin::Initialize() {
6162
MI::ModuleInit<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
6263
MI::ModuleInit<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
6364

64-
if (bOk) {
65-
m_pCmdBuffer = new char[m_constBufferSize];
66-
} else {
65+
if (!bOk) {
6766
CMIUtilString strInitError(CMIUtilString::Format(
6867
MIRSRC(IDS_MI_INIT_ERR_STREAMSTDIN), errMsg.c_str()));
6968
SetErrorDescription(strInitError);
@@ -94,11 +93,6 @@ bool CMICmnStreamStdin::Shutdown() {
9493

9594
ClrErrorDescription();
9695

97-
if (m_pCmdBuffer != nullptr) {
98-
delete[] m_pCmdBuffer;
99-
m_pCmdBuffer = nullptr;
100-
}
101-
10296
bool bOk = MIstatus::success;
10397
CMIUtilString errMsg;
10498

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

189-
// Read user input
190-
const char *pText = ::fgets(&m_pCmdBuffer[0], m_constBufferSize, stdin);
191-
if (pText == nullptr) {
183+
std::getline(std::cin, m_pCmdString);
184+
185+
if (std::cin.eof()) {
192186
#ifdef _MSC_VER
193187
// Was Ctrl-C hit?
194188
// On Windows, Ctrl-C gives an ERROR_OPERATION_ABORTED as error on the
195-
// command-line.
196-
// The end-of-file indicator is also set, so without this check we will exit
197-
// next if statement.
189+
// command-line and the end-of-file indicator is also set.
198190
if (::GetLastError() == ERROR_OPERATION_ABORTED)
199191
return nullptr;
200192
#endif
201-
if (::feof(stdin)) {
202-
const bool bForceExit = true;
203-
CMIDriver::Instance().SetExitApplicationFlag(bForceExit);
204-
} else if (::ferror(stdin) != 0)
205-
vwErrMsg = ::strerror(errno);
193+
const bool bForceExit = true;
194+
CMIDriver::Instance().SetExitApplicationFlag(bForceExit);
195+
} else if (std::cin.fail()) {
196+
vwErrMsg = ::strerror(errno);
206197
return nullptr;
207198
}
208199

209-
// Strip off new line characters
210-
for (char *pI = m_pCmdBuffer; *pI != '\0'; pI++) {
211-
if ((*pI == '\n') || (*pI == '\r')) {
212-
*pI = '\0';
213-
break;
214-
}
215-
}
216-
217-
return pText;
200+
return m_pCmdString.c_str();
218201
}

src/MICmnStreamStdin.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,5 @@ class CMICmnStreamStdin : public CMICmnBase,
5555
CMIUtilString m_strPromptCurrent; // Command line prompt as shown to the user
5656
bool m_bShowPrompt; // True = Yes prompt is shown/output to the user (stdout),
5757
// false = no prompt
58-
static const int m_constBufferSize = 2048;
59-
char *m_pCmdBuffer;
58+
std::string m_pCmdString;
6059
};

0 commit comments

Comments
 (0)