Skip to content

Commit

Permalink
i.evapo.time: Fix loop indexing
Browse files Browse the repository at this point in the history
to respect array bounds: avoid call to array index, which is past the
end of the array. (#1248)

Addresses -Warray-bounds compiler warnings.
  • Loading branch information
nilason committed Feb 2, 2021
1 parent 8aab74c commit 313581c
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions imagery/i.evapo.time/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int main(int argc, char *argv[])
module->description =_("Computes temporal integration of satellite "
"ET actual (ETa) following the daily ET reference "
"(ETo) from meteorological station(s).");

/* Define the different options */
input = G_define_standard_option(G_OPT_R_INPUTS);
input->key = "eta";
Expand All @@ -100,7 +100,7 @@ int main(int argc, char *argv[])
input3->type = TYPE_DOUBLE;
input3->required = YES;
input3->description = _("Value of DOY for ETo first day");

input4 = G_define_option();
input4->key = "start_period";
input4->type = TYPE_DOUBLE;
Expand All @@ -114,11 +114,11 @@ int main(int argc, char *argv[])
input5->description = _("Value of DOY for the last day of the period studied");

output = G_define_standard_option(G_OPT_R_OUTPUT);

/* init nfiles */
nfiles = 1;
nfiles1 = 1;
nfiles2 = 1;
nfiles = 0;
nfiles1 = 0;
nfiles2 = 0;

/********************/

Expand Down Expand Up @@ -157,13 +157,13 @@ int main(int argc, char *argv[])
inrast[nfiles] = Rast_allocate_d_buf();
nfiles++;
}
nfiles--;
if (nfiles <= 1)

if (nfiles < 2)
G_fatal_error(_("The min specified input map is two"));

/****************************************/
for (; *ptr1 != NULL; ptr1++) {
if (nfiles1 > MAXFILES)
if (nfiles1 == MAXFILES)
G_fatal_error(_("Too many ETa_doy files. Only %d allowed."),
MAXFILES);
name1 = *ptr1;
Expand All @@ -173,8 +173,8 @@ int main(int argc, char *argv[])
inrast1[nfiles1] = Rast_allocate_d_buf();
nfiles1++;
}
nfiles1--;
if (nfiles1 <= 1)

if (nfiles1 < 2)
G_fatal_error(_("The min specified input map is two"));


Expand All @@ -185,7 +185,7 @@ int main(int argc, char *argv[])
/****************************************/

for (; *ptr2 != NULL; ptr2++) {
if (nfiles > MAXFILES)
if (nfiles2 == MAXFILES)
G_fatal_error(_("Too many ETo files. Only %d allowed."),
MAXFILES);
name2 = *ptr2;
Expand All @@ -195,8 +195,8 @@ int main(int argc, char *argv[])
inrast2[nfiles2] = Rast_allocate_d_buf();
nfiles2++;
}
nfiles2--;
if (nfiles2 <= 1)

if (nfiles2 < 2)
G_fatal_error(_("The min specified input map is two"));

/* Allocate output buffer, use input map data_type */
Expand All @@ -222,54 +222,54 @@ int main(int argc, char *argv[])
G_percent(row, nrows, 2);

/* read input map */
for (i = 1; i <= nfiles; i++)
for (i = 0; i < nfiles; i++)
Rast_get_d_row(infd[i], inrast[i], row);
for (i = 1; i <= nfiles1; i++)

for (i = 0; i < nfiles1; i++)
Rast_get_d_row(infd1[i], inrast1[i], row);

for (i = 1; i <= nfiles2; i++)
for (i = 0; i < nfiles2; i++)
Rast_get_d_row (infd2[i], inrast2[i], row);

/*process the data */
for (col = 0; col < ncols; col++)
{
int d1_null=0;
int d_null=0;
for (i = 1; i <= nfiles; i++)
for (i = 0; i < nfiles; i++)
{
if (Rast_is_d_null_value(&((DCELL *) inrast[i])[col]))
d_null=1;
else
d[i] = ((DCELL *) inrast[i])[col];
}
for (i = 1; i <= nfiles1; i++)
for (i = 0; i < nfiles1; i++)
{
if (Rast_is_d_null_value(&((DCELL *) inrast1[i])[col]))
d1_null=1;
else
d1[i] = ((DCELL *) inrast1[i])[col];
}

for (i = 1; i <= nfiles2; i++)
for (i = 0; i < nfiles2; i++)
d2[i] = ((DCELL *) inrast2[i])[col];

/* Find out the DOY of the eto image */
for (i = 1; i <= nfiles1; i++)
for (i = 0; i < nfiles1; i++)
{
if ( d_null==1 || d1_null==1 )
Rast_set_d_null_value(&outrast[col],1);
Rast_set_d_null_value(&outrast[col],1);
else
{
doy[i] = d1[i] - etodoy+1;
if (Rast_is_d_null_value(&d2[(int)doy[i]]) || d2[(int)doy[i]]==0 )
Rast_set_d_null_value(&outrast[col],1);
else
d_ETrF[i] = d[i] / d2[(int)doy[i]];
}
}
}

for (i = 1; i <= nfiles1; i++)
for (i = 0; i < nfiles1; i++)
{
/* do nothing */
if ( d_null==1 || d1_null==1)
Expand All @@ -279,25 +279,25 @@ int main(int argc, char *argv[])
else
{
DOYbeforeETa[i]=0; DOYafterETa[i]=0;
if (i == 1)
if (i == 0)
DOYbeforeETa[i] = startperiod;
else
{
int k=i-1;
while (d1[k]>=startperiod )
{
if (d1[k]<0) /* case were d1[k] is null */
k=k-1;
k=k-1;
else
{
DOYbeforeETa[i] = 1+((d1[i] + d1[k])/2.0);
break;
}
}
}

}
if (i == nfiles1)

if (i == (nfiles1-1))
DOYafterETa[i] = endperiod;
else
{
Expand All @@ -310,22 +310,22 @@ int main(int argc, char *argv[])
{
DOYafterETa[i] = (d1[i] + d1[k]) / 2.0;
break;
}
}
}
}
}
}

sum[MAXFILES] = 0.0;
for (i = 1; i <= nfiles1; i++)
/* sum[MAXFILES] = 0.0; */
for (i = 0; i < nfiles1; i++)
{
if(d_null==1 || d1_null==1)
{
/* do nothing */
}
}
else
{
if (DOYbeforeETa[i]==0 || DOYbeforeETa[i]==0 )
if (DOYbeforeETa[i]==0 || DOYbeforeETa[i]==0 )
Rast_set_d_null_value(&outrast[col],1);
else
{
Expand All @@ -337,31 +337,31 @@ int main(int argc, char *argv[])
}
}
}

d_out = 0.0;
for (i = 1; i <= nfiles1; i++)
for (i = 0; i < nfiles1; i++)
{
if(d_null==1 || d_null==1)
Rast_set_d_null_value(&outrast[col],1);
else
{
{
d_out += d_ETrF[i] * sum[i];
outrast[col] = d_out;
}
}
}
}
Rast_put_row(outfd, outrast, out_data_type);
}

for (i = 1; i <= nfiles; i++) {
for (i = 0; i < nfiles; i++) {
G_free(inrast[i]);
Rast_close(infd[i]);
}
for (i = 1; i <= nfiles1; i++) {
for (i = 0; i < nfiles1; i++) {
G_free(inrast1[i]);
Rast_close(infd1[i]);
}
for (i = 1; i <= nfiles2; i++) {
for (i = 0; i < nfiles2; i++) {
G_free(inrast2[i]);
Rast_close(infd2[i]);
}
Expand Down

0 comments on commit 313581c

Please sign in to comment.