From 5662dd1f1c7c1ae6eb3e7ec1cf48e195bc0a484d Mon Sep 17 00:00:00 2001 From: Andoni Morales Alastruey Date: Tue, 23 Nov 2021 16:39:06 +0100 Subject: [PATCH] Fix passing long environment variables to the debugger 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. https://github.com/microsoft/vscode-cpptools/issues/8411 https://github.com/microsoft/vscode-cpptools/issues/6874 --- src/MICmnStreamStdin.cpp | 41 ++++++++++++---------------------------- src/MICmnStreamStdin.h | 3 +-- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/MICmnStreamStdin.cpp b/src/MICmnStreamStdin.cpp index 84d67ca..a0a1afc 100644 --- a/src/MICmnStreamStdin.cpp +++ b/src/MICmnStreamStdin.cpp @@ -10,6 +10,7 @@ #ifdef _WIN32 #include "Platform.h" #endif +#include #include // In-house headers: @@ -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. @@ -61,9 +62,7 @@ bool CMICmnStreamStdin::Initialize() { MI::ModuleInit(IDS_MI_INIT_ERR_LOG, bOk, errMsg); MI::ModuleInit(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); @@ -94,11 +93,6 @@ bool CMICmnStreamStdin::Shutdown() { ClrErrorDescription(); - if (m_pCmdBuffer != nullptr) { - delete[] m_pCmdBuffer; - m_pCmdBuffer = nullptr; - } - bool bOk = MIstatus::success; CMIUtilString errMsg; @@ -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(); } diff --git a/src/MICmnStreamStdin.h b/src/MICmnStreamStdin.h index b193757..866e563 100644 --- a/src/MICmnStreamStdin.h +++ b/src/MICmnStreamStdin.h @@ -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; };