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

Commit 9a3b8c81 authored by Chris Manton's avatar Chris Manton
Browse files

Properly clean up security device record

Clean up any dynamic aspects of the device security record

Bug: 265800692
Tag: #refactor
Test: gd/cert/run

Change-Id: I7f27aaf6ecea7e26084cb183e004f7357cce32c2
parent f6ac08c3
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -337,7 +337,12 @@ typedef struct tBTM_CB {
#endif
    security_mode = initial_security_mode;
    pairing_bda = RawAddress::kAny;
    sec_dev_rec = list_new(osi_free);
    sec_dev_rec = list_new([](void* ptr) {
      // Invoke destructor for all record objects and reset to default
      // initialized value so memory may be properly freed
      *((tBTM_SEC_DEV_REC*)ptr) = {};
      osi_free(ptr);
    });

    /* Initialize BTM component structures */
    btm_inq_vars.Init(); /* Inquiry Database and Structures */
+27 −0
Original line number Diff line number Diff line
@@ -79,6 +79,10 @@ using testing::StrEq;
using testing::StrictMock;
using testing::Test;

// NOTE: The production code allows N+1 device records.
constexpr size_t kBtmSecMaxDeviceRecords =
    static_cast<size_t>(BTM_SEC_MAX_DEVICE_RECORDS + 1);

std::string Hex16(int n) {
  std::ostringstream oss;
  oss << "0x" << std::hex << std::setw(4) << std::setfill('0') << n;
@@ -373,3 +377,26 @@ TEST_F(StackBtmTest, btm_ble_sec_req_act_text) {
  ASSERT_EQ("BTM_BLE_SEC_REQ_ACT_DISCARD",
            btm_ble_sec_req_act_text(BTM_BLE_SEC_REQ_ACT_DISCARD));
}

TEST_F(StackBtmWithInitFreeTest, btm_sec_allocate_dev_rec__all) {
  tBTM_SEC_DEV_REC* records[kBtmSecMaxDeviceRecords];

  // Fill up the records
  for (size_t i = 0; i < kBtmSecMaxDeviceRecords; i++) {
    ASSERT_EQ(i, list_length(btm_cb.sec_dev_rec));
    records[i] = btm_sec_allocate_dev_rec();
    ASSERT_NE(nullptr, records[i]);
  }

  // Second pass up the records
  for (size_t i = 0; i < kBtmSecMaxDeviceRecords; i++) {
    ASSERT_EQ(kBtmSecMaxDeviceRecords, list_length(btm_cb.sec_dev_rec));
    records[i] = btm_sec_allocate_dev_rec();
    ASSERT_NE(nullptr, records[i]);
  }

  // NOTE: The memory allocated for each record is automatically
  // allocated by the btm module and freed when the device record
  // list is freed.
  // Further, the memory for each record is reused when necessary.
}