Changeset 215521 in webkit


Ignore:
Timestamp:
Apr 19, 2017 11:17:02 AM (7 years ago)
Author:
ddkilzer@apple.com
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.

Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r215512 r215521  
     12017-04-19  David Kilzer  <ddkilzer@apple.com>
     2
     3        Stop using strcpy() in WebKit::EnvironmentUtilities::stripValuesEndingWithString()
     4        <https://webkit.org/b/170994>
     5        <rdar://problem/29889932>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        * Platform/unix/EnvironmentUtilities.cpp:
     10        (WebKit::EnvironmentUtilities::stripValuesEndingWithString):
     11        Switch from using strcpy() to strlcpy().  Also switch from using
     12        strstr() to strnstr().
     13        * Platform/unix/EnvironmentUtilities.h: Switch to #pragma once.
     14        (WebKit::EnvironmentUtilities::stripValuesEndingWithString):
     15        Export function for testing.
     16        * WebKit2.xcodeproj/project.pbxproj:
     17        (EnvironmentUtilitiesTest.h): Make header private for testing.
     18
    1192017-04-19  Eric Carlson  <eric.carlson@apple.com>
    220
  • trunk/Source/WebKit2/Platform/unix/EnvironmentUtilities.cpp

    r182364 r215521  
    4040    // Grab the current value of the environment variable.
    4141    char* environmentValue = getenv(environmentVariable);
    42        
     42
    4343    if (!environmentValue || environmentValue[0] == '\0')
    4444        return;
     45
     46    const size_t environmentValueLength = strlen(environmentValue);
     47    const size_t environmentValueBufferLength = environmentValueLength + 1;
    4548
    4649    // Set up the strings we'll be searching for.
     
    6063    // Loop over environmentValueBuffer, removing any components that match the search value ending with a colon.
    6164    char* componentStart = environmentValue;
    62     char* match = strstr(componentStart, searchValueWithColon);
     65    char* match = strnstr(componentStart, searchValueWithColon, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
    6366    bool foundAnyMatches = match != NULL;
    6467    while (match != NULL) {
    6568        // Update componentStart to point to the colon immediately preceding the match.
    66         char* nextColon = strstr(componentStart, ":");
     69        char* nextColon = strnstr(componentStart, ":", environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
    6770        while (nextColon && nextColon < match) {
    6871            componentStart = nextColon;
    69             nextColon = strstr(componentStart + 1, ":");
     72            nextColon = strnstr(componentStart + 1, ":", environmentValueLength - static_cast<size_t>(componentStart + 1 - environmentValue));
    7073        }
    71                
     74
     75        RELEASE_ASSERT(componentStart >= environmentValue);
     76        size_t environmentValueOffset = static_cast<size_t>(componentStart - environmentValue);
     77        RELEASE_ASSERT(environmentValueOffset < environmentValueBufferLength);
     78
    7279        // Copy over everything right of the match to the current component start, and search from there again.
    7380        if (componentStart[0] == ':') {
    7481            // If componentStart points to a colon, copy the colon over.
    75             strcpy(componentStart, match + searchLength);
     82            strlcpy(componentStart, match + searchLength, environmentValueBufferLength - environmentValueOffset);
    7683        } else {
    7784            // Otherwise, componentStart still points to the beginning of environmentValueBuffer, so don't copy over the colon.
    7885            // The edge case is if the colon is the last character in the string, so "match + searchLengthWithoutColon + 1" is the
    7986            // null terminator of the original input, in which case this is still safe.
    80             strcpy(componentStart, match + searchLengthWithColon);
     87            strlcpy(componentStart, match + searchLengthWithColon, environmentValueBufferLength - environmentValueOffset);
    8188        }
    8289       
    83         match = strstr(componentStart, searchValueWithColon);
     90        match = strnstr(componentStart, searchValueWithColon, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
    8491    }
    8592   
    8693    // Search for the value without a trailing colon, seeing if the original input ends with it.
    87     match = strstr(componentStart, searchValue);
     94    match = strnstr(componentStart, searchValue, environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
    8895    while (match != NULL) {
    8996        if (match[searchLength] == '\0')
    9097            break;
    91         match = strstr(match + 1, searchValue);
     98        match = strnstr(match + 1, searchValue, environmentValueLength - static_cast<size_t>(match + 1 - environmentValue));
    9299    }
    93100   
     
    95102    if (match) {
    96103        // Update componentStart to point to the colon immediately preceding the match.
    97         char* nextColon = strstr(componentStart, ":");
     104        char* nextColon = strnstr(componentStart, ":", environmentValueLength - static_cast<size_t>(componentStart - environmentValue));
    98105        while (nextColon && nextColon < match) {
    99106            componentStart = nextColon;
    100             nextColon = strstr(componentStart + 1, ":");
     107            nextColon = strnstr(componentStart + 1, ":", environmentValueLength - static_cast<size_t>(componentStart + 1 - environmentValue));
    101108        }
    102109       
  • trunk/Source/WebKit2/Platform/unix/EnvironmentUtilities.h

    r95901 r215521  
    2424 */
    2525
    26 #ifndef EnvironmentUtilities_h
    27 #define EnvironmentUtilities_h
     26#pragma once
    2827
     28#include "WKDeclarationSpecifiers.h"
    2929#include <wtf/text/WTFString.h>
    3030
     
    3333namespace EnvironmentUtilities {
    3434
    35 void stripValuesEndingWithString(const char* environmentVariable, const char* search);
     35WK_EXPORT void stripValuesEndingWithString(const char* environmentVariable, const char* search);
    3636
    3737} // namespace EnvironmentUtilities
    3838
    3939} // namespace WebKit
    40 
    41 #endif // #define EnvironmentUtilities_h
    42 
  • trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj

    r215488 r215521  
    10431043                51ACBBA1127A8F2C00D203B9 /* WebContextMenuProxyMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51ACBB9F127A8F2C00D203B9 /* WebContextMenuProxyMac.mm */; };
    10441044                51B15A8413843A3900321AD8 /* EnvironmentUtilities.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51B15A8213843A3900321AD8 /* EnvironmentUtilities.cpp */; };
    1045                 51B15A8513843A3900321AD8 /* EnvironmentUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B15A8313843A3900321AD8 /* EnvironmentUtilities.h */; };
     1045                51B15A8513843A3900321AD8 /* EnvironmentUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B15A8313843A3900321AD8 /* EnvironmentUtilities.h */; settings = {ATTRIBUTES = (Private, ); }; };
    10461046                51C0C9741DDD76000032CAD3 /* IconLoadingDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 51C0C9721DDD74F00032CAD3 /* IconLoadingDelegate.h */; };
    10471047                51C0C9751DDD76030032CAD3 /* IconLoadingDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51C0C9731DDD74F00032CAD3 /* IconLoadingDelegate.mm */; };
  • trunk/Tools/ChangeLog

    r215519 r215521  
     12017-04-19  David Kilzer  <ddkilzer@apple.com>
     2
     3        Stop using strcpy() in WebKit::EnvironmentUtilities::stripValuesEndingWithString()
     4        <https://webkit.org/b/170994>
     5        <rdar://problem/29889932>
     6
     7        Reviewed by Brent Fulgham.
     8
     9        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
     10        (EnvironmentUtilitiesTest.cpp): Add to TestWebKitAPILibrary
     11        target.
     12        * TestWebKitAPI/Tests/WebKit2/EnvironmentUtilitiesTest.cpp: Add.
     13        (TestWebKitAPI::strip): Helper method to set/get environment
     14        variable for testing.
     15        (TestWebKitAPI::WebKit2_StripValuesEndingWithString_Test): Add
     16        tests.
     17
    1182017-04-19  JF Bastien  <jfbastien@apple.com>
    219
  • trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj

    r215424 r215521  
    124124                37FB72971DB2E82F00E41BE4 /* ContextMenuDefaultItemsHaveTags.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37FB72951DB2E82F00E41BE4 /* ContextMenuDefaultItemsHaveTags.mm */; };
    125125                3FBD1B4A1D3D66AB00E6D6FA /* FullscreenLayoutConstraints.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 3FBD1B491D39D1DB00E6D6FA /* FullscreenLayoutConstraints.html */; };
     126                448D7E471EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 448D7E451EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp */; };
    126127                46397B951DC2C850009A78AE /* DOMNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46397B941DC2C850009A78AE /* DOMNode.mm */; };
    127128                46C519DA1D355AB200DAA51A /* LocalStorageNullEntries.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46C519D81D355A7300DAA51A /* LocalStorageNullEntries.mm */; };
     
    10401041                440A1D3814A0103A008A66F2 /* URL.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = URL.cpp; sourceTree = "<group>"; };
    10411042                442BBF681C91CAD90017087F /* RefLogger.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RefLogger.cpp; sourceTree = "<group>"; };
     1043                448D7E451EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EnvironmentUtilitiesTest.cpp; sourceTree = "<group>"; };
    10421044                44A622C114A0E2B60048515B /* WTFStringUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTFStringUtilities.h; sourceTree = "<group>"; };
    10431045                46397B941DC2C850009A78AE /* DOMNode.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DOMNode.mm; sourceTree = "<group>"; };
     
    20222024                                C045F9441385C2E900C0F3CD /* DownloadDecideDestinationCrash.cpp */,
    20232025                                07492B3A1DF8AE2D00633DE1 /* EnumerateMediaDevices.cpp */,
     2026                                448D7E451EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp */,
    20242027                                75F3133F18C171B70041CAEC /* EphemeralSessionPushStateNoHistoryCallback.cpp */,
    20252028                                1A5FEFDC1270E2A3000E2921 /* EvaluateJavaScript.cpp */,
     
    28112814                                7CCE7EBF1A411A7E00447C4C /* ElementAtPointInWebFrame.mm in Sources */,
    28122815                                07492B3B1DF8B14C00633DE1 /* EnumerateMediaDevices.cpp in Sources */,
     2816                                448D7E471EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp in Sources */,
    28132817                                7CCE7EEF1A411AE600447C4C /* EphemeralSessionPushStateNoHistoryCallback.cpp in Sources */,
    28142818                                7CCE7EF01A411AE600447C4C /* EvaluateJavaScript.cpp in Sources */,
Note: See TracChangeset for help on using the changeset viewer.