Skip to content

Commit 1293d9c

Browse files
committed
free memory correctly in case of an error. Fixes CID 131540 and CID 1315419
1 parent 0aa3049 commit 1293d9c

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

ompi/mca/sharedfp/individual/sharedfp_individual_file_open.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ int mca_sharedfp_individual_file_open (struct ompi_communicator_t *comm,
6060
if ( NULL == sh ){
6161
opal_output(0, "mca_sharedfp_individual_file_open: Error, unable to malloc "
6262
"f_sharedfp_ptr struct\n");
63+
free (shfileHandle );
6364
return OMPI_ERR_OUT_OF_RESOURCE;
6465
}
6566

@@ -94,6 +95,8 @@ int mca_sharedfp_individual_file_open (struct ompi_communicator_t *comm,
9495
MPI_INFO_NULL, datafilehandle, false);
9596
if ( OMPI_SUCCESS != err) {
9697
opal_output(0, "mca_sharedfp_individual_file_open: Error during datafile file open\n");
98+
free (shfileHandle );
99+
free (sh);
97100
return err;
98101
}
99102

@@ -107,14 +110,31 @@ int mca_sharedfp_individual_file_open (struct ompi_communicator_t *comm,
107110

108111
/* metadata filename created by appending .metadata.$rank to the original filename*/
109112
metadatafilename = (char*) malloc ( len );
113+
if ( NULL == metadatafilename ) {
114+
free (shfileHandle );
115+
free (sh);
116+
opal_output(0, "mca_sharedfp_individual_file_open: Error during memory allocation\n");
117+
return OMPI_ERR_OUT_OF_RESOURCE;
118+
}
110119
snprintf ( metadatafilename, len, "%s%s%d", filename, ".metadata.",rank);
111120

112121
metadatafilehandle = (mca_io_ompio_file_t *)malloc(sizeof(mca_io_ompio_file_t));
122+
if ( NULL == metadatafilehandle ) {
123+
free (shfileHandle );
124+
free (sh);
125+
free (metadatafilename);
126+
opal_output(0, "mca_sharedfp_individual_file_open: Error during memory allocation\n");
127+
return OMPI_ERR_OUT_OF_RESOURCE;
128+
}
113129
err = ompio_io_ompio_file_open ( MPI_COMM_SELF,metadatafilename,
114130
MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE,
115131
MPI_INFO_NULL, metadatafilehandle, false);
116132
if ( OMPI_SUCCESS != err) {
117133
opal_output(0, "mca_sharedfp_individual_file_open: Error during metadatafile file open\n");
134+
free (shfileHandle );
135+
free (sh);
136+
free (metadatafilename);
137+
free (metadatafilehandle);
118138
return err;
119139
}
120140

ompi/mca/sharedfp/lockedfile/sharedfp_lockedfile_file_open.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ int mca_sharedfp_lockedfile_file_open (struct ompi_communicator_t *comm,
6161
sh = (struct mca_sharedfp_base_data_t*)malloc(sizeof(struct mca_sharedfp_base_data_t));
6262
if ( NULL == sh){
6363
opal_output(0, "mca_sharedfp_lockedfile_file_open: Error, unable to malloc f_sharedfp_ptr struct\n");
64+
free ( shfileHandle);
6465
return OMPI_ERR_OUT_OF_RESOURCE;
6566
}
6667
/*Populate the sh file structure based on the implementation*/
@@ -80,6 +81,8 @@ int mca_sharedfp_lockedfile_file_open (struct ompi_communicator_t *comm,
8081
module_data = (struct mca_sharedfp_lockedfile_data*)malloc(sizeof(struct mca_sharedfp_lockedfile_data));
8182
if ( NULL == module_data ) {
8283
printf("mca_sharedfp_lockedfile_file_open: Error, unable to malloc lockedfile_data struct\n");
84+
free (shfileHandle);
85+
free (sh);
8386
return OMPI_ERR_OUT_OF_RESOURCE;
8487
}
8588

@@ -104,6 +107,8 @@ int mca_sharedfp_lockedfile_file_open (struct ompi_communicator_t *comm,
104107
handle = open ( lockedfilename, O_RDWR, 0644 );
105108
if ( -1 == handle ) {
106109
printf("[%d]mca_sharedfp_lockedfile_file_open: Error during file open\n", rank);
110+
free (shfileHandle);
111+
free (sh);
107112
free(module_data);
108113
return OMPI_ERROR;
109114
}

ompi/mca/sharedfp/sm/sharedfp_sm_file_open.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ int mca_sharedfp_sm_file_open (struct ompi_communicator_t *comm,
6464
/*Open the same file again without shared file pointer*/
6565
/*----------------------------------------------------*/
6666
shfileHandle = (mca_io_ompio_file_t *)malloc(sizeof(mca_io_ompio_file_t));
67+
if ( NULL == shfileHandle ) {
68+
opal_output(0, "mca_sharedfp_sm_file_open: Error during memory allocation\n");
69+
return OMPI_ERR_OUT_OF_RESOURCE;
70+
}
6771
err = ompio_io_ompio_file_open(comm,filename,amode,info,shfileHandle,false);
6872
if ( OMPI_SUCCESS != err) {
6973
opal_output(0, "mca_sharedfp_sm_file_open: Error during file open\n");
74+
free (shfileHandle);
7075
return err;
7176
}
7277
shfileHandle->f_fh = fh->f_fh;

0 commit comments

Comments
 (0)