fix buffer overflow in print() on windows

This commit is contained in:
arvidn 2015-06-18 00:31:28 -04:00
parent 184a1aa422
commit 6cf9965416
1 changed files with 3 additions and 7 deletions

View File

@ -263,15 +263,11 @@ void apply_ansi_code(int* attributes, bool* reverse, int code)
}
}
#endif
void print(char const* str)
void print(char const* buf)
{
#ifdef _WIN32
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
char buffer[4096];
char* buf = buffer;
strcpy(buf, str);
int current_attributes = 7;
bool reverse = false;
SetConsoleTextAttribute(out, current_attributes);
@ -282,10 +278,10 @@ void print(char const* str)
{
if (*buf == '\033' && buf[1] == '[')
{
*buf = 0;
WriteFile(out, start, buf - start, &written, NULL);
buf += 2; // skip escape and '['
start = buf;
if (*buf == 0) break;
if (*start == 'K')
{
// this means clear the rest of the line.
@ -341,7 +337,7 @@ void print(char const* str)
WriteFile(out, start, buf - start, &written, NULL);
#else
fputs(str, stdout);
fputs(buf, stdout);
#endif
}