Patch #3479 2013-06-16 21:16

bluehazzard

Fix a buffer overflow error in squirrel print
Download
3479-Fix_a_buffer_o.patch (2.7 KB)
Category
Application::Bugfix
Status
Open
Close date
 
Assigned to
mortenmacfly
Index: src/include/scriptingmanager.h
===================================================================
--- src/include/scriptingmanager.h    (revision 9158)
+++ src/include/scriptingmanager.h    (working copy)
@@ -18,6 +18,7 @@
 #include "manager.h"
 #include "menuitemsmanager.h"
 #include <wx/intl.h>
+#include "sqplus.h"
 
 struct SquirrelError;
 
@@ -255,4 +256,6 @@
         DECLARE_EVENT_TABLE()
 };
 
+void PrintSquirrelToWxString(wxString &msg,const SQChar *s, va_list& vl);
+
 #endif // SCRIPTING_H
 
Index: src/sdk/scriptingmanager.cpp
===================================================================
--- src/sdk/scriptingmanager.cpp    (revision 9158)
+++ src/sdk/scriptingmanager.cpp    (working copy)
@@ -29,7 +29,7 @@
 #include "crc32.h"
 #include "menuitemsmanager.h"
 #include "genericmultilinenotesdlg.h"
-#include "sqplus.h"
+//#include "sqplus.h"
 #include "scriptbindings.h"
 #include "sc_plugin.h"
 #include "sqstdstring.h"
@@ -40,26 +40,44 @@
 static wxString s_ScriptErrors;
 static wxString capture;
 
+void PrintSquirrelToWxString(wxString &msg,const SQChar * s, va_list& vl)
+{
+    //wxString msg;
+    int buffer_size = 2048;
+    SQChar *tmp_buffer;
+    for(;;buffer_size*=2)
+    {
+        tmp_buffer = new SQChar [buffer_size];
+        int retvalue = vsnprintf(tmp_buffer,buffer_size,s,vl);
+        if(retvalue < buffer_size)
+        {
+            // Buffersize was large enough
+            msg = cbC2U(tmp_buffer);
+            delete[] tmp_buffer;
+            break;
+        }
+        // Buffer size was not enough
+        delete[] tmp_buffer;
+    }
+}
+
 static void ScriptsPrintFunc(HSQUIRRELVM /*v*/, const SQChar * s, ...)
 {
-    static SQChar temp[2048];
     va_list vl;
     va_start(vl,s);
-    scvsprintf( temp,s,vl);
-    wxString msg = cbC2U(temp);
-    Manager::Get()->GetLogManager()->DebugLog(msg);
+    wxString msg;
+    PrintSquirrelToWxString(msg,s,vl);
     va_end(vl);
-
     s_ScriptErrors << msg;
 }
 
 static void CaptureScriptOutput(HSQUIRRELVM /*v*/, const SQChar * s, ...)
 {
-    static SQChar temp[2048];
     va_list vl;
     va_start(vl,s);
-    scvsprintf(temp,s,vl);
-    ::capture.append(cbC2U(temp));
+    wxString msg;
+    PrintSquirrelToWxString(msg,s,vl);
+    ::capture.append(msg);
     va_end(vl);
 }
 
Index: src/src/scriptconsole.cpp
===================================================================
--- src/src/scriptconsole.cpp    (revision 9158)
+++ src/src/scriptconsole.cpp    (working copy)
@@ -46,11 +46,10 @@
 
 static void ScriptConsolePrintFunc(HSQUIRRELVM /*v*/, const SQChar * s, ...)
 {
-    static SQChar temp[2048];
     va_list vl;
     va_start(vl,s);
-    scvsprintf( temp,s,vl);
-    wxString msg = cbC2U(temp);
+    wxString msg;
+    PrintSquirrelToWxString(msg,s,vl);
     va_end(vl);
 
     if (s_Console)
ollydbg 2013-06-17 05:48
bluehazzard 2013-06-17 06:37

Changes:

* Add function to avoid code duplication