This is a fairly new issue that should be accountable fairly quickly ...
Basically, after the first call to Printf in echo, the contents of args are corrupted. It seems to me that I am passing pointers incorrectly. But I canβt understand why?
#define MAX_PRINT_OUTPUT 4096 void Echo(char *args[MAX_COMMAND_ARGUMENTS], int argCount) { for (int i = 1; i < argCount; ++i) { Printf("%s ", args[i]); Printf("\n"); } }; void Printf(const char *output, ...) { va_list args; char formattedOutput[MAX_PRINT_OUTPUT]; va_start(args, output); vsnprintf(formattedOutput, sizeof(formattedOutput), output, args); va_end(args); g_PlatformDevice->Print(formattedOutput); }; void RiseWindows::Print(const char *output) { //Corruption appears to occur as soon as this function is entered #define CONSOLE_OUTPUT_SIZE 32767 char buffer[CONSOLE_OUTPUT_SIZE]; char *pBuffer = buffer; const char *pOutput = output; int i = 0; while (pOutput[i] && ((pBuffer - buffer) < sizeof(buffer) - 1)) { if (pOutput[i] == '\n' && pOutput[i+1] == '\r' ) { pBuffer[0] = '\r'; pBuffer[1] = '\n'; pBuffer += 2; ++i; } else if (pOutput[i] == '\r') { pBuffer[0] = '\r'; pBuffer[1] = '\n'; pBuffer += 2; } else if (pOutput[i] == '\n') { pBuffer[0] = '\r'; pBuffer[1] = '\n'; pBuffer += 2; } else { *pBuffer = pOutput[i]; ++pBuffer; } ++i; } *pBuffer = 0; SendMessage(this->ConsoleWindow.hwndBuffer, EM_LINESCROLL, 0, 0xffff); SendMessage(this->ConsoleWindow.hwndBuffer, EM_SCROLLCARET, 0, 0); SendMessage(this->ConsoleWindow.hwndBuffer, EM_REPLACESEL, 0, (LPARAM)buffer); };
NOTE This is not a production code, just a proof of concept.
EDIT g_PlatformDevice is of type RiseWindows, if it is not clear ...
EDIT This is on a Windows XP platform running vs2008
UPDATE For everyone interested, the problem was apparently an overflowing call stack, further down the stack, then this other large array was defined. Refactoring eliminates memory corruption. So beat the chalk with a stack!
Adam naylor
source share