Fix VocabCompare to prevent integer overflow during vocabulary sorting
Change-Id: I3796e5aef14b3de1200907e135952258a004d049
diff --git a/src/dereko2vec.c b/src/dereko2vec.c
index 8c34179..5b8ef43 100644
--- a/src/dereko2vec.c
+++ b/src/dereko2vec.c
@@ -220,7 +220,11 @@
// Used later for sorting by word counts
int VocabCompare(const void *a, const void *b) {
- return ((struct vocab_word *) b)->cn - ((struct vocab_word *) a)->cn;
+ long long freq1 = ((struct vocab_word *) a)->cn;
+ long long freq2 = ((struct vocab_word *) b)->cn;
+ if (freq1 < freq2) return 1;
+ else if (freq1 > freq2) return -1;
+ else return 0;
}
// Sorts the vocabulary by frequency using word counts