Compare strings by value instead of reference

Summary:
LLD, our new iOS linker, is an ongoing effort to migrate our old outdated ld64 linker. As part of our effort to rollout LLD to all apps, we are making sure LLD reaches parity with ld64.

Due to Identical Code Folding (ICF), LLD and ld64 handles strings differently. LLD treats each string as a separate object in memory even if the values of the strings are the same. ld64 happens to aggregate these values across files. This behavior creates a subtle difference on our codebase when we start comparing by value or by reference.

`char * ` fields from `RawPropsKey.h` are using `==` which compares by its address. Here, we cast the buffer to a string to make the comparison, while avoiding the cast if one happens to be null.

Changelog: [Internal]

Reviewed By: int3, JoshuaGross

Differential Revision: D30444176

fbshipit-source-id: 74216926803adbece05206ddd8478cc3c8e6812e
This commit is contained in:
Vincent Lee 2021-08-25 13:23:40 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 2fb102b601
Коммит 25c5d194ad
1 изменённых файлов: 10 добавлений и 2 удалений

Просмотреть файл

@ -51,10 +51,18 @@ RawPropsKey::operator std::string() const noexcept {
return std::string{buffer, length}; return std::string{buffer, length};
} }
static bool areFieldsEqual(char const *lhs, char const *rhs) {
if (lhs == nullptr || rhs == nullptr) {
return lhs == rhs;
}
return std::string(lhs) == std::string(rhs);
}
bool operator==(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept { bool operator==(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept {
// Note: We check the name first. // Note: We check the name first.
return lhs.name == rhs.name && lhs.prefix == rhs.prefix && return areFieldsEqual(lhs.name, rhs.name) &&
lhs.suffix == rhs.suffix; areFieldsEqual(lhs.prefix, rhs.prefix) &&
areFieldsEqual(lhs.suffix, rhs.suffix);
} }
bool operator!=(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept { bool operator!=(RawPropsKey const &lhs, RawPropsKey const &rhs) noexcept {