Skip to content

Commit 213a981

Browse files
committed
io/ompio: plug memory leaks
as reported by Coverity with CIDs 1369022 and 1369023
1 parent c09899f commit 213a981

File tree

1 file changed

+67
-54
lines changed

1 file changed

+67
-54
lines changed

ompi/mca/io/ompio/io_ompio_aggregators.c

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Copyright (c) 2008-2016 University of Houston. All rights reserved.
1414
* Copyright (c) 2011-2015 Cisco Systems, Inc. All rights reserved.
1515
* Copyright (c) 2012-2013 Inria. All rights reserved.
16-
* Copyright (c) 2015 Research Organization for Information Science
16+
* Copyright (c) 2015-2016 Research Organization for Information Science
1717
* and Technology (RIST). All rights reserved.
1818
* $COPYRIGHT$
1919
*
@@ -122,13 +122,14 @@ int mca_io_ompio_fview_based_grouping(mca_io_ompio_file_t *fh,
122122
start_offsets_lens = (OMPI_MPI_OFFSET_TYPE* )malloc (3 * fh->f_size * sizeof(OMPI_MPI_OFFSET_TYPE));
123123
if (NULL == start_offsets_lens) {
124124
opal_output (1, "OUT OF MEMORY\n");
125-
return OMPI_ERR_OUT_OF_RESOURCE;
125+
ret = OMPI_ERR_OUT_OF_RESOURCE;
126+
goto exit;
126127
}
127128
end_offsets = (OMPI_MPI_OFFSET_TYPE* )malloc (fh->f_size * sizeof(OMPI_MPI_OFFSET_TYPE));
128129
if (NULL == end_offsets) {
129130
opal_output (1, "OUT OF MEMORY\n");
130-
free(start_offsets_lens);
131-
return OMPI_ERR_OUT_OF_RESOURCE;
131+
ret = OMPI_ERR_OUT_OF_RESOURCE;
132+
goto exit;
132133
}
133134

134135
//Allgather start offsets across processes in a group on aggregator
@@ -141,9 +142,7 @@ int mca_io_ompio_fview_based_grouping(mca_io_ompio_file_t *fh,
141142
fh->f_comm,
142143
fh->f_comm->c_coll.coll_allgather_module);
143144
if ( OMPI_SUCCESS != ret ) {
144-
free (start_offsets_lens);
145-
free (end_offsets);
146-
return ret;
145+
goto exit;
147146
}
148147

149148

@@ -180,10 +179,17 @@ int mca_io_ompio_fview_based_grouping(mca_io_ompio_file_t *fh,
180179
}
181180

182181
*num_groups = p+1;
183-
free (start_offsets_lens);
184-
free (end_offsets);
182+
ret = OMPI_SUCCESS;
183+
184+
exit:
185+
if (NULL != start_offsets_lens) {
186+
free (start_offsets_lens);
187+
}
188+
if (NULL != end_offsets) {
189+
free(end_offsets);
190+
}
185191

186-
return OMPI_SUCCESS;
192+
return ret;
187193
}
188194

189195
int mca_io_ompio_cart_based_grouping(mca_io_ompio_file_t *ompio_fh)
@@ -499,19 +505,15 @@ int mca_io_ompio_create_groups(mca_io_ompio_file_t *fh,
499505

500506
if (NULL != start_offsets_lens) {
501507
free (start_offsets_lens);
502-
start_offsets_lens = NULL;
503508
}
504509
if (NULL != end_offsets) {
505510
free (end_offsets);
506-
end_offsets = NULL;
507511
}
508512
if(NULL != aggr_bytes_per_group){
509513
free(aggr_bytes_per_group);
510-
aggr_bytes_per_group = NULL;
511514
}
512515
if( NULL != decision_list){
513516
free(decision_list);
514-
decision_list = NULL;
515517
}
516518

517519

@@ -524,7 +526,7 @@ int mca_io_ompio_merge_initial_groups(mca_io_ompio_file_t *fh,
524526
int is_aggregator){
525527

526528
OMPI_MPI_OFFSET_TYPE sum_bytes = 0;
527-
MPI_Request *sendreq = NULL;
529+
MPI_Request *sendreqs = NULL;
528530

529531
int start = 0;
530532
int end = 0;
@@ -633,8 +635,8 @@ int mca_io_ompio_merge_initial_groups(mca_io_ompio_file_t *fh,
633635

634636
//New aggregators communicate new grouping info to the groups
635637
if(is_new_aggregator){
636-
sendreq = (MPI_Request *)malloc ( 2 *fh->f_procs_per_group * sizeof(MPI_Request));
637-
if (NULL == sendreq) {
638+
sendreqs = (MPI_Request *)malloc ( 2 *fh->f_procs_per_group * sizeof(MPI_Request));
639+
if (NULL == sendreqs) {
638640
return OMPI_ERR_OUT_OF_RESOURCE;
639641
}
640642
//Communicate grouping info
@@ -650,11 +652,10 @@ int mca_io_ompio_merge_initial_groups(mca_io_ompio_file_t *fh,
650652
OMPIO_PROCS_PER_GROUP_TAG,
651653
MCA_PML_BASE_SEND_STANDARD,
652654
fh->f_comm,
653-
&sendreq[r++]));
655+
sendreqs + r++));
654656
if ( OMPI_SUCCESS != ret ) {
655657
opal_output (1, "mca_io_ompio_merge_initial_groups: error in Isend\n");
656-
free ( sendreq );
657-
return ret;
658+
goto exit;
658659
}
659660
//new aggregator sends distribution of process to all its new members
660661
ret = MCA_PML_CALL(isend(fh->f_procs_in_group,
@@ -664,11 +665,10 @@ int mca_io_ompio_merge_initial_groups(mca_io_ompio_file_t *fh,
664665
OMPIO_PROCS_IN_GROUP_TAG,
665666
MCA_PML_BASE_SEND_STANDARD,
666667
fh->f_comm,
667-
&sendreq[r++]));
668+
sendreqs + r++));
668669
if ( OMPI_SUCCESS != ret ) {
669670
opal_output (1, "mca_io_ompio_merge_initial_groups: error in Isend 2\n");
670-
free ( sendreq );
671-
return ret;
671+
goto exit;
672672
}
673673

674674
}
@@ -709,8 +709,12 @@ int mca_io_ompio_merge_initial_groups(mca_io_ompio_file_t *fh,
709709
}
710710

711711
if(is_new_aggregator) {
712-
ret = ompi_request_wait_all (r, sendreq, MPI_STATUSES_IGNORE);
713-
free (sendreq);
712+
ret = ompi_request_wait_all (r, sendreqs, MPI_STATUSES_IGNORE);
713+
}
714+
715+
exit:
716+
if (NULL != sendreqs) {
717+
free(sendreqs);
714718
}
715719

716720
return ret;
@@ -883,15 +887,15 @@ int mca_io_ompio_merge_groups(mca_io_ompio_file_t *fh,
883887
sizes_old_group = (int*)malloc(num_merge_aggrs * sizeof(int));
884888
if (NULL == sizes_old_group) {
885889
opal_output (1, "OUT OF MEMORY\n");
886-
return OMPI_ERR_OUT_OF_RESOURCE;
890+
goto exit;
887891
}
888892

889893

890894
displs = (int*)malloc(num_merge_aggrs * sizeof(int));
891895
if (NULL == displs) {
892896
opal_output (1, "OUT OF MEMORY\n");
893-
free(sizes_old_group);
894-
return OMPI_ERR_OUT_OF_RESOURCE;
897+
ret = OMPI_ERR_OUT_OF_RESOURCE;
898+
goto exit;
895899
}
896900

897901

@@ -909,9 +913,7 @@ int mca_io_ompio_merge_groups(mca_io_ompio_file_t *fh,
909913
fh->f_comm);
910914

911915
if ( OMPI_SUCCESS != ret ) {
912-
free (displs);
913-
free (sizes_old_group);
914-
return ret;
916+
goto exit;
915917
}
916918
fh->f_procs_per_group = 0;
917919

@@ -928,9 +930,8 @@ int mca_io_ompio_merge_groups(mca_io_ompio_file_t *fh,
928930
fh->f_procs_in_group = (int*)malloc (fh->f_procs_per_group * sizeof(int));
929931
if (NULL == fh->f_procs_in_group) {
930932
opal_output (1, "OUT OF MEMORY\n");
931-
free(sizes_old_group);
932-
free(displs);
933-
return OMPI_ERR_OUT_OF_RESOURCE;
933+
ret = OMPI_ERR_OUT_OF_RESOURCE;
934+
goto exit;
934935
}
935936

936937
//New aggregator also collects the grouping distribution
@@ -948,8 +949,13 @@ int mca_io_ompio_merge_groups(mca_io_ompio_file_t *fh,
948949
num_merge_aggrs,
949950
fh->f_comm);
950951

951-
free (displs);
952-
free (sizes_old_group);
952+
exit:
953+
if (NULL != displs) {
954+
free (displs);
955+
}
956+
if (NULL != sizes_old_group) {
957+
free (sizes_old_group);
958+
}
953959

954960
return ret;
955961

@@ -1013,11 +1019,8 @@ int mca_io_ompio_split_a_group(mca_io_ompio_file_t *fh,
10131019
*min_cci = cci[i];
10141020
}
10151021
}
1016-
//if cci is not needed anymore
1017-
if (NULL != cci) {
1018-
free (cci);
1019-
cci = NULL;
1020-
}
1022+
1023+
free (cci);
10211024
return OMPI_SUCCESS;
10221025
}
10231026

@@ -1115,12 +1118,6 @@ int mca_io_ompio_prepare_to_group(mca_io_ompio_file_t *fh,
11151118
opal_output (1, "OUT OF MEMORY\n");
11161119
return OMPI_ERR_OUT_OF_RESOURCE;
11171120
}
1118-
end_offsets_tmp = (OMPI_MPI_OFFSET_TYPE* )malloc (fh->f_init_procs_per_group * sizeof(OMPI_MPI_OFFSET_TYPE));
1119-
if (NULL == end_offsets_tmp) {
1120-
opal_output (1, "OUT OF MEMORY\n");
1121-
free(start_offsets_lens_tmp);
1122-
return OMPI_ERR_OUT_OF_RESOURCE;
1123-
}
11241121

11251122
//Gather start offsets across processes in a group on aggregator
11261123
ret = fcoll_base_coll_allgather_array (start_offset_len,
@@ -1135,7 +1132,12 @@ int mca_io_ompio_prepare_to_group(mca_io_ompio_file_t *fh,
11351132
fh->f_comm);
11361133
if ( OMPI_SUCCESS != ret ) {
11371134
opal_output (1, "mca_io_ompio_prepare_to_grou[: error in fcoll_base_coll_allgather_array\n");
1138-
return ret;
1135+
goto exit;
1136+
}
1137+
end_offsets_tmp = (OMPI_MPI_OFFSET_TYPE* )malloc (fh->f_init_procs_per_group * sizeof(OMPI_MPI_OFFSET_TYPE));
1138+
if (NULL == end_offsets_tmp) {
1139+
opal_output (1, "OUT OF MEMORY\n");
1140+
goto exit;
11391141
}
11401142
for( k = 0 ; k < fh->f_init_procs_per_group; k++){
11411143
end_offsets_tmp[k] = start_offsets_lens_tmp[3*k] + start_offsets_lens_tmp[3*k+1];
@@ -1159,15 +1161,14 @@ int mca_io_ompio_prepare_to_group(mca_io_ompio_file_t *fh,
11591161
aggr_bytes_per_group_tmp = (OMPI_MPI_OFFSET_TYPE*)malloc (fh->f_init_num_aggrs * sizeof(OMPI_MPI_OFFSET_TYPE));
11601162
if (NULL == aggr_bytes_per_group_tmp) {
11611163
opal_output (1, "OUT OF MEMORY\n");
1162-
return OMPI_ERR_OUT_OF_RESOURCE;
1164+
ret = OMPI_ERR_OUT_OF_RESOURCE;
1165+
goto exit;
11631166
}
11641167
decision_list_tmp = (int* )malloc (fh->f_init_num_aggrs * sizeof(int));
11651168
if (NULL == decision_list_tmp) {
11661169
opal_output (1, "OUT OF MEMORY\n");
1167-
free(aggr_bytes_per_group_tmp);
1168-
free(start_offsets_lens_tmp);
1169-
free(end_offsets_tmp);
1170-
return OMPI_ERR_OUT_OF_RESOURCE;
1170+
ret = OMPI_ERR_OUT_OF_RESOURCE;
1171+
goto exit;
11711172
}
11721173
//Communicate bytes per group between all aggregators
11731174
ret = fcoll_base_coll_allgather_array (bytes_per_group,
@@ -1182,7 +1183,8 @@ int mca_io_ompio_prepare_to_group(mca_io_ompio_file_t *fh,
11821183
fh->f_comm);
11831184
if ( OMPI_SUCCESS != ret ) {
11841185
opal_output (1, "mca_io_ompio_prepare_to_grou[: error in fcoll_base_coll_allgather_array 2\n");
1185-
return ret;
1186+
free(decision_list_tmp);
1187+
goto exit;
11861188
}
11871189

11881190
for( i = 0; i < fh->f_init_num_aggrs; i++){
@@ -1262,6 +1264,17 @@ int mca_io_ompio_prepare_to_group(mca_io_ompio_file_t *fh,
12621264
fh->f_init_procs_per_group,
12631265
fh->f_comm);
12641266

1267+
exit:
1268+
if (NULL != aggr_bytes_per_group_tmp) {
1269+
free(aggr_bytes_per_group_tmp);
1270+
}
1271+
if (NULL != start_offsets_lens_tmp) {
1272+
free(start_offsets_lens_tmp);
1273+
}
1274+
if (NULL != end_offsets_tmp) {
1275+
free(end_offsets_tmp);
1276+
}
1277+
12651278
return ret;
12661279
}
12671280

0 commit comments

Comments
 (0)