Skip to content

Commit

Permalink
i.pca: Fix Resource Leak issues (#4664)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamDesai authored Dec 2, 2024
1 parent 6bf8287 commit 43b9d8d
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions imagery/i.pca/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ static int calc_mu_cov(int *fds, double **covar, double *mu, double *stddev,
DCELL **rowbuf = (DCELL **)G_malloc(bands * sizeof(DCELL *));
double **sum2 = (double **)G_calloc(bands, sizeof(double *));
double *sumsq, *sd, *sum;
int ret = 1;

if (stddev) {
sumsq = (double *)G_calloc(bands, sizeof(double));
Expand Down Expand Up @@ -358,8 +359,10 @@ static int calc_mu_cov(int *fds, double **covar, double *mu, double *stddev,
}
G_percent(1, 1, 1);

if (count < 2)
return 0;
if (count < 2) {
ret = 0;
goto free_exit;
}

for (i = 0; i < bands; i++) {
if (stddev) {
Expand All @@ -378,22 +381,21 @@ static int calc_mu_cov(int *fds, double **covar, double *mu, double *stddev,
if (j != i)
covar[j][i] = covar[i][j];
}

G_free(sum2[i]);
G_free(rowbuf[i]);
}
for (i = 0; i < bands; i++)
mu[i] = sum[i] / count;

free_exit:
for (i = 0; i < bands; i++) {
G_free(sum2[i]);
G_free(rowbuf[i]);
}
G_free(rowbuf);

G_free(sum2);
if (sd)
G_free(sd);
if (sumsq)
G_free(sumsq);
G_free(sd);
G_free(sumsq);

return 1;
return ret;
}

static int write_pca(double **eigmat, double *mu, double *stddev, int *inp_fd,
Expand Down Expand Up @@ -571,6 +573,8 @@ static int write_pca(double **eigmat, double *mu, double *stddev, int *inp_fd,
G_free(min);
G_free(max);
G_free(old_range);
G_free(pcs);
G_free(out_fd);

return 0;
}
Expand Down

0 comments on commit 43b9d8d

Please sign in to comment.