Skip to content

Commit e0f07cf

Browse files
authored
Merge pull request #81 from ImagingDataCommons/fix-double-free
fix a double free error
2 parents 345eda5 + 3661aa4 commit e0f07cf

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* improve memory usage [bgilbert]
55
* fix docs build with LLVM != 14 [bgilbert]
66
* improve thread safety docs [mollyclaretechcyte]
7+
* fix a double free error and clarify docs on pointer ownership [dtatsis]
78

89
## 1.0.5, 9/10/23
910

include/dicom/dicom.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,11 @@ DcmDataSet *dcm_dataset_clone(DcmError **error, const DcmDataSet *dataset);
10261026
* :param dataset: Pointer to Data Set
10271027
* :param element: Pointer to Data Element
10281028
*
1029-
* The object takes over ownership of the memory referenced by `element`
1030-
* and frees it when the object is destroyed or if the insert operation fails.
1029+
* On success, the dataset takes over ownership of `element`
1030+
* and frees it when the dataset is destroyed.
1031+
*
1032+
* If the insert operation fails, ownership does not pass and the caller is
1033+
* responsible for freeing `element`.
10311034
*
10321035
* :return: Whether insert operation was successful
10331036
*/
@@ -1198,8 +1201,11 @@ DcmSequence *dcm_sequence_create(DcmError **error);
11981201
* :param seq: Pointer to Sequence
11991202
* :param item: Data Set item
12001203
*
1201-
* The object takes over ownership of the memory referenced by `item`
1202-
* and frees it when the object is destroyed or if the append operation fails.
1204+
* On success, the sequence takes over ownership of `item`
1205+
* and frees it when the sequence is destroyed.
1206+
*
1207+
* If the append fails, ownership does not pass and the caller is
1208+
* responsible for freeing `item`.
12031209
*
12041210
* :return: Whether append operation was successful
12051211
*/

src/dicom-data.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,7 @@ DcmDataSet *dcm_dataset_clone(DcmError **error, const DcmDataSet *dataset)
13911391
return NULL;
13921392
}
13931393
if (!dcm_dataset_insert(error, cloned_dataset, cloned_element)) {
1394+
dcm_element_destroy(cloned_element);
13941395
dcm_dataset_destroy(cloned_dataset);
13951396
return NULL;
13961397
}
@@ -1435,7 +1436,6 @@ bool dcm_dataset_insert(DcmError **error,
14351436
"Element already exists",
14361437
"Inserting Data Element '%08x' into Data Set failed",
14371438
element->tag);
1438-
dcm_element_destroy(element);
14391439
return false;
14401440
}
14411441

@@ -1707,7 +1707,6 @@ DcmDataSet *dcm_sequence_steal(DcmError **error,
17071707
}
17081708

17091709
DcmDataSet *result = seq_item->dataset;
1710-
//dcm_dataset_lock(result);
17111710
seq_item->dataset = NULL;
17121711
// this will free the SequenceItem
17131712
utarray_erase(seq->items, index, 1);

0 commit comments

Comments
 (0)