diff --git a/Source/CBL_Attachment.m b/Source/CBL_Attachment.m index 16dc9a3aa..74b888acd 100644 --- a/Source/CBL_Attachment.m +++ b/Source/CBL_Attachment.m @@ -115,6 +115,16 @@ - (instancetype) initWithName: (NSString*)name *outStatus = kCBLStatusBadAttachment; return nil; } + // #817: There could be a revpos set to a parent revision: + id revPosObj = attachInfo[@"revpos"]; + if (revPosObj) { + int revPos = [$castIf(NSNumber, revPosObj) intValue]; + if (revPos <= 0) { + *outStatus = kCBLStatusBadAttachment; + return nil; + } + self->revpos = (unsigned)revPos; + } } else { *outStatus = kCBLStatusBadAttachment; return nil; diff --git a/Unit-Tests/DatabaseAttachment_Tests.m b/Unit-Tests/DatabaseAttachment_Tests.m index 05a171acd..12f4de462 100644 --- a/Unit-Tests/DatabaseAttachment_Tests.m +++ b/Unit-Tests/DatabaseAttachment_Tests.m @@ -484,6 +484,27 @@ - (void) test14_FollowingAttachments { } +- (void) test16_FollowWithRevpos { + NSDictionary* attachInfo = $dict({@"content_type", @"text/plain"}, + {@"digest", @"md5-DaUdFsLh8FKLbcBIDlU57g=="}, + {@"follows", @YES}, + {@"length", @51200}, + {@"revpos", @2}); + CBLStatus status; + CBL_Attachment *attachment = [[CBL_Attachment alloc] initWithName: @"attachment" + info: attachInfo + status: &status]; + Assert(attachment); + Assert(status != kCBLStatusBadAttachment); + NSDictionary* stub = [attachment asStubDictionary]; + AssertEqualish(stub, $dict({@"content_type", @"text/plain"}, + {@"digest", @"sha1-AAAAAAAAAAAAAAAAAAAAAAAAAAA="}, + {@"length", @51200}, + {@"revpos", @2}, + {@"stub", @YES})); +} + + static NSDictionary* attachmentsDict(NSData* data, NSString* name, NSString* type, BOOL gzipped) { if (gzipped) data = [NSData gtm_dataByGzippingData: data];