Bug 127469 - [GTK] Avoid unnecessary string duplications in FileSystemGtk
Summary: [GTK] Avoid unnecessary string duplications in FileSystemGtk
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Platform (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: Gtk
Depends on:
Blocks:
 
Reported: 2014-01-23 00:58 PST by Carlos Garcia Campos
Modified: 2014-01-28 00:11 PST (History)
2 users (show)

See Also:


Attachments
Patch (9.04 KB, patch)
2014-01-23 01:01 PST, Carlos Garcia Campos
no flags Details | Formatted Diff | Diff
Patch (9.05 KB, patch)
2014-01-23 02:23 PST, Carlos Garcia Campos
no flags Details | Formatted Diff | Diff
Updated patch (9.14 KB, patch)
2014-01-23 10:35 PST, Carlos Garcia Campos
mrobinson: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Carlos Garcia Campos 2014-01-23 00:58:56 PST
We are using fileSystemRepresentation() everywhere internally which returns a CString that always duplicates the string.
Comment 1 Carlos Garcia Campos 2014-01-23 01:01:29 PST
Created attachment 221956 [details]
Patch
Comment 2 Sergio Villar Senin 2014-01-23 01:28:37 PST
Comment on attachment 221956 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=221956&action=review

Looks good to me but your changes unveil an already existing bug. See inline...

> Source/WebCore/platform/gtk/FileSystemGtk.cpp:84
> +    GUniquePtr<gchar> display(g_filename_to_utf8(filename.get(), 0, nullptr, nullptr, nullptr));

This definitely sounds like a bug to me. We're passing 0 as the string length. We should either use -1 if null terminated or the actual string length if known.
Comment 3 Carlos Garcia Campos 2014-01-23 02:22:32 PST
(In reply to comment #2)
> (From update of attachment 221956 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=221956&action=review
> 
> Looks good to me but your changes unveil an already existing bug. See inline...
> 
> > Source/WebCore/platform/gtk/FileSystemGtk.cpp:84
> > +    GUniquePtr<gchar> display(g_filename_to_utf8(filename.get(), 0, nullptr, nullptr, nullptr));
> 
> This definitely sounds like a bug to me. We're passing 0 as the string length. We should either use -1 if null terminated or the actual string length if known.

You are right, that should be -1
Comment 4 Carlos Garcia Campos 2014-01-23 02:23:51 PST
Created attachment 221964 [details]
Patch

Good catch Sergio! Patch updated
Comment 5 Martin Robinson 2014-01-23 06:14:47 PST
Comment on attachment 221956 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=221956&action=review

> Source/WebCore/ChangeLog:11
> +        We are using fileSystemRepresentation() everywhere internally
> +        which returns a CString that always duplicates the string.
> +        Add unescapedFilename() internal helper function that returns a
> +        GUniquePtr and used it everywhere instead of fileSystemRepresentation().

I'm not sure this is true. CStrings contain a reference-counted internal buffer.
Comment 6 Carlos Garcia Campos 2014-01-23 06:28:27 PST
(In reply to comment #5)
> (From update of attachment 221956 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=221956&action=review
> 
> > Source/WebCore/ChangeLog:11
> > +        We are using fileSystemRepresentation() everywhere internally
> > +        which returns a CString that always duplicates the string.
> > +        Add unescapedFilename() internal helper function that returns a
> > +        GUniquePtr and used it everywhere instead of fileSystemRepresentation().
> 
> I'm not sure this is true. CStrings contain a reference-counted internal buffer.

The constructor always allocates memory and copies the given string.
Comment 7 Carlos Garcia Campos 2014-01-23 06:32:08 PST
Check it yourself:

http://trac.webkit.org/browser/trunk/Source/WTF/wtf/text/CString.cpp#L63
Comment 8 Martin Robinson 2014-01-23 09:12:23 PST
Comment on attachment 221964 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=221964&action=review

You're right. I neglected to notice that fileSystemRepresentation was converting from a c string into a CString.

> Source/WebCore/platform/gtk/FileSystemGtk.cpp:62
> +    if (path.isEmpty())
> +        return nullptr;
> +    return GUniquePtr<char>(g_uri_unescape_string(path.utf8().data(), nullptr));
> +}

This doesn't appear to be doing the right thing for Windows. In Windows we just need to return path.utf8() to maintain the current behavior.
Comment 9 Carlos Garcia Campos 2014-01-23 09:28:12 PST
Comment on attachment 221964 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=221964&action=review

>> Source/WebCore/platform/gtk/FileSystemGtk.cpp:62
>> +}
> 
> This doesn't appear to be doing the right thing for Windows. In Windows we just need to return path.utf8() to maintain the current behavior.

You are right, I forgot the windows ifdef. Will update the patch, thanks!
Comment 10 Carlos Garcia Campos 2014-01-23 10:35:04 PST
Created attachment 221998 [details]
Updated patch

Fixed the windows behaviour.
Comment 11 Carlos Garcia Campos 2014-01-28 00:11:13 PST
Committed r162921: <http://trac.webkit.org/changeset/162921>