Merge "Revert "Remove Azerbaijani subtype""
diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
index 7757d29..cb5b0e3 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
@@ -58,6 +58,7 @@
     private static final boolean DBG_STRESS_TEST = false;
 
     private static final int TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS = 100;
+    private static final int TIMEOUT_FOR_READ_OPS_FOR_TESTS_IN_MILLISECONDS = 1000;
 
     /**
      * The maximum length of a word in this dictionary.
@@ -761,7 +762,7 @@
                 }
             }
         });
-        return holder.get(false, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS);
+        return holder.get(false, TIMEOUT_FOR_READ_OPS_FOR_TESTS_IN_MILLISECONDS);
     }
 
     @UsedForTesting
diff --git a/native/jni/src/suggest/core/dictionary/bigram_dictionary.cpp b/native/jni/src/suggest/core/dictionary/bigram_dictionary.cpp
index 2a62b55..d0b96b0 100644
--- a/native/jni/src/suggest/core/dictionary/bigram_dictionary.cpp
+++ b/native/jni/src/suggest/core/dictionary/bigram_dictionary.cpp
@@ -41,6 +41,9 @@
 
 void BigramDictionary::addWordBigram(int *word, int length, int probability, int *bigramProbability,
         int *bigramCodePoints, int *outputTypes) const {
+    if (length >= MAX_WORD_LENGTH) {
+        length = MAX_WORD_LENGTH - 1;
+    }
     word[length] = 0;
     if (DEBUG_DICT_FULL) {
 #ifdef FLAG_DBG
@@ -66,14 +69,17 @@
     if (insertAt >= MAX_RESULTS) {
         return;
     }
-    memmove(bigramProbability + (insertAt + 1),
-            bigramProbability + insertAt,
+    // Shift result buffers to insert the new entry.
+    memmove(bigramProbability + (insertAt + 1), bigramProbability + insertAt,
             (MAX_RESULTS - insertAt - 1) * sizeof(bigramProbability[0]));
-    bigramProbability[insertAt] = probability;
-    outputTypes[insertAt] = Dictionary::KIND_PREDICTION;
+    memmove(outputTypes + (insertAt + 1), outputTypes + insertAt,
+            (MAX_RESULTS - insertAt - 1) * sizeof(outputTypes[0]));
     memmove(bigramCodePoints + (insertAt + 1) * MAX_WORD_LENGTH,
             bigramCodePoints + insertAt * MAX_WORD_LENGTH,
             (MAX_RESULTS - insertAt - 1) * sizeof(bigramCodePoints[0]) * MAX_WORD_LENGTH);
+    // Put the result.
+    bigramProbability[insertAt] = probability;
+    outputTypes[insertAt] = Dictionary::KIND_PREDICTION;
     int *dest = bigramCodePoints + insertAt * MAX_WORD_LENGTH;
     while (length--) {
         *dest++ = *word++;
diff --git a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
index 17423a7..8433569 100644
--- a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
+++ b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
@@ -105,8 +105,10 @@
         final UserHistoryDictionary dict =
                 PersonalizationHelper.getUserHistoryDictionary(getContext(),
                         new Locale(testFilenameSuffix));
+        dict.waitAllTasksForTests();
         dict.clearAndFlushDictionary();
         dict.close();
+        dict.waitAllTasksForTests();
     }
 
     /**