From 6cf9965416ff1ac6edd0ccb26b03cd2067f53469 Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 18 Jun 2015 00:31:28 -0400 Subject: [PATCH] fix buffer overflow in print() on windows --- examples/print.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/examples/print.cpp b/examples/print.cpp index 1744af7e2..08b5e90f0 100644 --- a/examples/print.cpp +++ b/examples/print.cpp @@ -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 }