Merge pull request #591 from mcneish1/valgrind-issues-dev

Fix use-after-free in Smatrix, test_pda
This commit is contained in:
Lew Rossman
2020-04-14 13:06:47 -04:00
committed by GitHub
5 changed files with 26 additions and 23 deletions

View File

@@ -1777,6 +1777,8 @@ int DLLEXPORT EN_addnode(EN_Project p, char *id, int nodeType, int *index)
hyd->NodeDemand = (double *)realloc(hyd->NodeDemand, size);
qual->NodeQual = (double *)realloc(qual->NodeQual, size);
hyd->NodeHead = (double *)realloc(hyd->NodeHead, size);
hyd->DemandFlow = (double *)realloc(hyd->DemandFlow, size);
hyd->EmitterFlow = (double *)realloc(hyd->EmitterFlow, size);
// Actions taken when a new Junction is added
if (nodeType == EN_JUNCTION)

View File

@@ -305,16 +305,12 @@ int allocmatrix(Project *pr)
hyd->P = (double *) calloc(net->Nlinks+1,sizeof(double));
hyd->Y = (double *) calloc(net->Nlinks+1,sizeof(double));
hyd->DemandFlow = (double *) calloc(net->Nnodes + 1, sizeof(double));
hyd->EmitterFlow = (double *) calloc(net->Nnodes+1, sizeof(double));
hyd->Xflow = (double *) calloc(MAX((net->Nnodes+1), (net->Nlinks+1)),
sizeof(double));
hyd->OldStatus = (StatusType *) calloc(net->Nlinks+net->Ntanks+1,
sizeof(StatusType));
ERRCODE(MEMCHECK(hyd->P));
ERRCODE(MEMCHECK(hyd->Y));
ERRCODE(MEMCHECK(hyd->DemandFlow));
ERRCODE(MEMCHECK(hyd->EmitterFlow));
ERRCODE(MEMCHECK(hyd->Xflow));
ERRCODE(MEMCHECK(hyd->OldStatus));
return errcode;
@@ -334,8 +330,6 @@ void freematrix(Project *pr)
free(hyd->P);
free(hyd->Y);
free(hyd->DemandFlow);
free(hyd->EmitterFlow);
free(hyd->Xflow);
free(hyd->OldStatus);
}

View File

@@ -253,6 +253,8 @@ void initpointers(Project *pr)
pr->hydraul.P = NULL;
pr->hydraul.Y = NULL;
pr->hydraul.Xflow = NULL;
pr->hydraul.DemandFlow = NULL;
pr->hydraul.EmitterFlow = NULL;
pr->quality.NodeQual = NULL;
pr->quality.PipeRateCoeff = NULL;
@@ -313,10 +315,14 @@ int allocdata(Project *pr)
pr->hydraul.NodeDemand = (double *)calloc(n, sizeof(double));
pr->hydraul.NodeHead = (double *)calloc(n, sizeof(double));
pr->quality.NodeQual = (double *)calloc(n, sizeof(double));
pr->hydraul.DemandFlow = (double *)calloc(n, sizeof(double));
pr->hydraul.EmitterFlow = (double *)calloc(n, sizeof(double));
ERRCODE(MEMCHECK(pr->network.Node));
ERRCODE(MEMCHECK(pr->hydraul.NodeDemand));
ERRCODE(MEMCHECK(pr->hydraul.NodeHead));
ERRCODE(MEMCHECK(pr->quality.NodeQual));
ERRCODE(MEMCHECK(pr->hydraul.DemandFlow));
ERRCODE(MEMCHECK(pr->hydraul.EmitterFlow));
}
// Allocate memory for network links
@@ -388,6 +394,8 @@ void freedata(Project *pr)
free(pr->hydraul.LinkFlow);
free(pr->hydraul.LinkSetting);
free(pr->hydraul.LinkStatus);
free(pr->hydraul.DemandFlow);
free(pr->hydraul.EmitterFlow);
free(pr->quality.NodeQual);
// Free memory used for nodal adjacency lists

View File

@@ -48,8 +48,8 @@ static int paralink(Network *, Smatrix *, int, int, int k);
static void xparalinks(Network *);
static int reordernodes(Project *);
static int factorize(Project *);
static int growlist(Project *, int);
static int newlink(Project *, Padjlist);
static int growlist(Project *, int, int *);
static int newlink(Project *, Padjlist, int *);
static int linked(Network *, int, int);
static int addlink(Network *, int, int, int);
static int storesparse(Project *, int);
@@ -443,8 +443,8 @@ int factorize(Project *pr)
Padjlist alink;
// Find degree of each junction node
sm->Degree = (int *)calloc(net->Nnodes + 1, sizeof(int));
if (sm->Degree == NULL) return 101;
int *degree = (int *)calloc(net->Nnodes + 1, sizeof(int));
if (degree == NULL) return 101;
// NOTE: For purposes of node re-ordering, Tanks (nodes with
// indexes above Njuncs) have zero degree of adjacency.
@@ -453,7 +453,7 @@ int factorize(Project *pr)
{
for (alink = net->Adjlist[k]; alink != NULL; alink = alink->next)
{
if (alink->node > 0) sm->Degree[k]++;
if (alink->node > 0) degree[k]++;
}
}
@@ -463,19 +463,19 @@ int factorize(Project *pr)
for (k = 1; k <= net->Njuncs; k++) // Examine each junction
{
knode = sm->Order[k]; // Re-ordered index
if (!growlist(pr, knode)) // Augment adjacency list
if (!growlist(pr, knode, degree)) // Augment adjacency list
{
errcode = 101;
break;
}
sm->Degree[knode] = 0; // In-activate node
degree[knode] = 0; // In-activate node
}
free(sm->Degree);
free(degree);
return errcode;
}
int growlist(Project *pr, int knode)
int growlist(Project *pr, int knode, int *degree)
/*
**--------------------------------------------------------------
** Input: knode = node index
@@ -496,10 +496,10 @@ int growlist(Project *pr, int knode)
for (alink = net->Adjlist[knode]; alink != NULL; alink = alink -> next)
{
node = alink->node; // End node of connecting link
if (node > 0 && sm->Degree[node] > 0) // End node is active
if (node > 0 && degree[node] > 0) // End node is active
{
sm->Degree[node]--; // Reduce degree of adjacency
if (!newlink(pr, alink)) // Add to adjacency list
degree[node]--; // Reduce degree of adjacency
if (!newlink(pr, alink, degree)) // Add to adjacency list
{
return 0;
}
@@ -509,7 +509,7 @@ int growlist(Project *pr, int knode)
}
int newlink(Project *pr, Padjlist alink)
int newlink(Project *pr, Padjlist alink, int *degree)
/*
**--------------------------------------------------------------
** Input: alink = element of node's adjacency list
@@ -533,7 +533,7 @@ int newlink(Project *pr, Padjlist alink)
// If jnode still active, and inode not connected to jnode,
// then add a new connection between inode and jnode.
if (jnode > 0 && sm->Degree[jnode] > 0) // jnode still active
if (jnode > 0 && degree[jnode] > 0) // jnode still active
{
if (!linked(net, inode, jnode)) // inode not linked to jnode
{
@@ -545,8 +545,8 @@ int newlink(Project *pr, Padjlist alink)
// reflect the new connection.
if (!addlink(net, inode, jnode, sm->Ncoeffs)) return 0;
if (!addlink(net, jnode, inode, sm->Ncoeffs)) return 0;
sm->Degree[inode]++;
sm->Degree[jnode]++;
degree[inode]++;
degree[jnode]++;
}
}
}

View File

@@ -688,7 +688,6 @@ typedef struct {
*XLNZ, // Start position of each column in NZSUB
*NZSUB, // Row index of each coeff. in each column
*LNZ, // Position of each coeff. in Aij array
*Degree, // Number of links adjacent to each node
*link, // Array used by linear eqn. solver
*first; // Array used by linear eqn. solver