Skip to content

dcmqrdb: fix memory leaks on error paths in startMoveRequest/startFin… - #170

Open
eomjaeeun wants to merge 1 commit into
DCMTK:masterfrom
eomjaeeun:fix/startmoverequest-leak
Open

eomjaeeun wants to merge 1 commit into
DCMTK:masterfrom
eomjaeeun:fix/startmoverequest-leak

Conversation

@eomjaeeun

Copy link
Copy Markdown

Summary

startMoveRequest() and startFindRequest() in dcmqrdb/libsrc/dcmqrdbi.cc leak memory on several error return paths. One reproducible path is an invalid QueryRetrieveLevel value in a C-GET or C-MOVE request, which causes the function to return early without freeing elem.PValueField or the accumulated findRequestList. Since the server does not crash on this error, repeated requests can cause cumulative memory growth.

Problem & Root Cause

Both functions parse the DICOM dataset in a loop, allocating each tag's value with malloc (line 2066):

elem.PValueField = (char*)malloc((size_t)(elem.ValueLength + 1));

The normal path frees this at the end of the loop body (line 2136). However, four early-return paths in startMoveRequest() skip this cleanup:

  1. Invalid QueryRetrieveLevel (line 2111) — leaks elem.PValueField + accumulated findRequestList entries. Reproducible with a malformed network request.
  2. malloc failure (line 2067) — leaks accumulated findRequestList.
  3. new DB_ElementList failure (line 2123) — leaks elem.PValueField + accumulated findRequestList.
  4. DB_CounterList allocation failure (line 2231) — leaks accumulated moveCounterList entries, and also misses a DB_unlock() call.

startFindRequest() has three analogous paths. It already frees elem.PValueField on its invalid-level path, but still leaks the findRequestList.

Notably, the !qrLevelFound path at line 2142 already has correct cleanup (DB_FreeElementList + NULL), providing an existing cleanup pattern.

Changes

  • dcmqrdb/libsrc/dcmqrdbi.cc
    • startMoveRequest(), paths 1–3: add the appropriate resource cleanup (free(elem.PValueField) and/or DB_FreeElementList(handle_->findRequestList)) before each affected error return, matching the existing cleanup at line 2142.
    • startMoveRequest(), path 4: free accumulated moveCounterList entries and add the missing DB_unlock() call before the error return.
    • startFindRequest(): add DB_FreeElementList(handle_->findRequestList); handle_->findRequestList = NULL; (and free(elem.PValueField) where applicable) before each affected error return.

Path 1 fix (invalid query level in startMoveRequest):

// Before (line 2107–2112):
} else {
    status->setStatus(STATUS_MOVE_Failed_UnableToProcess);
    return (QR_EC_IndexDatabaseError);
}

// After:
} else {
    free(elem.PValueField);
    DB_FreeElementList(handle_->findRequestList);
    handle_->findRequestList = NULL;
    status->setStatus(STATUS_MOVE_Failed_UnableToProcess);
    return (QR_EC_IndexDatabaseError);
}

The same pattern applies to the other early-return paths in both functions, with each path freeing the resources that have been allocated up to that point.

Verification

Built dcmqrscp with AddressSanitizer (-fsanitize=address) on current master (da4682c). Sent 20 C-GET requests with QueryRetrieveLevel = "INVALID" via pynetdicom:

from pynetdicom import AE
from pynetdicom.sop_class import PatientRootQueryRetrieveInformationModelGet
from pydicom.dataset import Dataset

ae = AE()
ae.add_requested_context(PatientRootQueryRetrieveInformationModelGet)

for i in range(20):
    assoc = ae.associate("127.0.0.1", 11112)
    if assoc.is_established:
        ds = Dataset()
        ds.QueryRetrieveLevel = "INVALID"
        ds.PatientID = "test123"
        ds.StudyInstanceUID = "1.2.3"
        responses = assoc.send_c_get(ds, PatientRootQueryRetrieveInformationModelGet)
        for (status, _) in responses:
            pass
        assoc.release()

The invalid QueryRetrieveLevel path in startMoveRequest() was verified dynamically using AddressSanitizer. The remaining error paths were identified through source code inspection.

Before fix — LeakSanitizer output on dcmqrscp shutdown:

==1416286==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 180 byte(s) in 20 object(s) allocated from:
    #0 malloc
    #1 DcmQueryRetrieveIndexDatabaseHandle::startMoveRequest(...)
       dcmqrdb/libsrc/dcmqrdbi.cc:2066
    #2 DcmQueryRetrieveGetContext::callbackHandler(...)
       dcmqrdb/libsrc/dcmqrcbg.cc:81
    #3 getCallback dcmqrdb/libsrc/dcmqrsrv.cc:60
    #4 DIMSE_getProvider(...)
       dcmnet/libsrc/dimget.cc:332
    #5 DcmQueryRetrieveSCP::getSCP(...)
       dcmqrdb/libsrc/dcmqrsrv.cc:333

SUMMARY: AddressSanitizer: 180 byte(s) leaked in 20 allocation(s).

20 requests × 9 bytes (8-byte DICOM CS-padded value + 1-byte null terminator) = 180 bytes, matching the LeakSanitizer report.

After fix — same test, clean exit, no LeakSanitizer output:

(no leak detected)

The changes are limited to error-handling paths and do not modify normal request processing.

Thanks ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant