Fix out-of-bounds string access in GNCIPv4AddressTest found by LibC++ hardening

LibC++ hardening detected an out-of-bounds write in `testInitFromDataWithLeadingEmptyBytes`. The `std::string` initialized with "\0\0\0\0" was empty, making the access `addressString[3]` invalid.

Updated the `std::string` constructor to specify the size, so it correctly includes the null bytes, preventing the crash.

PiperOrigin-RevId: 846853226
This commit is contained in:
hai007
2025-12-19 13:48:53 -08:00
committed by Copybara-Service
parent 9a25fc22ff
commit 46ad9a75ef
@@ -51,10 +51,11 @@
}
- (void)testInitFromDataWithLeadingEmptyBytes {
std::string addressString = "\0\0\0\0";
// Construct the string with an explicit size to include the null characters
std::string addressString("\0\0\0\0", 4);
addressString[3] = static_cast<char>(6);
NSData *addressData = [NSData dataWithBytes:addressString.data() length:4];
NSData *addressData = [NSData dataWithBytes:addressString.data() length:addressString.length()];
GNCIPv4Address *address = [GNCIPv4Address addressFromData:addressData];
XCTAssertEqual(address.byte1, 0);
XCTAssertEqual(address.byte2, 0);
@@ -63,13 +64,14 @@
}
- (void)testInitFromDataWithRealIP {
std::string addressString = "\0\0\0\0";
// Construct the string with an explicit size to include the null characters
std::string addressString("\0\0\0\0", 4);
addressString[0] = static_cast<char>(192);
addressString[1] = static_cast<char>(168);
addressString[2] = static_cast<char>(1);
addressString[3] = static_cast<char>(171);
NSData *addressData = [NSData dataWithBytes:addressString.data() length:4];
NSData *addressData = [NSData dataWithBytes:addressString.data() length:addressString.length()];
GNCIPv4Address *address = [GNCIPv4Address addressFromData:addressData];
XCTAssertEqual(address.byte1, 192);
XCTAssertEqual(address.byte2, 168);