Skip to content

Commit d9c472b

Browse files
committed
cluster: Fix some potential overflows
This fixes several issues reported by LGTM: Multiplication result may overflow 'int' before it is converted to 'size_type'. Multiplication result may overflow 'float' before it is converted to 'double'. Multiplication result may overflow 'int' before it is converted to 'unsigned long'. Signed-off-by: Stefan Weil <[email protected]>
1 parent d1d73b9 commit d9c472b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/classify/cluster.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,8 @@ PROTOTYPE *TestEllipticalProto(CLUSTERER *Clusterer,
10861086
int TotalDims = Left->SampleCount + Right->SampleCount;
10871087
if (TotalDims < N + 1 || TotalDims < 2)
10881088
return nullptr;
1089-
std::vector<float> Covariance(N * N);
1090-
std::vector<float> Inverse(N * N);
1089+
std::vector<float> Covariance(static_cast<size_t>(N) * N);
1090+
std::vector<float> Inverse(static_cast<size_t>(N) * N);
10911091
std::vector<float> Delta(N);
10921092
// Compute a new covariance matrix that only uses essential features.
10931093
for (int i = 0; i < N; ++i) {
@@ -1126,7 +1126,7 @@ PROTOTYPE *TestEllipticalProto(CLUSTERER *Clusterer,
11261126
for (int x = 0; x < N; ++x) {
11271127
double temp = 0.0;
11281128
for (int y = 0; y < N; ++y) {
1129-
temp += Inverse[y + N*x] * Delta[y];
1129+
temp += static_cast<double>(Inverse[y + N * x]) * Delta[y];
11301130
}
11311131
Tsq += Delta[x] * temp;
11321132
}
@@ -1368,7 +1368,7 @@ ComputeStatistics (int16_t N, PARAM_DESC ParamDesc[], CLUSTER * Cluster) {
13681368

13691369
// allocate memory to hold the statistics results
13701370
Statistics = (STATISTICS *) Emalloc (sizeof (STATISTICS));
1371-
Statistics->CoVariance = (float *) Emalloc (N * N * sizeof (float));
1371+
Statistics->CoVariance = (float *)Emalloc(sizeof(float) * N * N);
13721372
Statistics->Min = (float *) Emalloc (N * sizeof (float));
13731373
Statistics->Max = (float *) Emalloc (N * sizeof (float));
13741374

@@ -2483,7 +2483,7 @@ double InvertMatrix(const float* input, int size, float* inv) {
24832483
for (col = 0; col < size; col++) {
24842484
double sum = 0.0;
24852485
for (int k = 0; k < size; ++k) {
2486-
sum += input[row*size + k] * inv[k *size + col];
2486+
sum += static_cast<double>(input[row * size + k]) * inv[k * size + col];
24872487
}
24882488
if (row != col) {
24892489
error_sum += Abs(sum);

0 commit comments

Comments
 (0)