Skip to content

Commit 1f55b92

Browse files
authored
getByte into the actual buffer, not into the byte array struct (#5227)
Also: - add a test, confirmed failing without these changes - rename variable to better represent what it is
1 parent 096fc67 commit 1f55b92

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

Firebase/CoreDiagnostics/FIRCDLibrary/FIRCoreDiagnostics.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ + (NSString *)deviceModel {
236236
* @param data The data to copy into the new bytes array.
237237
*/
238238
pb_bytes_array_t *FIREncodeData(NSData *data) {
239-
pb_bytes_array_t *pbBytes = calloc(1, PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
240-
if (pbBytes != NULL) {
241-
[data getBytes:pbBytes range:NSMakeRange(0, data.length)];
242-
pbBytes->size = (pb_size_t)data.length;
239+
pb_bytes_array_t *pbBytesArray = calloc(1, PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
240+
if (pbBytesArray != NULL) {
241+
[data getBytes:pbBytesArray->bytes length:data.length];
242+
pbBytesArray->size = (pb_size_t)data.length;
243243
}
244-
return pbBytes;
244+
return pbBytesArray;
245245
}
246246

247247
/** Maps a service string to the representative nanopb enum.

GoogleDataTransportCCTSupport/GDTCCTLibrary/GDTCCTNanopbHelpers.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
4343
}
4444

4545
pb_bytes_array_t *GDTCCTEncodeData(NSData *data) {
46-
pb_bytes_array_t *pbBytes = calloc(1, PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
47-
if (pbBytes != NULL) {
48-
[data getBytes:pbBytes range:NSMakeRange(0, data.length)];
49-
pbBytes->size = (pb_size_t)data.length;
46+
pb_bytes_array_t *pbBytesArray = calloc(1, PB_BYTES_ARRAY_T_ALLOCSIZE(data.length));
47+
if (pbBytesArray != NULL) {
48+
[data getBytes:pbBytesArray->bytes length:data.length];
49+
pbBytesArray->size = (pb_size_t)data.length;
5050
}
51-
return pbBytes;
51+
return pbBytesArray;
5252
}
5353

5454
#pragma mark - CCT object constructors

GoogleDataTransportCCTSupport/GDTCCTTests/Unit/GDTCCTNanopbHelpersTest.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,15 @@ - (void)testEncodingProtoAboveDefaultOSThreshold {
161161
pb_release(gdt_cct_BatchedLogRequest_fields, &batch);
162162
}
163163

164+
- (void)testSimpleByteEncodingConsistency {
165+
NSData *data = [@"Simple." dataUsingEncoding:NSUTF8StringEncoding];
166+
pb_bytes_array_t *bytesArray = GDTCCTEncodeData(data);
167+
XCTAssertEqual(bytesArray->size, data.length);
168+
XCTAssertTrue(bytesArray->bytes);
169+
XCTAssertEqualObjects([[NSString alloc] initWithBytes:bytesArray->bytes
170+
length:bytesArray->size
171+
encoding:NSUTF8StringEncoding],
172+
@"Simple.");
173+
}
174+
164175
@end

0 commit comments

Comments
 (0)