RESOLVED FIXED226743
Reduce use of reinterpret_cast<> in the codebase
https://bugs.webkit.org/show_bug.cgi?id=226743
Summary Reduce use of reinterpret_cast<> in the codebase
Chris Dumez
Reported 2021-06-07 14:21:22 PDT
Reduce use of reinterpret_cast<> in the codebase.
Attachments
Patch (83.29 KB, patch)
2021-06-07 14:23 PDT, Chris Dumez
no flags
Patch (83.30 KB, patch)
2021-06-07 14:26 PDT, Chris Dumez
no flags
Patch (82.54 KB, patch)
2021-06-07 14:56 PDT, Chris Dumez
ews-feeder: commit-queue-
Patch (82.49 KB, patch)
2021-06-07 17:02 PDT, Chris Dumez
no flags
Patch (82.43 KB, patch)
2021-06-07 20:02 PDT, Chris Dumez
no flags
Patch (82.80 KB, patch)
2021-06-08 07:57 PDT, Chris Dumez
ews-feeder: commit-queue-
Patch (82.80 KB, patch)
2021-06-08 08:16 PDT, Chris Dumez
ews-feeder: commit-queue-
Chris Dumez
Comment 1 2021-06-07 14:23:33 PDT
Chris Dumez
Comment 2 2021-06-07 14:26:17 PDT
Chris Dumez
Comment 3 2021-06-07 14:56:49 PDT
Chris Dumez
Comment 4 2021-06-07 17:02:07 PDT
Chris Dumez
Comment 5 2021-06-07 20:02:46 PDT
Darin Adler
Comment 6 2021-06-07 22:50:08 PDT
Comment on attachment 430803 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=430803&action=review > Source/WTF/wtf/persistence/PersistentCoders.cpp:101 > + encoder.encodeFixedLengthData(string.characters8(), length * sizeof(LChar)); sizeof(LChar) can really be left out > Source/WTF/wtf/text/CString.h:65 > + CString(const LChar* data, size_t length) : CString(reinterpret_cast<const char*>(data), length) { } This seems a little bit questionable. It’s only OK if we want a Latin-1 C string, but typically we want UTF-8 C strings. The meaning of the "L" in "LChar" is Latin-1. On the other hand, it does "herd" all the reinterpret_cast calls into one place by overloading for uint8_t as well as char. But I think using LChar is misleading. > Source/WTF/wtf/text/CString.h:75 > + const LChar* characters8() const { return reinterpret_cast<const LChar*>(data()); } Same thought. The real characters8() function is always correct, if it can be called it’s guaranteed to have Latin-1. This is more questionable. It can be used to incorrectly treat UTF-8 as Latin-1. So I would not have named this characters8() nor used the LChar type. Although I suppose that’t correct if this CString happens to contain Latin-1. Maybe a different name would be better that isn’t claiming the string is Latin-1. Maybe we should teach CString how to track, in debug versions at least or at compile time, what encoding the characters are in, since we probably only use it for UTF-8 and Latin-1, not all arbitrary other types. > Source/WebCore/Modules/websockets/WebSocketHandshake.cpp:334 > + constexpr char httpVersionStaticPreambleLiteral[] = "HTTP/"; > + StringView httpVersionStaticPreamble(httpVersionStaticPreambleLiteral, strlen(httpVersionStaticPreambleLiteral)); > if (!httpStatusLine.startsWith(httpVersionStaticPreamble)) > return false; This code seems like overkill. I would write this: constexpr char preamble[] = "HTTP/"; if (!httpStatusLine.startsWith(preamble)) return false; unsigned preambleLength = strlen(preamble); Maybe someone thought they were optimizing by writing the other thing? > Source/WebCore/platform/graphics/avfoundation/InbandTextTrackPrivateAVF.cpp:535 > StringBuilder header; > - header.appendCharacters(reinterpret_cast<const unsigned char*>(CFDataGetBytePtr(webvttHeaderData)), length); > + header.appendCharacters(CFDataGetBytePtr(webvttHeaderData), length); > header.append("\n\n"); The following would be more efficient than using StringBuilder: auto header = makeString(StringView { CFDataGetBytePtr(webvttHeaderData), length }, "\n\n");
Chris Dumez
Comment 7 2021-06-08 07:57:30 PDT
Chris Dumez
Comment 8 2021-06-08 08:16:35 PDT
EWS
Comment 9 2021-06-08 10:35:23 PDT
Committed r278619 (238604@main): <https://commits.webkit.org/238604@main> All reviewed patches have been landed. Closing bug and clearing flags on attachment 430845 [details].
Radar WebKit Bug Importer
Comment 10 2021-06-08 10:36:20 PDT
Note You need to log in before you can comment on or make changes to this bug.