Skip to content

Commit

Permalink
Fix passing long environment variables to the debugger
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 Sep 27, 2023
1 parent 50b5630 commit b82a491
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
37 changes: 10 additions & 27 deletions src/MICmnStreamStdin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Platform.h"
#endif
#include <string.h>
#include <iostream>

// In-house headers:
#include "MICmnLog.h"
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)
} 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 nullptr;
}

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 b82a491

Please sign in to comment.