Changeset 244917 in webkit


Ignore:
Timestamp:
May 3, 2019 12:42:53 PM (5 years ago)
Author:
Brent Fulgham
Message:

Use more efficient path resolution logic
https://bugs.webkit.org/show_bug.cgi?id=197389
<rdar://problem/50268491>

Reviewed by Maciej Stachowiak.

The code in SandboxExtensionsCocoa.mm 'resolveSymlinksInPath' is pretty inefficient, and tries to reproduce (badly)
logic that is already provided by the operating system.

To make matters worse, 'resolvePathForSandboxExtension' was effectively performing the work of fully resolving
symlinks twice, since NSString's 'stringByStandardizingPath' method does some of this already.

Instead, we should just use NSString's 'stringByResolvingSymlinksInPath', which does the symlink resolution
using more efficient logic than our 'resolveSymlinksInPath' code.

  • Shared/Cocoa/SandboxExtensionCocoa.mm:

(WebKit::resolveSymlinksInPath): Removed.
(WebKit::resolvePathForSandboxExtension): Remove redundant call to 'resolveSymlinksInPath', and switches from
'stringByStandardizingPath' to 'stringByResolvingSymlinksInPath', which can take the place of both calls.
(WebKit::stringByResolvingSymlinksInPath): Switch to call 'stringByResolvingSymlinksInPath'.

Location:
trunk/Source/WebKit
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r244916 r244917  
     12019-05-03  Brent Fulgham  <bfulgham@apple.com>
     2
     3        Use more efficient path resolution logic
     4        https://bugs.webkit.org/show_bug.cgi?id=197389
     5        <rdar://problem/50268491>
     6
     7        Reviewed by Maciej Stachowiak.
     8
     9        The code in SandboxExtensionsCocoa.mm 'resolveSymlinksInPath' is pretty inefficient, and tries to reproduce (badly)
     10        logic that is already provided by the operating system.
     11
     12        To make matters worse, 'resolvePathForSandboxExtension' was effectively performing the work of fully resolving
     13        symlinks twice, since NSString's 'stringByStandardizingPath' method does some of this already.
     14
     15        Instead, we should just use NSString's 'stringByResolvingSymlinksInPath', which does the symlink resolution
     16        using more efficient logic than our 'resolveSymlinksInPath' code.
     17
     18        * Shared/Cocoa/SandboxExtensionCocoa.mm:
     19        (WebKit::resolveSymlinksInPath): Removed.
     20        (WebKit::resolvePathForSandboxExtension): Remove redundant call to 'resolveSymlinksInPath', and switches from
     21        'stringByStandardizingPath' to 'stringByResolvingSymlinksInPath', which can take the place of both calls.
     22        (WebKit::stringByResolvingSymlinksInPath): Switch to call 'stringByResolvingSymlinksInPath'.
     23
    1242019-05-02  Dean Jackson  <dino@apple.com>
    225
  • trunk/Source/WebKit/Shared/Cocoa/SandboxExtensionCocoa.mm

    r244747 r244917  
    11/*
    2  * Copyright (C) 2010-2016 Apple Inc. All rights reserved.
     2 * Copyright (C) 2010-2019 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    3232#import "Decoder.h"
    3333#import "Encoder.h"
    34 #import <sys/stat.h>
    3534#import <wtf/FileSystem.h>
    3635#import <wtf/spi/darwin/SandboxSPI.h>
     
    226225}
    227226
    228 static CString resolveSymlinksInPath(const CString& path)
    229 {
    230     struct stat statBuf;
    231 
    232     // Check if this file exists.
    233     if (!stat(path.data(), &statBuf)) {
    234         char resolvedName[PATH_MAX];
    235 
    236         return realpath(path.data(), resolvedName);
    237     }
    238 
    239     const char* slashPtr = strrchr(path.data(), '/');
    240     if (slashPtr == path.data())
    241         return path;
    242 
    243     size_t parentDirectoryLength = slashPtr - path.data();
    244     if (parentDirectoryLength >= PATH_MAX)
    245         return CString();
    246 
    247     // Get the parent directory.
    248     char parentDirectory[PATH_MAX];
    249     memcpy(parentDirectory, path.data(), parentDirectoryLength);
    250     parentDirectory[parentDirectoryLength] = '\0';
    251 
    252     // Resolve it.
    253     CString resolvedParentDirectory = resolveSymlinksInPath(CString(parentDirectory));
    254     if (resolvedParentDirectory.isNull())
    255         return CString();
    256 
    257     size_t lastPathComponentLength = path.length() - parentDirectoryLength;
    258     size_t resolvedPathLength = resolvedParentDirectory.length() + lastPathComponentLength;
    259     if (resolvedPathLength >= PATH_MAX)
    260         return CString();
    261 
    262     // Combine the resolved parent directory with the last path component.
    263     char* resolvedPathBuffer;
    264     CString resolvedPath = CString::newUninitialized(resolvedPathLength, resolvedPathBuffer);
    265     memcpy(resolvedPathBuffer, resolvedParentDirectory.data(), resolvedParentDirectory.length());
    266     memcpy(resolvedPathBuffer + resolvedParentDirectory.length(), slashPtr, lastPathComponentLength);
    267 
    268     return resolvedPath;
    269 }
    270 
    271227String stringByResolvingSymlinksInPath(const String& path)
    272228{
    273     return String::fromUTF8(resolveSymlinksInPath(path.utf8()));
     229    return [(NSString *)path stringByResolvingSymlinksInPath];
    274230}
    275231
     
    289245String resolvePathForSandboxExtension(const String& path)
    290246{
    291     // FIXME: Do we need both resolveSymlinksInPath() and -stringByStandardizingPath?
    292     CString fileSystemPath = FileSystem::fileSystemRepresentation([(NSString *)path stringByStandardizingPath]);
    293     if (fileSystemPath.isNull()) {
    294         LOG_ERROR("Could not create a valid file system representation for the string '%s' of length %lu", fileSystemPath.data(), fileSystemPath.length());
     247    String resolvedPath = stringByResolvingSymlinksInPath(path);
     248    if (resolvedPath.isEmpty()) {
     249        LOG_ERROR("Could not create a valid file system representation for the string '%s' of length %lu", resolvedPath.utf8().data(), resolvedPath.length());
    295250        return { };
    296251    }
    297252
    298     CString standardizedPath = resolveSymlinksInPath(fileSystemPath);
    299     return String::fromUTF8(standardizedPath);
     253    return resolvedPath;
    300254}
    301255
Note: See TracChangeset for help on using the changeset viewer.