Skip to content

Commit

Permalink
Fix Alignment of Hashed Structs (TextureGroup#287)
Browse files Browse the repository at this point in the history
* Fix alignment of ASImageNodeContentsKey struct to fix hashing

* Change the change log by logging a change

* Add the world's stupidest explicit cast

* Actually its simpler
  • Loading branch information
Adlai-Holler authored and bernieperez committed Apr 25, 2018
1 parent 754caea commit d529763
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
- [Layout] Extract layout implementation code into it's own subcategories [Michael Schneider] (https://github.com/maicki)[#272](https://github.com/TextureGroup/Texture/pull/272)
- [Fix] Fix a potential crash when cell nodes that need layout are deleted during the same runloop. [Adlai Holler](https://github.com/Adlai-Holler) [#279](https://github.com/TextureGroup/Texture/pull/279)
- [Batch fetching] Add ASBatchFetchingDelegate that takes scroll velocity and remaining time into account [Huy Nguyen](https://github.com/nguyenhuy) [#281](https://github.com/TextureGroup/Texture/pull/281)

- [Fix] Fix a major regression in our image node contents caching. [Adlai Holler](https://github.com/Adlai-Holler) [#287](https://github.com/TextureGroup/Texture/pull/287)
5 changes: 4 additions & 1 deletion Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,18 @@ - (BOOL)isEqual:(id)object

- (NSUInteger)hash
{
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wpadded"
struct {
NSUInteger imageHash;
CGSize backingSize;
CGRect imageDrawRect;
BOOL isOpaque;
NSInteger isOpaque;
NSUInteger backgroundColorHash;
void *willDisplayNodeContentWithRenderingContext;
void *didDisplayNodeContentWithRenderingContext;
void *imageModificationBlock;
#pragma clang diagnostic pop
} data = {
_image.hash,
_backingSize,
Expand Down
3 changes: 3 additions & 0 deletions Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ @implementation ASTextNodeRendererKey

- (NSUInteger)hash
{
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wpadded"
struct {
size_t attributesHash;
CGSize constrainedSize;
#pragma clang diagnostic pop
} data = {
_attributes.hash(),
_constrainedSize
Expand Down
5 changes: 5 additions & 0 deletions Source/Private/ASHashing.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ ASDISPLAYNODE_EXTERN_C_BEGIN
* _bounds.size
* };
* return ASHashBytes(&data, sizeof(data));
*
* @warning: If a struct has padding, any fields that are intiailized in {}
* will have garbage data for their padding, which will break this hash! Either
* use `pragma clang diagnostic warning "-Wpadded"` around your struct definition
* or manually initialize the fields of your struct (`myStruct.x = 7;` etc).
*/
NSUInteger ASHashBytes(void *bytes, size_t length);

Expand Down
3 changes: 3 additions & 0 deletions Source/TextKit/ASTextKitAttributes.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

size_t ASTextKitAttributes::hash() const
{
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wpadded"
struct {
NSUInteger attrStringHash;
NSUInteger truncationStringHash;
Expand All @@ -35,6 +37,7 @@
NSUInteger shadowColorHash;
CGFloat shadowOpacity;
CGFloat shadowRadius;
#pragma clang diagnostic pop
} data = {
[attributedString hash],
[truncationAttributedString hash],
Expand Down

0 comments on commit d529763

Please sign in to comment.