From 46ad9a75efdb2501d6f31d91e7b374083e34c036 Mon Sep 17 00:00:00 2001 From: hai007 Date: Fri, 19 Dec 2025 13:47:47 -0800 Subject: [PATCH] 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 --- .../apple/Mediums/WiFiCommon/Tests/GNCIPAddressTest.mm | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/platform/implementation/apple/Mediums/WiFiCommon/Tests/GNCIPAddressTest.mm b/internal/platform/implementation/apple/Mediums/WiFiCommon/Tests/GNCIPAddressTest.mm index f607e8e0..0e109437 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiCommon/Tests/GNCIPAddressTest.mm +++ b/internal/platform/implementation/apple/Mediums/WiFiCommon/Tests/GNCIPAddressTest.mm @@ -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(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(192); addressString[1] = static_cast(168); addressString[2] = static_cast(1); addressString[3] = static_cast(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);