Skip to content

Commit 0f6cea2

Browse files
authored
Set mutable data ref lengths, otherwise iOS will 0 out the buffer (#4849)
* Set mutable data ref lengths, otherwise iOS will 0 out the buffer * Same change in FIRCoreDiagnostics * Add a unit test * Update changelogs and a comment
1 parent 9d202ae commit 0f6cea2

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

Firebase/CoreDiagnostics/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v1.2.1
2+
- Fixed a bug that would manifest if a proto ended up being > 16,320 bytes.
3+
4+
# v1.2.0
5+
- Added basic watchOS support.
6+
17
# v1.1.2
28
- Switch the backend for diagnostics to FLL from CCT.
39

Firebase/CoreDiagnostics/FIRCDLibrary/FIRCoreDiagnostics.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ - (NSData *)transportBytes {
125125
// Encode a 2nd time to actually get the bytes from it.
126126
size_t bufferSize = sizestream.bytes_written;
127127
CFMutableDataRef dataRef = CFDataCreateMutable(CFAllocatorGetDefault(), bufferSize);
128+
CFDataSetLength(dataRef, bufferSize);
128129
pb_ostream_t ostream = pb_ostream_from_buffer((void *)CFDataGetBytePtr(dataRef), bufferSize);
129130
if (!pb_encode(&ostream, logs_proto_mobilesdk_ios_ICoreConfiguration_fields, &_config)) {
130131
GDTCORLogError(GDTCORMCETransportBytesError, @"Error in nanopb encoding for bytes: %s",

GoogleDataTransportCCTSupport/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v1.4.1
2+
- Fixed a bug that would manifest if a proto ended up being > 16,320 bytes.
3+
14
# v1.4.0
25
- Added the CSH backend and consolidated the CCT, FLL, and CSH backends.
36

GoogleDataTransportCCTSupport/GDTCCTLibrary/GDTCCTNanopbHelpers.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@
5757
// Encode a 2nd time to actually get the bytes from it.
5858
size_t bufferSize = sizestream.bytes_written;
5959
CFMutableDataRef dataRef = CFDataCreateMutable(CFAllocatorGetDefault(), bufferSize);
60+
CFDataSetLength(dataRef, bufferSize);
6061
pb_ostream_t ostream = pb_ostream_from_buffer((void *)CFDataGetBytePtr(dataRef), bufferSize);
6162
if (!pb_encode(&ostream, gdt_cct_BatchedLogRequest_fields, batchedLogRequest)) {
6263
GDTCORLogError(GDTCORMCEGeneralError, @"Error in nanopb encoding for bytes: %s",
6364
PB_GET_ERROR(&ostream));
6465
}
65-
CFDataSetLength(dataRef, ostream.bytes_written);
6666

6767
return CFBridgingRelease(dataRef);
6868
}

GoogleDataTransportCCTSupport/GDTCCTTests/Unit/GDTCCTNanopbHelpersTest.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,45 @@ - (void)testBytesAreDecodable {
130130
pb_release(gdt_cct_BatchedLogRequest_fields, &decodedBatch);
131131
}
132132

133+
/** Tests that creating a message above the apparent threshold of 16320 bytes works. */
134+
- (void)testEncodingProtoAboveDefaultOSThreshold {
135+
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
136+
NSArray *testData = @[
137+
@"message-32347456.dat", @"message-35458880.dat", @"message-39882816.dat",
138+
@"message-40043840.dat", @"message-40657984.dat"
139+
];
140+
NSMutableSet *storedEvents = [[NSMutableSet alloc] init];
141+
// 250 messages results in a total size of 16337 which is > 16320, the apparent OS limit. Changing
142+
// to 249 would've caused test to pass previously.
143+
for (int i = 0; i < 250; i++) {
144+
NSString *dataFile = testData[arc4random_uniform((uint32_t)testData.count)];
145+
NSData *messageData = [NSData dataWithContentsOfURL:[testBundle URLForResource:dataFile
146+
withExtension:nil]];
147+
XCTAssertNotNil(messageData);
148+
NSString *cachePath = NSTemporaryDirectory();
149+
NSString *filePath = [cachePath
150+
stringByAppendingPathComponent:[NSString stringWithFormat:@"test-%lf.txt",
151+
CFAbsoluteTimeGetCurrent()]];
152+
[messageData writeToFile:filePath atomically:YES];
153+
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
154+
XCTAssertNotNil(fileURL);
155+
XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
156+
[storedEvents addObject:[_generator generateStoredEvent:GDTCOREventQosDefault fileURL:fileURL]];
157+
}
158+
gdt_cct_BatchedLogRequest batch = gdt_cct_BatchedLogRequest_init_default;
159+
XCTAssertNoThrow((batch = GDTCCTConstructBatchedLogRequest(@{@"1018" : storedEvents})));
160+
NSData *data = GDTCCTEncodeBatchedLogRequest(&batch);
161+
XCTAssertNotNil(data);
162+
const char *bytes = (const char *)[data bytes];
163+
BOOL allZeroes = YES;
164+
for (int i = 0; i < data.length; i++) {
165+
char aByte = bytes[i];
166+
if (aByte != '\0') {
167+
allZeroes = NO;
168+
}
169+
}
170+
XCTAssertFalse(allZeroes);
171+
pb_release(gdt_cct_BatchedLogRequest_fields, &batch);
172+
}
173+
133174
@end

GoogleDataTransportCCTSupport/GDTCCTTests/Unit/TestServer/GDTCCTTestServer.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ - (NSData *)responseData {
9393
// Encode a 2nd time to actually get the bytes from it.
9494
size_t bufferSize = sizestream.bytes_written;
9595
CFMutableDataRef dataRef = CFDataCreateMutable(CFAllocatorGetDefault(), bufferSize);
96+
CFDataSetLength(dataRef, bufferSize);
9697
pb_ostream_t ostream = pb_ostream_from_buffer((void *)CFDataGetBytePtr(dataRef), bufferSize);
9798
if (!pb_encode(&sizestream, gdt_cct_LogResponse_fields, &logResponse)) {
9899
GDTCORAssert(NO, @"Error in nanopb encoding for bytes: %s", PB_GET_ERROR(&ostream));
99100
}
100-
CFDataSetLength(dataRef, ostream.bytes_written);
101101
pb_release(gdt_cct_LogResponse_fields, &logResponse);
102102
return CFBridgingRelease(dataRef);
103103
}

0 commit comments

Comments
 (0)