Skip to content

Commit 71d952f

Browse files
authored
i.smap: fix possible pole error with log in extract function (OSGeo#4499)
Using logarithm function call with zero argument will lead to a pole error, which occurs if the mathematical function has an exact infinite result. Refactor the conditional to only execute the code when number of subclasses are more than 1, which would preemptively stop us from getting into situation where log would have a zero argument. In cases where number of subclasses are <= 0, set the default value to 0. This is necessary as `ll` points to malloc'd memory and can contain random value. Signed-off-by: Mohan Yelugoti <[email protected]>
1 parent 620c822 commit 71d952f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

imagery/i.smap/model.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void extract(DCELL ***img, /* multispectral image, img[band][i][j] */
141141
ll[i][j][m] = subll[0];
142142
}
143143
/* compute mixture likelihood */
144-
else {
144+
else if (C->nsubclasses > 1) {
145145
/* find the most likely subclass */
146146
for (k = 0; k < C->nsubclasses; k++) {
147147
if (k == 0)
@@ -157,6 +157,9 @@ void extract(DCELL ***img, /* multispectral image, img[band][i][j] */
157157

158158
ll[i][j][m] = log(subsum) + maxlike;
159159
}
160+
else {
161+
ll[i][j][m] = 0.0;
162+
}
160163
}
161164
}
162165
}

0 commit comments

Comments
 (0)