Skip to content

Possible leak of the devices buffer in doInquire() on every inquiry #491

Description

@OvOhao

Possible leak of the devices buffer in doInquire() on every inquiry

I found a possible native memory leak in doInquire(). It allocates the devices
array with malloc and returns it by value, but neither caller ever frees it.

File: src/linux/DeviceINQ.cc

Function: DeviceINQ::doInquire (consumed by DeviceINQ::InquireSync and
InquireWorker::HandleOKCallback)

inquiryResult.devices = (bt_device*)malloc(num_rsp * sizeof(bt_device));
...
free( ii );            // the ii buffer is freed
close( sock );
return inquiryResult;  // inquiryResult.devices is never freed

doInquire() returns inquiryResult by value with a heap devices array. Both callers
read inquiryResult.devices[i] in a loop and then discard the struct without ever
calling free(inquiryResult.devices). Every device scan (inquire() / inquireSync())
leaks the whole array (num_rsp * sizeof(bt_device)). The adjacent ii buffer is
freed, which makes the omission clear.

Suggested fix: free(inquiryResult.devices) in both callers after they finish consuming
it (or have doInquire return ownership through a type that frees it).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions