Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e793f836 authored by Hu Wang's avatar Hu Wang Committed by Gabriel Biren
Browse files

Fix sub-underfow when converting scan result's age from ms to us

Commit 5a441fcf ('Fix mul-overflow when converting scan result's
age from ms to us') fixes mul-overflow, but still sub-underflow
can happen in same line due to data type is u64, fix this by
moving check to earlier lines to ensure the sub does not results
in a negative value.

Bug: 387187200
Test: m
Change-Id: I81035e749d627fa3a7901b672241488963f3e046
parent eaabd037
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -3895,12 +3895,13 @@ bool convertCachedScanResultToAidl(const legacy_hal::wifi_cached_scan_result& le
        return false;
    }
    *aidl_scan_result = {};
    aidl_scan_result->timeStampInUs =
            ts_us - (static_cast<uint64_t>(legacy_scan_result.age_ms) * 1000);
    if (aidl_scan_result->timeStampInUs < 0) {
    // Ensure that subtracting does not result in a negative value
    uint64_t age_us = static_cast<uint64_t>(legacy_scan_result.age_ms) * 1000;
    if (ts_us < age_us) {
        aidl_scan_result->timeStampInUs = 0;
        return false;
    }
    aidl_scan_result->timeStampInUs = ts_us - age_us;
    size_t max_len_excluding_null = sizeof(legacy_scan_result.ssid) - 1;
    size_t ssid_len = strnlen((const char*)legacy_scan_result.ssid, max_len_excluding_null);
    aidl_scan_result->ssid =