Skip to content

Commit

Permalink
Merge pull request ESMCI#1377 from NCAR/ejh_fix_codacy
Browse files Browse the repository at this point in the history
Fix memory leaks on error in test_rearr.c
  • Loading branch information
edhartnett authored Mar 27, 2019
2 parents 0725be7 + a59ab5e commit b0b28e8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/clib/pioc_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,7 @@ int check_unlim_use(int ncid)
int inq_file_metadata(file_desc_t *file, int ncid, int iotype, int *nvars, int **rec_var,
int **pio_type, int **pio_type_size, MPI_Datatype **mpi_type, int **mpi_type_size)
{
int nunlimdims; /* The number of unlimited dimensions. */
int nunlimdims = 0; /* The number of unlimited dimensions. */
int unlimdimid;
int *unlimdimids;
int mpierr;
Expand Down
73 changes: 43 additions & 30 deletions tests/cunit/test_rearr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,11 +1085,11 @@ int test_rearrange_comp2io(MPI_Comm test_comm, int my_rank)
/* Test function rearrange_io2comp. */
int test_rearrange_io2comp(MPI_Comm test_comm, int my_rank)
{
iosystem_desc_t *ios;
io_desc_t *iodesc;
iosystem_desc_t *ios = NULL;
io_desc_t *iodesc = NULL;
void *sbuf = NULL;
void *rbuf = NULL;
io_region *ior1;
io_region *ior1 = NULL;
int maplen = 2;
PIO_Offset compmap[2] = {1, 0};
const int gdimlen[NDIM1] = {8};
Expand All @@ -1099,17 +1099,17 @@ int test_rearrange_io2comp(MPI_Comm test_comm, int my_rank)

/* Allocate some space for data. */
if (!(sbuf = calloc(4, sizeof(int))))
return PIO_ENOMEM;
BAIL(PIO_ENOMEM);
if (!(rbuf = calloc(4, sizeof(int))))
return PIO_ENOMEM;
BAIL(PIO_ENOMEM);

/* Allocate IO system info struct for this test. */
if (!(ios = calloc(1, sizeof(iosystem_desc_t))))
return PIO_ENOMEM;
BAIL(PIO_ENOMEM);

/* Allocate IO desc struct for this test. */
if (!(iodesc = calloc(1, sizeof(io_desc_t))))
return PIO_ENOMEM;
BAIL(PIO_ENOMEM);

ios->ioproc = 1;
ios->io_rank = my_rank;
Expand Down Expand Up @@ -1149,17 +1149,17 @@ int test_rearrange_io2comp(MPI_Comm test_comm, int my_rank)
ios->num_comptasks = 4;
ios->num_uniontasks = 4;
if (!(ios->ioranks = calloc(ios->num_iotasks, sizeof(int))))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
BAIL(PIO_ENOMEM);
for (int i = 0; i < TARGET_NTASKS; i++)
ios->ioranks[i] = i;
if (!(ios->compranks = calloc(ios->num_comptasks, sizeof(int))))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
BAIL(PIO_ENOMEM);
for (int i = 0; i < TARGET_NTASKS; i++)
ios->compranks[i] = i;

/* This is how we allocate a region. */
if ((ret = alloc_region2(NULL, NDIM1, &ior1)))
return ret;
BAIL(ret);
ior1->next = NULL;
if (my_rank == 0)
ior1->count[0] = 8;
Expand All @@ -1172,42 +1172,55 @@ int test_rearrange_io2comp(MPI_Comm test_comm, int my_rank)

/* Run the function to test. */
if ((ret = rearrange_io2comp(ios, iodesc, sbuf, rbuf)))
return ret;
BAIL(ret);

/* We created send types, so free them. */
for (int st = 0; st < num_send_types; st++)
if (iodesc->stype[st] != PIO_DATATYPE_NULL)
if ((mpierr = MPI_Type_free(&iodesc->stype[st])))
MPIERR(mpierr);
MPIBAIL(mpierr);

/* We created one receive type, so free it. */
if (iodesc->rtype)
for (int r = 0; r < iodesc->nrecvs; r++)
if (iodesc->rtype[r] != PIO_DATATYPE_NULL)
if ((mpierr = MPI_Type_free(&iodesc->rtype[r])))
MPIERR(mpierr);
MPIBAIL(mpierr);

exit:
/* Free resources allocated in library code. */
free(iodesc->rtype);
free(iodesc->sindex);
free(iodesc->scount);
free(iodesc->stype);
free(iodesc->rcount);
free(iodesc->rfrom);
free(iodesc->rindex);
if (iodesc)
{
free(iodesc->rtype);
free(iodesc->sindex);
free(iodesc->scount);
free(iodesc->stype);
free(iodesc->rcount);
free(iodesc->rfrom);
free(iodesc->rindex);
}

/* Free resources from test. */
free(ior1->start);
free(ior1->count);
free(ior1);
free(ios->ioranks);
free(ios->compranks);
free(iodesc);
free(ios);
free(sbuf);
free(rbuf);
if (ior1->start)
free(ior1->start);
if (ior1->count)
free(ior1->count);
if (ior1)
free(ior1);
if (ios->ioranks)
free(ios->ioranks);
if (ios->compranks)
free(ios->compranks);
if (iodesc)
free(iodesc);
if (ios)
free(ios);
if (sbuf)
free(sbuf);
if (rbuf)
free(rbuf);

return 0;
return ret;
}

/* These tests do not need an iosysid. */
Expand Down

0 comments on commit b0b28e8

Please sign in to comment.