RESOLVED FIXED Bug 238693
Mark String(const char*) constructor as explicit
https://bugs.webkit.org/show_bug.cgi?id=238693
Summary Mark String(const char*) constructor as explicit
Chris Dumez
Reported 2022-04-01 19:53:11 PDT
Mark String(const char*) constructor as explicit to encourage developers to use ASCIILiteral and the ""_s suffix more. This constructor should also not be called if the input may contain UTF-8 since it treats input as latin1.
Attachments
WIP Patch (55.36 KB, patch)
2022-04-01 19:54 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (61.55 KB, patch)
2022-04-01 20:10 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (65.92 KB, patch)
2022-04-01 20:19 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (76.27 KB, patch)
2022-04-01 20:32 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (77.06 KB, patch)
2022-04-01 20:40 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (78.24 KB, patch)
2022-04-01 21:00 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (86.50 KB, patch)
2022-04-01 21:42 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (87.17 KB, patch)
2022-04-01 21:50 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (94.32 KB, patch)
2022-04-01 22:26 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (105.27 KB, patch)
2022-04-01 22:43 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (108.32 KB, patch)
2022-04-01 22:53 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (121.19 KB, patch)
2022-04-01 23:23 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (123.47 KB, patch)
2022-04-01 23:27 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (135.84 KB, patch)
2022-04-02 10:05 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (149.76 KB, patch)
2022-04-02 10:52 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (150.23 KB, patch)
2022-04-02 12:43 PDT, Chris Dumez
no flags
WIP Patch (150.91 KB, patch)
2022-04-02 12:44 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (170.16 KB, patch)
2022-04-02 13:36 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (171.86 KB, patch)
2022-04-02 14:07 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (174.48 KB, patch)
2022-04-02 14:35 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (178.42 KB, patch)
2022-04-02 14:49 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (189.26 KB, patch)
2022-04-02 15:04 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (192.68 KB, patch)
2022-04-02 15:31 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (193.80 KB, patch)
2022-04-02 15:42 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (194.47 KB, patch)
2022-04-02 15:59 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (197.10 KB, patch)
2022-04-02 16:25 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (228.32 KB, patch)
2022-04-02 16:47 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (230.32 KB, patch)
2022-04-02 17:18 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (232.47 KB, patch)
2022-04-02 17:42 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (238.56 KB, patch)
2022-04-02 18:12 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (239.50 KB, patch)
2022-04-02 18:23 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (253.36 KB, patch)
2022-04-02 19:00 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (253.54 KB, patch)
2022-04-02 19:10 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (261.61 KB, patch)
2022-04-02 19:44 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (274.17 KB, patch)
2022-04-02 20:25 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (274.89 KB, patch)
2022-04-02 20:47 PDT, Chris Dumez
no flags
WIP Patch (274.78 KB, patch)
2022-04-02 21:01 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (279.54 KB, patch)
2022-04-02 22:04 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (280.24 KB, patch)
2022-04-02 22:29 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (284.53 KB, patch)
2022-04-02 22:58 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (290.43 KB, patch)
2022-04-03 10:34 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (292.62 KB, patch)
2022-04-03 12:27 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (303.66 KB, patch)
2022-04-03 13:30 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (311.96 KB, patch)
2022-04-03 14:09 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (326.13 KB, patch)
2022-04-03 14:34 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (326.42 KB, patch)
2022-04-03 14:43 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (329.50 KB, patch)
2022-04-03 14:55 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (335.11 KB, patch)
2022-04-03 16:04 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (349.43 KB, patch)
2022-04-03 17:13 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (350.31 KB, patch)
2022-04-03 17:55 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (353.97 KB, patch)
2022-04-03 19:03 PDT, Chris Dumez
ews-feeder: commit-queue-
WIP Patch (355.15 KB, patch)
2022-04-03 20:15 PDT, Chris Dumez
no flags
Patch (405.51 KB, patch)
2022-04-03 21:25 PDT, Chris Dumez
no flags
Patch (405.40 KB, patch)
2022-04-04 11:08 PDT, Chris Dumez
no flags
Patch (405.94 KB, patch)
2022-04-04 12:32 PDT, Chris Dumez
no flags
Patch (405.99 KB, patch)
2022-04-05 10:09 PDT, Chris Dumez
no flags
Chris Dumez
Comment 1 2022-04-01 19:54:17 PDT
Created attachment 456430 [details] WIP Patch
Chris Dumez
Comment 2 2022-04-01 20:10:26 PDT
Created attachment 456431 [details] WIP Patch
Chris Dumez
Comment 3 2022-04-01 20:19:00 PDT
Created attachment 456433 [details] WIP Patch
Chris Dumez
Comment 4 2022-04-01 20:32:39 PDT
Created attachment 456434 [details] WIP Patch
Chris Dumez
Comment 5 2022-04-01 20:40:13 PDT
Created attachment 456437 [details] WIP Patch
Chris Dumez
Comment 6 2022-04-01 21:00:52 PDT
Created attachment 456441 [details] WIP Patch
Chris Dumez
Comment 7 2022-04-01 21:42:07 PDT
Created attachment 456443 [details] WIP Patch
Chris Dumez
Comment 8 2022-04-01 21:50:23 PDT
Created attachment 456445 [details] WIP Patch
Chris Dumez
Comment 9 2022-04-01 22:26:27 PDT
Created attachment 456446 [details] WIP Patch
Chris Dumez
Comment 10 2022-04-01 22:43:22 PDT
Created attachment 456448 [details] WIP Patch
Chris Dumez
Comment 11 2022-04-01 22:53:08 PDT
Created attachment 456449 [details] WIP Patch
Chris Dumez
Comment 12 2022-04-01 23:23:07 PDT
Created attachment 456451 [details] WIP Patch
Chris Dumez
Comment 13 2022-04-01 23:27:08 PDT
Created attachment 456452 [details] WIP Patch
Darin Adler
Comment 14 2022-04-02 09:44:29 PDT
Comment on attachment 456452 [details] WIP Patch View in context: https://bugs.webkit.org/attachment.cgi?id=456452&action=review Super-great; just love this. Given how many changes there still are here, I still think we might want another round or two of "prep" patch? The only tricky parts of this patch are places where we add String { } and maybe Latin-1 is not what we want, and it’s still subtle. Would be better if it was named String::fromLatin1, and the places where we add String::fromUTF8 without checking, because Latin-1 conversion can’t fail, but UTF-8 conversion can. > Source/WTF/wtf/posix/FileSystemPOSIX.cpp:246 > + auto fsFile = fileSystemRepresentation(path); Can fileSystemRepresentation return null when the string is non-null? Just wondering since the code we were deleting did have a check for null. I wonder if we should add checks for null at all the fileSystemRepresentation call sites. > Source/WTF/wtf/text/WTFString.h:84 > WTF_EXPORT_PRIVATE String(const LChar* characters); As another bit of cleanup, not nearly as important as the ASCIILiteral work, I wonder if we really need this one any more; how common is a null-terminated LChar array? > Source/WTF/wtf/text/WTFString.h:87 > + // as the String(ASCIILiteral) is more efficient. Also note that this constructor treats input as latin1. I would write Latin-1 rather than latin1, here and in the other comment above. > Source/WTF/wtf/text/WTFString.h:89 > + WTF_EXPORT_PRIVATE explicit String(const char* characters); I think we should go further and rename this to String::fromLatin1 now that there are so many fewer call sites. > Source/WebCore/Modules/mediastream/gstreamer/GStreamerMediaEndpoint.cpp:1100 > + auto exc = Exception { OperationError, error ? String::fromUTF8(error->message) : "Unknown Error"_s }; In cases like this I always wonder about the "illegal UTF-8" case where String::fromUTF8 will return null. Obviously not a large concern, but a small simmering worry in the back of my mind. > Source/WebCore/accessibility/ios/AXObjectCacheIOS.mm:47 > + ASCIILiteral name = ASCIILiteral::null(); Why doesn’t ASCIILiteral default to null without explicit initialization? > Source/WebCore/editing/ios/EditorIOS.mm:93 > + ASCIILiteral newValue = ASCIILiteral::null(); Ditto. > Source/WebCore/page/DebugPageOverlays.cpp:261 > + ASCIILiteral key { ASCIILiteral::null() }; > + ASCIILiteral name { ASCIILiteral::null() }; Same question. > Source/WebKit/Shared/API/Cocoa/WKRemoteObjectCoder.mm:59 > + if ([local compare:wire] == NSOrderedSame) isEqualToString: should be used here instead of compare: since it’s more efficient. Might also need to add a null check of wire, since I don’t think compare: or isEqualToString: work with nil. > Source/WebKit/Shared/API/Cocoa/WKRemoteObjectCoder.mm:68 > + char localType = [local characterAtIndex:i]; > + char wireType = [wire characterAtIndex:i]; This chops the characters down to "char", which is only OK if we are guaranteed the strings contain no non-ASCII characters. Not new, but irritating. I know the people who wrote this originally think the use of strchr is clever, but I would write this: auto map = [](unichar character) -> unichar { switch (character) { case 'B': // `bool` and `signed char` are interchangeable. return 'c'; default: return character; } }; if (map(localType) != map(wireType)) return false; Very easy to add any other equivalencies in the same fashion.
Chris Dumez
Comment 15 2022-04-02 10:05:38 PDT
Created attachment 456461 [details] WIP Patch
Chris Dumez
Comment 16 2022-04-02 10:26:05 PDT
(In reply to Darin Adler from comment #14) > Comment on attachment 456452 [details] > WIP Patch > > View in context: > https://bugs.webkit.org/attachment.cgi?id=456452&action=review > > Super-great; just love this. > > Given how many changes there still are here, I still think we might want > another round or two of "prep" patch? I was waiting to see how big the patch will get. Sadly, the remaining failures are for other ports so I am relying on EWS to find failures for me. I am hoping to make the constructor explicit as soon as possible though. The longer I wait, the more likely people will introduce new code without ASCIILiteral. > > The only tricky parts of this patch are places where we add String { } and > maybe Latin-1 is not what we want, and it’s still subtle. Would be better if > it was named String::fromLatin1, and the places where we add > String::fromUTF8 without checking, because Latin-1 conversion can’t fail, > but UTF-8 conversion can. > > > Source/WTF/wtf/posix/FileSystemPOSIX.cpp:246 > > + auto fsFile = fileSystemRepresentation(path); > > Can fileSystemRepresentation return null when the string is non-null? Just > wondering since the code we were deleting did have a check for null. I > wonder if we should add checks for null at all the fileSystemRepresentation > call sites. Looking at the Cocoa implementation of fileSystemRepresentation(), I believe it is indeed possible for it to return a null CString() when provided a non-null String as input. > > > Source/WTF/wtf/text/WTFString.h:84 > > WTF_EXPORT_PRIVATE String(const LChar* characters); > > As another bit of cleanup, not nearly as important as the ASCIILiteral work, > I wonder if we really need this one any more; how common is a > null-terminated LChar array? Agreed, I can look into that in a follow-up. Similarly, AtomString has an implicit constructor from `const char*` too that should probably be explicit. > > > Source/WTF/wtf/text/WTFString.h:87 > > + // as the String(ASCIILiteral) is more efficient. Also note that this constructor treats input as latin1. > > I would write Latin-1 rather than latin1, here and in the other comment > above. Will fix. > > > Source/WTF/wtf/text/WTFString.h:89 > > + WTF_EXPORT_PRIVATE explicit String(const char* characters); > > I think we should go further and rename this to String::fromLatin1 now that > there are so many fewer call sites. I was wondering about the same thing. I like it but I'd probably want to do this separately as this is going to be a fairly large (though mechanical) change. > > > Source/WebCore/Modules/mediastream/gstreamer/GStreamerMediaEndpoint.cpp:1100 > > + auto exc = Exception { OperationError, error ? String::fromUTF8(error->message) : "Unknown Error"_s }; > > In cases like this I always wonder about the "illegal UTF-8" case where > String::fromUTF8 will return null. Obviously not a large concern, but a > small simmering worry in the back of my mind. > > > Source/WebCore/accessibility/ios/AXObjectCacheIOS.mm:47 > > + ASCIILiteral name = ASCIILiteral::null(); > > Why doesn’t ASCIILiteral default to null without explicit initialization? Good question, I think we totally should have a default constructor. It would make ASCIILiteral a bit easier to use. > > > Source/WebCore/editing/ios/EditorIOS.mm:93 > > + ASCIILiteral newValue = ASCIILiteral::null(); > > Ditto. > > > Source/WebCore/page/DebugPageOverlays.cpp:261 > > + ASCIILiteral key { ASCIILiteral::null() }; > > + ASCIILiteral name { ASCIILiteral::null() }; > > Same question. > > > Source/WebKit/Shared/API/Cocoa/WKRemoteObjectCoder.mm:59 > > + if ([local compare:wire] == NSOrderedSame) > > isEqualToString: should be used here instead of compare: since it’s more > efficient. Good to know, will update. > > Might also need to add a null check of wire, since I don’t think compare: or > isEqualToString: work with nil. Ok. > > > Source/WebKit/Shared/API/Cocoa/WKRemoteObjectCoder.mm:68 > > + char localType = [local characterAtIndex:i]; > > + char wireType = [wire characterAtIndex:i]; > > This chops the characters down to "char", which is only OK if we are > guaranteed the strings contain no non-ASCII characters. Not new, but > irritating. I know the people who wrote this originally think the use of > strchr is clever, but I would write this: > > auto map = [](unichar character) -> unichar { > switch (character) { > case 'B': > // `bool` and `signed char` are interchangeable. > return 'c'; > default: > return character; > } > }; > if (map(localType) != map(wireType)) > return false; > > Very easy to add any other equivalencies in the same fashion.
Chris Dumez
Comment 17 2022-04-02 10:52:12 PDT
Created attachment 456462 [details] WIP Patch
Chris Dumez
Comment 18 2022-04-02 12:43:43 PDT
Created attachment 456466 [details] WIP Patch
Chris Dumez
Comment 19 2022-04-02 12:44:52 PDT
Created attachment 456467 [details] WIP Patch
Chris Dumez
Comment 20 2022-04-02 13:36:08 PDT
Created attachment 456470 [details] WIP Patch
Chris Dumez
Comment 21 2022-04-02 14:07:00 PDT
Created attachment 456471 [details] WIP Patch
Chris Dumez
Comment 22 2022-04-02 14:35:50 PDT
Created attachment 456473 [details] WIP Patch
Chris Dumez
Comment 23 2022-04-02 14:49:32 PDT
Created attachment 456475 [details] WIP Patch
Chris Dumez
Comment 24 2022-04-02 15:04:39 PDT
Created attachment 456476 [details] WIP Patch
Chris Dumez
Comment 25 2022-04-02 15:31:53 PDT
Created attachment 456478 [details] WIP Patch
Chris Dumez
Comment 26 2022-04-02 15:42:53 PDT
Created attachment 456479 [details] WIP Patch
Chris Dumez
Comment 27 2022-04-02 15:59:48 PDT
Created attachment 456481 [details] WIP Patch
Chris Dumez
Comment 28 2022-04-02 16:25:55 PDT
Created attachment 456483 [details] WIP Patch
Chris Dumez
Comment 29 2022-04-02 16:47:13 PDT
Created attachment 456484 [details] WIP Patch
Chris Dumez
Comment 30 2022-04-02 17:18:10 PDT
Created attachment 456485 [details] WIP Patch
Chris Dumez
Comment 31 2022-04-02 17:42:28 PDT
Created attachment 456486 [details] WIP Patch
Chris Dumez
Comment 32 2022-04-02 18:12:59 PDT
Created attachment 456487 [details] WIP Patch
Chris Dumez
Comment 33 2022-04-02 18:23:27 PDT
Created attachment 456488 [details] WIP Patch
Chris Dumez
Comment 34 2022-04-02 19:00:40 PDT
Created attachment 456489 [details] WIP Patch
Chris Dumez
Comment 35 2022-04-02 19:10:35 PDT
Created attachment 456490 [details] WIP Patch
Chris Dumez
Comment 36 2022-04-02 19:44:40 PDT
Created attachment 456491 [details] WIP Patch
Chris Dumez
Comment 37 2022-04-02 20:25:09 PDT
Created attachment 456493 [details] WIP Patch
Chris Dumez
Comment 38 2022-04-02 20:47:16 PDT
Created attachment 456494 [details] WIP Patch
Chris Dumez
Comment 39 2022-04-02 21:01:22 PDT
Created attachment 456495 [details] WIP Patch
Chris Dumez
Comment 40 2022-04-02 22:04:28 PDT
Created attachment 456496 [details] WIP Patch
Chris Dumez
Comment 41 2022-04-02 22:29:20 PDT
Created attachment 456497 [details] WIP Patch
Chris Dumez
Comment 42 2022-04-02 22:58:40 PDT
Created attachment 456498 [details] WIP Patch
Chris Dumez
Comment 43 2022-04-03 10:34:45 PDT
Created attachment 456506 [details] WIP Patch
Chris Dumez
Comment 44 2022-04-03 12:27:37 PDT
Created attachment 456509 [details] WIP Patch
Chris Dumez
Comment 45 2022-04-03 13:30:38 PDT
Created attachment 456512 [details] WIP Patch
Chris Dumez
Comment 46 2022-04-03 14:09:58 PDT
Created attachment 456514 [details] WIP Patch
Chris Dumez
Comment 47 2022-04-03 14:34:52 PDT
Created attachment 456516 [details] WIP Patch
Chris Dumez
Comment 48 2022-04-03 14:43:30 PDT
Created attachment 456517 [details] WIP Patch
Chris Dumez
Comment 49 2022-04-03 14:55:26 PDT
Created attachment 456519 [details] WIP Patch
Chris Dumez
Comment 50 2022-04-03 16:04:44 PDT
Created attachment 456522 [details] WIP Patch
Chris Dumez
Comment 51 2022-04-03 17:13:24 PDT
Created attachment 456523 [details] WIP Patch
Chris Dumez
Comment 52 2022-04-03 17:55:06 PDT
Created attachment 456524 [details] WIP Patch
Chris Dumez
Comment 53 2022-04-03 19:03:17 PDT
Created attachment 456526 [details] WIP Patch
Chris Dumez
Comment 54 2022-04-03 20:15:17 PDT
Created attachment 456527 [details] WIP Patch
Chris Dumez
Comment 55 2022-04-03 21:25:43 PDT
Chris Dumez
Comment 56 2022-04-04 11:08:52 PDT
Chris Dumez
Comment 57 2022-04-04 12:32:13 PDT
Chris Dumez
Comment 58 2022-04-04 14:52:14 PDT
Patch is ready for review.
Joseph Pecoraro
Comment 59 2022-04-04 16:55:11 PDT
Neat!!
Geoffrey Garen
Comment 60 2022-04-05 10:06:59 PDT
Comment on attachment 456606 [details] Patch r=me
Chris Dumez
Comment 61 2022-04-05 10:09:08 PDT
Chris Dumez
Comment 62 2022-04-05 11:54:42 PDT
Comment on attachment 456714 [details] Patch Clearing flags on attachment: 456714 Committed r292408 (249271@trunk): <https://commits.webkit.org/249271@trunk>
Chris Dumez
Comment 63 2022-04-05 11:54:48 PDT
All reviewed patches have been landed. Closing bug.
Radar WebKit Bug Importer
Comment 64 2022-04-05 11:55:23 PDT
Note You need to log in before you can comment on or make changes to this bug.