Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i.pca: Fix Resource Leak issue #4664

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 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 cleanup;
}

for (i = 0; i < bands; i++) {
if (stddev) {
Expand All @@ -378,22 +381,25 @@ 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;

cleanup:
for (i = 0; i < bands; i++) {
if (sum2[i])
nilason marked this conversation as resolved.
Show resolved Hide resolved
G_free(sum2[i]);
if (rowbuf[i])
G_free(rowbuf[i]);
}
G_free(rowbuf);

G_free(sum2);
if (sd)
G_free(sd);
if (sumsq)
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 +577,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
Loading