Skip to content

Memory leak in HCICordioTransportClass #106

Closed
@Wenn0101

Description

@Wenn0101

I am working on porting this library to a new target, and I have found what I believe may be a memory leak in the HCICordioTransportClass::write() function.

In file \ArduinoBLE\src\utility\HCICordioTransport.cpp

size_t HCICordioTransportClass::write(const uint8_t* data, size_t length)
{
  if (!_begun) {
    return 0;
  }

  uint8_t packetLength = length - 1;
  uint8_t packetType   = data[0];

#if CORDIO_ZERO_COPY_HCI
  uint8_t* packet = (uint8_t*)WsfMsgAlloc(max(packetLength, MIN_WSF_ALLOC));

  memcpy(packet, &data[1], packetLength);

  return CordioHCIHook::getTransportDriver().write(packetType, packetLength, packet);
#else
  return CordioHCIHook::getTransportDriver().write(packetType, packetLength, (uint8_t*)&data[1]);
#endif
}

If CORDIO_ZERO_COPY_HCI is defined, memory is allocated, and I am never seeing it freed. Where are these messages supposed to be freed?

Locally I have made changes so that the allocated memory is freed after transmitting.This has solved my problem of running out of space and receiving a null pointer from WsfMsgAlloc(). However, I get the feeling that I am missing something about how this is supposed to work.

My code is:

size_t HCICordioTransportClass::write(const uint8_t* data, size_t length)
{
  if (!_begun) {
    return 0;
  }

  uint8_t packetLength = length - 1;
  uint8_t packetType   = data[0];

#if CORDIO_ZERO_COPY_HCI
  uint8_t* packet = (uint8_t*)WsfMsgAlloc(max(packetLength, MIN_WSF_ALLOC));
  if(packet)
  {
    memcpy(packet, &data[1], packetLength);

    uint16_t writtenBytes = CordioHCIHook::getTransportDriver().write(packetType, packetLength, packet);
    WsfMsgFree(packet);
    return writtenBytes;
  }
  else
  {
    return 0;
  }
#else
  return CordioHCIHook::getTransportDriver().write(packetType, packetLength, (uint8_t*)&data[1]);
#endif
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions