Author: hbelusca
Date: Sun Nov  9 01:46:31 2014
New Revision: 65336

URL: http://svn.reactos.org/svn/reactos?rev=65336&view=rev
Log:
[NTVDM]: Use variable-length buffers in DisplayMessage, in case we display a 
very long message (uses the _vscwprintf CRT function, not available on Win2k. 
You need to recompile NTVDM with WIN2K_COMPLIANT define if you want to be able 
to run it on win2k. In that case DisplayMessage uses a quite large enough 
buffer for its needs). If somebody knows an alternative to _vscwprintf that 
does the very same job, and which exists on Win2k, I would be happy to use it 
instead.

Modified:
    trunk/reactos/subsystems/ntvdm/ntvdm.c

Modified: trunk/reactos/subsystems/ntvdm/ntvdm.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/subsystems/ntvdm/ntvdm.c?rev=65336&r1=65335&r2=65336&view=diff
==============================================================================
--- trunk/reactos/subsystems/ntvdm/ntvdm.c      [iso-8859-1] (original)
+++ trunk/reactos/subsystems/ntvdm/ntvdm.c      [iso-8859-1] Sun Nov  9 
01:46:31 2014
@@ -185,13 +185,48 @@
 VOID
 DisplayMessage(LPCWSTR Format, ...)
 {
-    WCHAR Buffer[256];
+#ifndef WIN2K_COMPLIANT
+    WCHAR  StaticBuffer[256];
+    LPWSTR Buffer = StaticBuffer; // Use the static buffer by default.
+#else
+    WCHAR  Buffer[2048]; // Large enough. If not, increase it by hand.
+#endif
+    size_t MsgLen;
     va_list Parameters;
 
     va_start(Parameters, Format);
-    _vsnwprintf(Buffer, 256, Format, Parameters);
+
+#ifndef WIN2K_COMPLIANT
+    /*
+     * Retrieve the message length and if it is too long, allocate
+     * an auxiliary buffer; otherwise use the static buffer.
+     */
+    MsgLen = _vscwprintf(Format, Parameters) + 1; // NULL-terminated
+    if (MsgLen > sizeof(StaticBuffer)/sizeof(StaticBuffer[0]))
+    {
+        Buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MsgLen * 
sizeof(WCHAR));
+        if (Buffer == NULL)
+        {
+            /* Allocation failed, use the static buffer and display a suitable 
error message */
+            Buffer = StaticBuffer;
+            Format = L"DisplayMessage()\nOriginal message is too long and 
allocating an auxiliary buffer failed.";
+            MsgLen = wcslen(Format);
+        }
+    }
+#else
+    MsgLen = sizeof(Buffer)/sizeof(Buffer[0]);
+#endif
+
+    /* Display the message */
+    _vsnwprintf(Buffer, MsgLen, Format, Parameters);
     DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer);
     MessageBoxW(NULL, Buffer, L"NTVDM Subsystem", MB_OK);
+
+#ifndef WIN2K_COMPLIANT
+    /* Free the buffer if needed */
+    if (Buffer != StaticBuffer) HeapFree(GetProcessHeap(), 0, Buffer);
+#endif
+
     va_end(Parameters);
 }
 
@@ -205,8 +240,8 @@
         case CTRL_BREAK_EVENT:
         {
             /* HACK: Stop the VDM */
+            DPRINT1("Ctrl-C/Break: Stop the VDM\n");
             EmulatorTerminate();
-
             break;
         }
         case CTRL_LAST_CLOSE_EVENT:


Reply via email to