Title: [215521] trunk
Revision
215521
Author
[email protected]
Date
2017-04-19 11:17:02 -0700 (Wed, 19 Apr 2017)

Log Message

Stop using strcpy() in WebKit::EnvironmentUtilities::stripValuesEndingWithString()
<https://webkit.org/b/170994>
<rdar://problem/29889932>

Reviewed by Brent Fulgham.

Source/WebKit2:

* Platform/unix/EnvironmentUtilities.cpp:
(WebKit::EnvironmentUtilities::stripValuesEndingWithString):
Switch from using strcpy() to strlcpy().  Also switch from using
strstr() to strnstr().
* Platform/unix/EnvironmentUtilities.h: Switch to #pragma once.
(WebKit::EnvironmentUtilities::stripValuesEndingWithString):
Export function for testing.
* WebKit2.xcodeproj/project.pbxproj:
(EnvironmentUtilitiesTest.h): Make header private for testing.

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
(EnvironmentUtilitiesTest.cpp): Add to TestWebKitAPILibrary
target.
* TestWebKitAPI/Tests/WebKit2/EnvironmentUtilitiesTest.cpp: Add.
(TestWebKitAPI::strip): Helper method to set/get environment
variable for testing.
(TestWebKitAPI::WebKit2_StripValuesEndingWithString_Test): Add
tests.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (215520 => 215521)


--- trunk/Source/WebKit2/ChangeLog	2017-04-19 18:08:03 UTC (rev 215520)
+++ trunk/Source/WebKit2/ChangeLog	2017-04-19 18:17:02 UTC (rev 215521)
@@ -1,3 +1,21 @@
+2017-04-19  David Kilzer  <[email protected]>
+
+        Stop using strcpy() in WebKit::EnvironmentUtilities::stripValuesEndingWithString()
+        <https://webkit.org/b/170994>
+        <rdar://problem/29889932>
+
+        Reviewed by Brent Fulgham.
+
+        * Platform/unix/EnvironmentUtilities.cpp:
+        (WebKit::EnvironmentUtilities::stripValuesEndingWithString):
+        Switch from using strcpy() to strlcpy().  Also switch from using
+        strstr() to strnstr().
+        * Platform/unix/EnvironmentUtilities.h: Switch to #pragma once.
+        (WebKit::EnvironmentUtilities::stripValuesEndingWithString):
+        Export function for testing.
+        * WebKit2.xcodeproj/project.pbxproj:
+        (EnvironmentUtilitiesTest.h): Make header private for testing.
+
 2017-04-19  Eric Carlson  <[email protected]>
 
         Provide a way for clients to unmute a media stream.

Modified: trunk/Source/WebKit2/Platform/unix/EnvironmentUtilities.cpp (215520 => 215521)


--- trunk/Source/WebKit2/Platform/unix/EnvironmentUtilities.cpp	2017-04-19 18:08:03 UTC (rev 215520)
+++ trunk/Source/WebKit2/Platform/unix/EnvironmentUtilities.cpp	2017-04-19 18:17:02 UTC (rev 215521)
@@ -39,10 +39,13 @@
     
     // Grab the current value of the environment variable.
     char* environmentValue = getenv(environmentVariable);
-        
+
     if (!environmentValue || environmentValue[0] == '\0')
         return;
 
+    const size_t environmentValueLength = strlen(environmentValue);
+    const size_t environmentValueBufferLength = environmentValueLength + 1;
+
     // Set up the strings we'll be searching for.
     size_t searchLength = strlen(searchValue);
     if (!searchLength)
@@ -59,45 +62,49 @@
     
     // Loop over environmentValueBuffer, removing any components that match the search value ending with a colon.
     char* componentStart = environmentValue;
-    char* match = strstr(componentStart, searchValueWithColon);
+    char* match = strnstr(componentStart, searchValueWithColon, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
     bool foundAnyMatches = match != NULL;
     while (match != NULL) {
         // Update componentStart to point to the colon immediately preceding the match.
-        char* nextColon = strstr(componentStart, ":");
+        char* nextColon = strnstr(componentStart, ":", environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
         while (nextColon && nextColon < match) {
             componentStart = nextColon;
-            nextColon = strstr(componentStart + 1, ":");
+            nextColon = strnstr(componentStart + 1, ":", environmentValueLength - static_cast<size_t>(componentStart + 1 - environmentValue));
         }
-                
+
+        RELEASE_ASSERT(componentStart >= environmentValue);
+        size_t environmentValueOffset = static_cast<size_t>(componentStart - environmentValue);
+        RELEASE_ASSERT(environmentValueOffset < environmentValueBufferLength);
+
         // Copy over everything right of the match to the current component start, and search from there again.
         if (componentStart[0] == ':') {
             // If componentStart points to a colon, copy the colon over.
-            strcpy(componentStart, match + searchLength);
+            strlcpy(componentStart, match + searchLength, environmentValueBufferLength - environmentValueOffset);
         } else {
             // Otherwise, componentStart still points to the beginning of environmentValueBuffer, so don't copy over the colon.
             // The edge case is if the colon is the last character in the string, so "match + searchLengthWithoutColon + 1" is the
             // null terminator of the original input, in which case this is still safe.
-            strcpy(componentStart, match + searchLengthWithColon);
+            strlcpy(componentStart, match + searchLengthWithColon, environmentValueBufferLength - environmentValueOffset);
         }
         
-        match = strstr(componentStart, searchValueWithColon);
+        match = strnstr(componentStart, searchValueWithColon, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
     }
     
     // Search for the value without a trailing colon, seeing if the original input ends with it.
-    match = strstr(componentStart, searchValue);
+    match = strnstr(componentStart, searchValue, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
     while (match != NULL) {
         if (match[searchLength] == '\0')
             break;
-        match = strstr(match + 1, searchValue);
+        match = strnstr(match + 1, searchValue, environmentValueLength - static_cast<size_t>(match + 1 - environmentValue));
     }
     
     // Since the original input ends with the search, strip out the last component.
     if (match) {
         // Update componentStart to point to the colon immediately preceding the match.
-        char* nextColon = strstr(componentStart, ":");
+        char* nextColon = strnstr(componentStart, ":", environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
         while (nextColon && nextColon < match) {
             componentStart = nextColon;
-            nextColon = strstr(componentStart + 1, ":");
+            nextColon = strnstr(componentStart + 1, ":", environmentValueLength - static_cast<size_t>(componentStart + 1 - environmentValue));
         }
         
         // Whether componentStart points to the original string or the last colon, putting the null terminator there will get us the desired result.

Modified: trunk/Source/WebKit2/Platform/unix/EnvironmentUtilities.h (215520 => 215521)


--- trunk/Source/WebKit2/Platform/unix/EnvironmentUtilities.h	2017-04-19 18:08:03 UTC (rev 215520)
+++ trunk/Source/WebKit2/Platform/unix/EnvironmentUtilities.h	2017-04-19 18:17:02 UTC (rev 215521)
@@ -23,9 +23,9 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef EnvironmentUtilities_h
-#define EnvironmentUtilities_h
+#pragma once
 
+#include "WKDeclarationSpecifiers.h"
 #include <wtf/text/WTFString.h>
 
 namespace WebKit {
@@ -32,11 +32,8 @@
 
 namespace EnvironmentUtilities {
 
-void stripValuesEndingWithString(const char* environmentVariable, const char* search);
+WK_EXPORT void stripValuesEndingWithString(const char* environmentVariable, const char* search);
 
 } // namespace EnvironmentUtilities
 
 } // namespace WebKit
-
-#endif // #define EnvironmentUtilities_h
-

Modified: trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj (215520 => 215521)


--- trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj	2017-04-19 18:08:03 UTC (rev 215520)
+++ trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj	2017-04-19 18:17:02 UTC (rev 215521)
@@ -1042,7 +1042,7 @@
 		51ACBBA0127A8F2C00D203B9 /* WebContextMenuProxyMac.h in Headers */ = {isa = PBXBuildFile; fileRef = 51ACBB9E127A8F2C00D203B9 /* WebContextMenuProxyMac.h */; };
 		51ACBBA1127A8F2C00D203B9 /* WebContextMenuProxyMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51ACBB9F127A8F2C00D203B9 /* WebContextMenuProxyMac.mm */; };
 		51B15A8413843A3900321AD8 /* EnvironmentUtilities.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51B15A8213843A3900321AD8 /* EnvironmentUtilities.cpp */; };
-		51B15A8513843A3900321AD8 /* EnvironmentUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B15A8313843A3900321AD8 /* EnvironmentUtilities.h */; };
+		51B15A8513843A3900321AD8 /* EnvironmentUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B15A8313843A3900321AD8 /* EnvironmentUtilities.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		51C0C9741DDD76000032CAD3 /* IconLoadingDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 51C0C9721DDD74F00032CAD3 /* IconLoadingDelegate.h */; };
 		51C0C9751DDD76030032CAD3 /* IconLoadingDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51C0C9731DDD74F00032CAD3 /* IconLoadingDelegate.mm */; };
 		51CD1C5D1B3493AF00142CA5 /* WKSecurityOriginRef.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51CD1C591B3493A900142CA5 /* WKSecurityOriginRef.cpp */; };

Modified: trunk/Tools/ChangeLog (215520 => 215521)


--- trunk/Tools/ChangeLog	2017-04-19 18:08:03 UTC (rev 215520)
+++ trunk/Tools/ChangeLog	2017-04-19 18:17:02 UTC (rev 215521)
@@ -1,3 +1,20 @@
+2017-04-19  David Kilzer  <[email protected]>
+
+        Stop using strcpy() in WebKit::EnvironmentUtilities::stripValuesEndingWithString()
+        <https://webkit.org/b/170994>
+        <rdar://problem/29889932>
+
+        Reviewed by Brent Fulgham.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        (EnvironmentUtilitiesTest.cpp): Add to TestWebKitAPILibrary
+        target.
+        * TestWebKitAPI/Tests/WebKit2/EnvironmentUtilitiesTest.cpp: Add.
+        (TestWebKitAPI::strip): Helper method to set/get environment
+        variable for testing.
+        (TestWebKitAPI::WebKit2_StripValuesEndingWithString_Test): Add
+        tests.
+
 2017-04-19  JF Bastien  <[email protected]>
 
         WebAssembly: add script which can import GCC torture tests

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (215520 => 215521)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2017-04-19 18:08:03 UTC (rev 215520)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2017-04-19 18:17:02 UTC (rev 215521)
@@ -123,6 +123,7 @@
 		37E7DD671EA071F3009B396D /* AdditionalReadAccessAllowedURLsPlugin.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37E7DD661EA071F3009B396D /* AdditionalReadAccessAllowedURLsPlugin.mm */; };
 		37FB72971DB2E82F00E41BE4 /* ContextMenuDefaultItemsHaveTags.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37FB72951DB2E82F00E41BE4 /* ContextMenuDefaultItemsHaveTags.mm */; };
 		3FBD1B4A1D3D66AB00E6D6FA /* FullscreenLayoutConstraints.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 3FBD1B491D39D1DB00E6D6FA /* FullscreenLayoutConstraints.html */; };
+		448D7E471EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 448D7E451EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp */; };
 		46397B951DC2C850009A78AE /* DOMNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46397B941DC2C850009A78AE /* DOMNode.mm */; };
 		46C519DA1D355AB200DAA51A /* LocalStorageNullEntries.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46C519D81D355A7300DAA51A /* LocalStorageNullEntries.mm */; };
 		46C519E61D3563FD00DAA51A /* LocalStorageNullEntries.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 46C519E21D35629600DAA51A /* LocalStorageNullEntries.html */; };
@@ -1039,6 +1040,7 @@
 		41973B5C1AF22875006C7B36 /* SharedBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SharedBuffer.cpp; sourceTree = "<group>"; };
 		440A1D3814A0103A008A66F2 /* URL.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = URL.cpp; sourceTree = "<group>"; };
 		442BBF681C91CAD90017087F /* RefLogger.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RefLogger.cpp; sourceTree = "<group>"; };
+		448D7E451EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EnvironmentUtilitiesTest.cpp; sourceTree = "<group>"; };
 		44A622C114A0E2B60048515B /* WTFStringUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTFStringUtilities.h; sourceTree = "<group>"; };
 		46397B941DC2C850009A78AE /* DOMNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DOMNode.mm; sourceTree = "<group>"; };
 		46C519D81D355A7300DAA51A /* LocalStorageNullEntries.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = LocalStorageNullEntries.mm; sourceTree = "<group>"; };
@@ -2021,6 +2023,7 @@
 				F6F49C6615545C8D0007F39D /* DOMWindowExtensionNoCache_Bundle.cpp */,
 				C045F9441385C2E900C0F3CD /* DownloadDecideDestinationCrash.cpp */,
 				07492B3A1DF8AE2D00633DE1 /* EnumerateMediaDevices.cpp */,
+				448D7E451EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp */,
 				75F3133F18C171B70041CAEC /* EphemeralSessionPushStateNoHistoryCallback.cpp */,
 				1A5FEFDC1270E2A3000E2921 /* EvaluateJavaScript.cpp */,
 				BCC8B95A12611F4700DE46A4 /* FailedLoad.cpp */,
@@ -2810,6 +2813,7 @@
 				7CCE7EE01A411A9A00447C4C /* EditorCommands.mm in Sources */,
 				7CCE7EBF1A411A7E00447C4C /* ElementAtPointInWebFrame.mm in Sources */,
 				07492B3B1DF8B14C00633DE1 /* EnumerateMediaDevices.cpp in Sources */,
+				448D7E471EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp in Sources */,
 				7CCE7EEF1A411AE600447C4C /* EphemeralSessionPushStateNoHistoryCallback.cpp in Sources */,
 				7CCE7EF01A411AE600447C4C /* EvaluateJavaScript.cpp in Sources */,
 				315118101DB1AE4000176304 /* ExtendedColor.cpp in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/WebKit2/EnvironmentUtilitiesTest.cpp (0 => 215521)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2/EnvironmentUtilitiesTest.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2/EnvironmentUtilitiesTest.cpp	2017-04-19 18:17:02 UTC (rev 215521)
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "Test.h"
+
+#include <WebKit/EnvironmentUtilities.h>
+#include <stdlib.h>
+
+namespace TestWebKitAPI {
+
+const char* const environmentVariable = "DYLD_INSERT_LIBRARIES";
+#define PROCESS_DYLIB "Process.dylib"
+const char* const stripValue = "/" PROCESS_DYLIB;
+
+static const char* strip(const char* input)
+{
+    setenv(environmentVariable, input, 1);
+    WebKit::EnvironmentUtilities::stripValuesEndingWithString(environmentVariable, stripValue);
+    return getenv(environmentVariable);
+}
+
+TEST(WebKit2, StripValuesEndingWithString)
+{
+    EXPECT_STREQ(strip(""), "");
+    EXPECT_STREQ(strip(":"), ":");
+    EXPECT_STREQ(strip("::"), "::");
+    EXPECT_STREQ(strip(":::"), ":::");
+    EXPECT_STREQ(strip("::::"), "::::");
+    EXPECT_STREQ(strip(":::::"), ":::::");
+
+    EXPECT_STREQ(strip(PROCESS_DYLIB), PROCESS_DYLIB);
+    EXPECT_STREQ(strip(":" PROCESS_DYLIB), ":" PROCESS_DYLIB);
+    EXPECT_STREQ(strip(PROCESS_DYLIB ":"), PROCESS_DYLIB ":");
+    EXPECT_STREQ(strip(":" PROCESS_DYLIB ":"), ":" PROCESS_DYLIB ":");
+
+    EXPECT_STREQ(strip("/" PROCESS_DYLIB), nullptr);
+    EXPECT_STREQ(strip(":/" PROCESS_DYLIB), nullptr);
+    EXPECT_STREQ(strip("/" PROCESS_DYLIB ":"), nullptr);
+    EXPECT_STREQ(strip(":/" PROCESS_DYLIB ":"), ":");
+
+    EXPECT_STREQ(strip(PROCESS_DYLIB "/"), PROCESS_DYLIB "/");
+    EXPECT_STREQ(strip(":" PROCESS_DYLIB "/"), ":" PROCESS_DYLIB "/");
+    EXPECT_STREQ(strip(PROCESS_DYLIB "/:"), PROCESS_DYLIB "/:");
+    EXPECT_STREQ(strip(":" PROCESS_DYLIB "/:"), ":" PROCESS_DYLIB "/:");
+
+    EXPECT_STREQ(strip("/" PROCESS_DYLIB "/"), "/" PROCESS_DYLIB "/");
+    EXPECT_STREQ(strip(":/" PROCESS_DYLIB "/"), ":/" PROCESS_DYLIB "/");
+    EXPECT_STREQ(strip("/" PROCESS_DYLIB "/:"), "/" PROCESS_DYLIB "/:");
+    EXPECT_STREQ(strip(":/" PROCESS_DYLIB "/:"), ":/" PROCESS_DYLIB "/:");
+
+    EXPECT_STREQ(strip("/Before.dylib:/" PROCESS_DYLIB), "/Before.dylib");
+    EXPECT_STREQ(strip("/" PROCESS_DYLIB ":/After.dylib"), "/After.dylib");
+    EXPECT_STREQ(strip("/Before.dylib:/" PROCESS_DYLIB ":/After.dylib"), "/Before.dylib:/After.dylib");
+    EXPECT_STREQ(strip("/Before.dylib:/" PROCESS_DYLIB ":/Middle.dylib:/" PROCESS_DYLIB ":/After.dylib"), "/Before.dylib:/Middle.dylib:/After.dylib");
+
+    EXPECT_STREQ(strip("/" PROCESS_DYLIB ":/" PROCESS_DYLIB), nullptr);
+    EXPECT_STREQ(strip("/" PROCESS_DYLIB ":/" PROCESS_DYLIB ":/" PROCESS_DYLIB), nullptr);
+
+    EXPECT_STREQ(strip("/usr/lib/" PROCESS_DYLIB), nullptr);
+    EXPECT_STREQ(strip("/" PROCESS_DYLIB "/" PROCESS_DYLIB), nullptr);
+}
+
+}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to