Bug 173690 - [TestWebKitAPI] Fix misuse of -[NSData dataWithBytesNoCopy:length:] with global variables
Summary: [TestWebKitAPI] Fix misuse of -[NSData dataWithBytesNoCopy:length:] with glob...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Tools / Tests (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: David Kilzer (:ddkilzer)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-06-21 20:12 PDT by David Kilzer (:ddkilzer)
Modified: 2017-06-22 06:15 PDT (History)
6 users (show)

See Also:


Attachments
Patch v1 (4.70 KB, patch)
2017-06-21 20:13 PDT, David Kilzer (:ddkilzer)
no flags Details | Formatted Diff | Diff
Patch v2 (4.82 KB, patch)
2017-06-21 22:49 PDT, David Kilzer (:ddkilzer)
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Kilzer (:ddkilzer) 2017-06-21 20:12:00 PDT
Per documentation, -[NSData dataWithBytesNoCopy:length:] takes
ownership of malloc()-ed memory, then frees it when it's
released.  These tests were passing global variables into the
method, which is not malloc()-ed memory, which violations the
API contract.

The fix is to switch to using -[NSData dataWithBytes:length:] so
that the global C strings are copied into malloc()-ed memory.

Caught by the clang static analyzer.
Comment 1 David Kilzer (:ddkilzer) 2017-06-21 20:13:09 PDT
Created attachment 313579 [details]
Patch v1
Comment 2 Chris Dumez 2017-06-21 21:17:04 PDT
Comment on attachment 313579 [details]
Patch v1

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

> Tools/TestWebKitAPI/Tests/WebKit2Cocoa/IconLoadingDelegate.mm:-145
> -    RetainPtr<IconLoadingSchemeHandler> handler = adoptNS([[IconLoadingSchemeHandler alloc] initWithData:[NSData dataWithBytesNoCopy:(void*)mainBytes length:sizeof(mainBytes)]]);

Why not keep the existing code but add "freeWhenDone:NO" ?
https://developer.apple.com/documentation/foundation/nsdata/1416020-initwithbytesnocopy?language=objc
Comment 3 David Kilzer (:ddkilzer) 2017-06-21 22:41:51 PDT
(In reply to Chris Dumez from comment #2)
> Comment on attachment 313579 [details]
> Patch v1
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=313579&action=review
> 
> > Tools/TestWebKitAPI/Tests/WebKit2Cocoa/IconLoadingDelegate.mm:-145
> > -    RetainPtr<IconLoadingSchemeHandler> handler = adoptNS([[IconLoadingSchemeHandler alloc] initWithData:[NSData dataWithBytesNoCopy:(void*)mainBytes length:sizeof(mainBytes)]]);
> 
> Why not keep the existing code but add "freeWhenDone:NO" ?
> https://developer.apple.com/documentation/foundation/nsdata/1416020-
> initwithbytesnocopy?language=objc

Sure, could do that as well.  I saw the shorter method first!
Comment 4 David Kilzer (:ddkilzer) 2017-06-21 22:49:33 PDT
Created attachment 313586 [details]
Patch v2
Comment 5 David Kilzer (:ddkilzer) 2017-06-22 06:15:16 PDT
Committed r218694: <http://trac.webkit.org/changeset/218694>