Title: [209689] trunk/Source
Revision
209689
Author
[email protected]
Date
2016-12-11 16:57:02 -0800 (Sun, 11 Dec 2016)

Log Message

Use std::vsnprintf instead of vasprintf
https://bugs.webkit.org/show_bug.cgi?id=165740

Reviewed by Sam Weinig.

Source/WebCore:

* platform/FileHandle.cpp:
(WebCore::FileHandle::printf): Use vsnprintf, including StringExtras.h to
ensure compatibility with older versions of the Visual Studio library,
and Vector for the buffer. Use inline capacity in the vector so we normally
don't need to allocate any memory on the heap.
* xml/XSLTUnicodeSort.cpp:
(xsltTransformErrorTrampoline): Ditto.
* xml/parser/XMLDocumentParserLibxml2.cpp:
(WebCore::XMLDocumentParser::error): Ditto.

Source/WTF:

* wtf/Platform.h: Remove HAVE_VASPRINTF.
* wtf/StringExtras.h: Change the vsnprintf workaround to be used only
in older versions of Visual Studio, since the problem it works around
was resolved in Visual Studio 2015.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (209688 => 209689)


--- trunk/Source/WTF/ChangeLog	2016-12-12 00:56:25 UTC (rev 209688)
+++ trunk/Source/WTF/ChangeLog	2016-12-12 00:57:02 UTC (rev 209689)
@@ -1,3 +1,15 @@
+2016-12-11  Darin Adler  <[email protected]>
+
+        Use std::vsnprintf instead of vasprintf
+        https://bugs.webkit.org/show_bug.cgi?id=165740
+
+        Reviewed by Sam Weinig.
+
+        * wtf/Platform.h: Remove HAVE_VASPRINTF.
+        * wtf/StringExtras.h: Change the vsnprintf workaround to be used only
+        in older versions of Visual Studio, since the problem it works around
+        was resolved in Visual Studio 2015.
+
 2016-12-10  Commit Queue  <[email protected]>
 
         Unreviewed, rolling out r209653, r209654, r209663, and

Modified: trunk/Source/WTF/wtf/Platform.h (209688 => 209689)


--- trunk/Source/WTF/wtf/Platform.h	2016-12-12 00:56:25 UTC (rev 209688)
+++ trunk/Source/WTF/wtf/Platform.h	2016-12-12 00:57:02 UTC (rev 209689)
@@ -633,12 +633,6 @@
 #define USE_PTHREADS 1
 #endif /* OS(UNIX) */
 
-#if !defined(HAVE_VASPRINTF)
-#if !COMPILER(MSVC) && !COMPILER(MINGW)
-#define HAVE_VASPRINTF 1
-#endif
-#endif
-
 #if OS(DARWIN)
 #define HAVE_DISPATCH_H 1
 #define HAVE_MADV_FREE 1

Modified: trunk/Source/WTF/wtf/StringExtras.h (209688 => 209689)


--- trunk/Source/WTF/wtf/StringExtras.h	2016-12-12 00:56:25 UTC (rev 209688)
+++ trunk/Source/WTF/wtf/StringExtras.h	2016-12-12 00:57:02 UTC (rev 209689)
@@ -35,13 +35,17 @@
 #include <strings.h> 
 #endif 
 
-#if COMPILER(MSVC)
-// FIXME: why a COMPILER check instead of OS? also, these should be HAVE checks
+#if COMPILER(MSVC) && _MSC_VER < 1900
 
+// In versions of the Microsoft library before Visual Studio 2015, snprintf and vsnprintf
+// did not null-terminate when the result entirely filled the buffer. The following work
+// around that limitation. This means that any file using snprintf or vsnprintf needs to
+// include this header and use the global namespace style to invoke it, not the std
+// namespace style.
+
 #include <errno.h>
 
-#if _MSC_VER < 1900
-inline int snprintf(char* buffer, size_t count, const char* format, ...) 
+inline int snprintf(char* buffer, size_t count, const char* format, ...)
 {
     int result;
     va_list args;
@@ -58,7 +62,6 @@
 
     return result;
 }
-#endif
 
 inline double wtf_vsnprintf(char* buffer, size_t count, const char* format, va_list args)
 {
@@ -74,10 +77,16 @@
     return result;
 }
 
-// Work around a difference in Microsoft's implementation of vsnprintf, where 
+// Work around a difference in Microsoft's implementation of vsnprintf, where
 // vsnprintf does not null terminate the buffer. WebKit can rely on the null termination.
 #define vsnprintf(buffer, count, format, args) wtf_vsnprintf(buffer, count, format, args)
 
+#endif
+
+#if COMPILER(MSVC)
+
+// FIXME: We should stop using these entirely and use suitable versions of equalIgnoringASCIICase instead.
+
 inline int strncasecmp(const char* s1, const char* s2, size_t len)
 {
     return _strnicmp(s1, s2, len);

Modified: trunk/Source/WebCore/ChangeLog (209688 => 209689)


--- trunk/Source/WebCore/ChangeLog	2016-12-12 00:56:25 UTC (rev 209688)
+++ trunk/Source/WebCore/ChangeLog	2016-12-12 00:57:02 UTC (rev 209689)
@@ -1,5 +1,22 @@
 2016-12-11  Darin Adler  <[email protected]>
 
+        Use std::vsnprintf instead of vasprintf
+        https://bugs.webkit.org/show_bug.cgi?id=165740
+
+        Reviewed by Sam Weinig.
+
+        * platform/FileHandle.cpp:
+        (WebCore::FileHandle::printf): Use vsnprintf, including StringExtras.h to
+        ensure compatibility with older versions of the Visual Studio library,
+        and Vector for the buffer. Use inline capacity in the vector so we normally
+        don't need to allocate any memory on the heap.
+        * xml/XSLTUnicodeSort.cpp:
+        (xsltTransformErrorTrampoline): Ditto.
+        * xml/parser/XMLDocumentParserLibxml2.cpp:
+        (WebCore::XMLDocumentParser::error): Ditto.
+
+2016-12-11  Darin Adler  <[email protected]>
+
         Make some refinements to HTMLPlugInImageElement
         https://bugs.webkit.org/show_bug.cgi?id=165742
 

Modified: trunk/Source/WebCore/platform/FileHandle.cpp (209688 => 209689)


--- trunk/Source/WebCore/platform/FileHandle.cpp	2016-12-12 00:56:25 UTC (rev 209688)
+++ trunk/Source/WebCore/platform/FileHandle.cpp	2016-12-12 00:57:02 UTC (rev 209689)
@@ -29,8 +29,7 @@
 #include "config.h"
 #include "FileHandle.h"
 
-#include <stdarg.h>
-#include <stdio.h>
+#include <wtf/StringExtras.h>
 
 namespace WebCore {
 
@@ -100,23 +99,20 @@
 
 bool FileHandle::printf(const char* format, ...)
 {
-#if OS(WINDOWS)
-    // TODO: implement this without relying on vasprintf.
-    return false;
-#else
     va_list args;
     va_start(args, format);
 
-    char* buffer = nullptr;
-    if (vasprintf(&buffer, format, args) == -1)
-        return false;
-    auto writeResult = write(buffer, strlen(buffer));
-    free(buffer);
+    va_list preflightArgs;
+    va_copy(preflightArgs, args);
+    size_t stringLength = vsnprintf(nullptr, 0, format, preflightArgs);
+    va_end(preflightArgs);
 
+    Vector<char, 1024> buffer(stringLength + 1);
+    vsnprintf(buffer.data(), stringLength + 1, format, args);
+
     va_end(args);
 
-    return writeResult >= 0;
-#endif
+    return write(buffer.data(), stringLength) >= 0;
 }
 
 void FileHandle::close()

Modified: trunk/Source/WebCore/xml/XSLTUnicodeSort.cpp (209688 => 209689)


--- trunk/Source/WebCore/xml/XSLTUnicodeSort.cpp	2016-12-12 00:56:25 UTC (rev 209688)
+++ trunk/Source/WebCore/xml/XSLTUnicodeSort.cpp	2016-12-12 00:57:02 UTC (rev 209689)
@@ -33,6 +33,8 @@
 
 #include <libxslt/templates.h>
 #include <libxslt/xsltutils.h>
+#include <wtf/StringExtras.h>
+#include <wtf/Vector.h>
 #include <wtf/unicode/Collator.h>
 
 #if OS(DARWIN) && !PLATFORM(EFL) && !PLATFORM(GTK)
@@ -51,15 +53,19 @@
 {
     va_list args;
     va_start(args, message);
-    char* messageWithArgs;
-    vasprintf(&messageWithArgs, message, args);
+
+    va_list preflightArgs;
+    va_copy(preflightArgs, args);
+    size_t stringLength = vsnprintf(nullptr, 0, message, preflightArgs);
+    va_end(preflightArgs);
+
+    Vector<char, 1024> buffer(stringLength + 1);
+    vsnprintf(buffer.data(), stringLength + 1, message, args);
     va_end(args);
 
     static void (*xsltTransformErrorPointer)(xsltTransformContextPtr, xsltStylesheetPtr, xmlNodePtr, const char*, ...) WTF_ATTRIBUTE_PRINTF(4, 5)
         = reinterpret_cast<void (*)(xsltTransformContextPtr, xsltStylesheetPtr, xmlNodePtr, const char*, ...)>(dlsym(libxsltLibrary(), "xsltTransformError"));
-    xsltTransformErrorPointer(context, style, node, "%s", messageWithArgs);
-
-    free(messageWithArgs);
+    xsltTransformErrorPointer(context, style, node, "%s", buffer.data());
 }
 
 #define xsltTransformError xsltTransformErrorTrampoline

Modified: trunk/Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp (209688 => 209689)


--- trunk/Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp	2016-12-12 00:56:25 UTC (rev 209688)
+++ trunk/Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp	2016-12-12 00:57:02 UTC (rev 209689)
@@ -916,24 +916,19 @@
     if (isStopped())
         return;
 
-#if HAVE(VASPRINTF)
-    char* m;
-    if (vasprintf(&m, message, args) == -1)
-        return;
-#else
-    char m[1024];
-    vsnprintf(m, sizeof(m) - 1, message, args);
-#endif
+    va_list preflightArgs;
+    va_copy(preflightArgs, args);
+    size_t stringLength = vsnprintf(nullptr, 0, message, preflightArgs);
+    va_end(preflightArgs);
 
+    Vector<char, 1024> buffer(stringLength + 1);
+    vsnprintf(buffer.data(), stringLength + 1, message, args);
+
     TextPosition position = textPosition();
     if (m_parserPaused)
-        m_pendingCallbacks->appendErrorCallback(type, reinterpret_cast<const xmlChar*>(m), position.m_line, position.m_column);
+        m_pendingCallbacks->appendErrorCallback(type, reinterpret_cast<const xmlChar*>(buffer.data()), position.m_line, position.m_column);
     else
-        handleError(type, m, textPosition());
-
-#if HAVE(VASPRINTF)
-    free(m);
-#endif
+        handleError(type, buffer.data(), textPosition());
 }
 
 void XMLDocumentParser::processingInstruction(const xmlChar* target, const xmlChar* data)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to