From d30c35787c4ea026325aaca03213a07f878514ca Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Wed, 17 May 2017 18:08:50 -0700 Subject: [PATCH] Fix Alignment of Hashed Structs (#287) * 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 --- CHANGELOG.md | 2 +- Source/ASImageNode.mm | 5 ++++- Source/ASTextNode.mm | 3 +++ Source/Private/ASHashing.h | 5 +++++ Source/TextKit/ASTextKitAttributes.mm | 3 +++ 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce58586f62..9d28b712a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/Source/ASImageNode.mm b/Source/ASImageNode.mm index 94e1440fbe..d36509d566 100644 --- a/Source/ASImageNode.mm +++ b/Source/ASImageNode.mm @@ -113,15 +113,18 @@ typedef void (^ASImageNodeDrawParametersBlock)(ASWeakMapEntry *entry); - (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, diff --git a/Source/ASTextNode.mm b/Source/ASTextNode.mm index d1334b21bb..5102ad6a0d 100644 --- a/Source/ASTextNode.mm +++ b/Source/ASTextNode.mm @@ -64,9 +64,12 @@ static NSString *ASTextNodeTruncationTokenAttributeName = @"ASTextNodeTruncation - (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 diff --git a/Source/Private/ASHashing.h b/Source/Private/ASHashing.h index c658a42d36..0fc8d72ff8 100644 --- a/Source/Private/ASHashing.h +++ b/Source/Private/ASHashing.h @@ -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); diff --git a/Source/TextKit/ASTextKitAttributes.mm b/Source/TextKit/ASTextKitAttributes.mm index 3e6dc80823..abe55c81d7 100755 --- a/Source/TextKit/ASTextKitAttributes.mm +++ b/Source/TextKit/ASTextKitAttributes.mm @@ -24,6 +24,8 @@ NSString *const ASTextKitEntityAttributeName = @"ck_entity"; size_t ASTextKitAttributes::hash() const { +#pragma clang diagnostic push +#pragma clang diagnostic warning "-Wpadded" struct { NSUInteger attrStringHash; NSUInteger truncationStringHash; @@ -35,6 +37,7 @@ size_t ASTextKitAttributes::hash() const NSUInteger shadowColorHash; CGFloat shadowOpacity; CGFloat shadowRadius; +#pragma clang diagnostic pop } data = { [attributedString hash], [truncationAttributedString hash],