From 3c4bb7747d1a16d6b9d2d34992bad250069632a7 Mon Sep 17 00:00:00 2001
From: satok <satok@google.com>
Date: Fri, 4 Mar 2011 22:50:19 -0800
Subject: [PATCH] A bug fix for the mistyped space algorithm

Bug: 3311719

-- also fixed compiler warnings

Change-Id: I6941c0d02f10d67af88bc943748dde8d8783fabb
---
 native/src/defines.h              | 2 +-
 native/src/proximity_info.cpp     | 2 +-
 native/src/unigram_dictionary.cpp | 8 +++-----
 native/src/unigram_dictionary.h   | 4 ++--
 4 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/native/src/defines.h b/native/src/defines.h
index 16927e5bb8..a81aa49983 100644
--- a/native/src/defines.h
+++ b/native/src/defines.h
@@ -103,7 +103,7 @@ static void prof_out(void) {
 #define U_SHORT_MAX 1 << 16
 #endif
 #ifndef S_INT_MAX
-#define S_INT_MAX ((1 << 31) - 1)
+#define S_INT_MAX 2147483647 // ((1 << 31) - 1)
 #endif
 
 // Define this to use mmap() for dictionary loading.  Undefine to use malloc() instead of mmap().
diff --git a/native/src/proximity_info.cpp b/native/src/proximity_info.cpp
index 5f2d09f5cc..102123c3ce 100644
--- a/native/src/proximity_info.cpp
+++ b/native/src/proximity_info.cpp
@@ -42,7 +42,7 @@ ProximityInfo::~ProximityInfo() {
 }
 
 inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
-    return (y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH)
+    return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
             * MAX_PROXIMITY_CHARS_SIZE;
 }
 
diff --git a/native/src/unigram_dictionary.cpp b/native/src/unigram_dictionary.cpp
index 3487d4f118..c2cd76084f 100644
--- a/native/src/unigram_dictionary.cpp
+++ b/native/src/unigram_dictionary.cpp
@@ -81,7 +81,7 @@ bool UnigramDictionary::isDigraph(const int* codes, const int i, const int codes
 void UnigramDictionary::getWordWithDigraphSuggestionsRec(const ProximityInfo *proximityInfo,
         const int *xcoordinates, const int* ycoordinates, const int *codesBuffer,
         const int codesBufferSize, const int flags, const int* codesSrc, const int codesRemain,
-        int currentDepth, int* codesDest, unsigned short* outWords, int* frequencies) {
+        const int currentDepth, int* codesDest, unsigned short* outWords, int* frequencies) {
 
     if (currentDepth < MAX_UMLAUT_SEARCH_DEPTH) {
         for (int i = 0; i < codesRemain; ++i) {
@@ -232,11 +232,9 @@ void UnigramDictionary::getWordSuggestions(const ProximityInfo *proximityInfo,
             if (DEBUG_PROXIMITY_INFO)
                 LOGI("Input[%d] x = %d, y = %d, has space proximity = %d",
                         i, x, y, proximityInfo->hasSpaceProximity(x, y));
-
             if (proximityInfo->hasSpaceProximity(x, y)) {
                 getMistypedSpaceWords(mInputLength, i);
             }
-
         }
     }
     PROF_END(6);
@@ -405,7 +403,7 @@ bool UnigramDictionary::getSplitTwoWordsSuggestion(const int inputLength,
         const int secondWordLength) {
     if (inputLength >= MAX_WORD_LENGTH) return false;
     if (0 >= firstWordLength || 0 >= secondWordLength || firstWordStartPos >= secondWordStartPos
-            || firstWordStartPos < 0 || secondWordStartPos >= inputLength)
+            || firstWordStartPos < 0 || secondWordStartPos + secondWordLength > inputLength)
         return false;
     const int newWordLength = firstWordLength + secondWordLength + 1;
     // Allocating variable length array on stack
@@ -487,7 +485,7 @@ void UnigramDictionary::getWordsRec(const int childrenCount, const int pos, cons
     }
 }
 
-static const int TWO_31ST_DIV_255 = ((1 << 31) - 1) / 255;
+static const int TWO_31ST_DIV_255 = S_INT_MAX / 255;
 static inline int capped255MultForFullMatchAccentsOrCapitalizationDifference(const int num) {
     return (num < TWO_31ST_DIV_255 ? 255 * num : S_INT_MAX);
 }
diff --git a/native/src/unigram_dictionary.h b/native/src/unigram_dictionary.h
index ef820cba5d..3d3007ce09 100644
--- a/native/src/unigram_dictionary.h
+++ b/native/src/unigram_dictionary.h
@@ -46,7 +46,7 @@ private:
     void getWordWithDigraphSuggestionsRec(const ProximityInfo *proximityInfo,
         const int *xcoordinates, const int* ycoordinates, const int *codesBuffer,
         const int codesBufferSize, const int flags, const int* codesSrc, const int codesRemain,
-        int currentDepth, int* codesDest, unsigned short* outWords, int* frequencies);
+        const int currentDepth, int* codesDest, unsigned short* outWords, int* frequencies);
     void initSuggestions(const int *codes, const int codesSize, unsigned short *outWords,
             int *frequencies);
     void getSuggestionCandidates(const int skipPos, const int excessivePos,
@@ -113,7 +113,7 @@ private:
     const int FULL_WORD_MULTIPLIER;
     const int ROOT_POS;
     const unsigned int BYTES_IN_ONE_CHAR;
-    const unsigned int MAX_UMLAUT_SEARCH_DEPTH;
+    const int MAX_UMLAUT_SEARCH_DEPTH;
 
     // Flags for special processing
     // Those *must* match the flags in BinaryDictionary.Flags.ALL_FLAGS in BinaryDictionary.java
-- 
GitLab