From 44861474fbea784f12fe86bc56d30d5d9be4ad81 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 19 Jul 2011 18:16:34 +0900 Subject: [PATCH] Add a number of NULL pointer guards. None of these are expected to actually be null, but those are included for peace of mind and foolproofing against future code changes. Bug: 4580040 Change-Id: Ib112b3e5db5f177aaf61767164b7e78d711f90a0 --- .../latin/BinaryDictionaryGetter.java | 4 +++- .../latin/DictionaryCollection.java | 6 +++++- .../inputmethod/latin/DictionaryFactory.java | 20 ++++++++++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 7ce92920d..bce787db2 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -91,7 +91,9 @@ class BinaryDictionaryGetter { Log.e(TAG, "Unable to read source data for locale " + locale.toString() + ": falling back to internal dictionary"); } - return Arrays.asList(loadFallbackResource(context, fallbackResId)); + final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId); + if (null == fallbackAsset) return null; + return Arrays.asList(fallbackAsset); } } } diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java index 5e7de3e6b..e987d9f0c 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java +++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java @@ -32,7 +32,11 @@ public class DictionaryCollection extends Dictionary { } public DictionaryCollection(Dictionary... dictionaries) { - mDictionaries = new CopyOnWriteArrayList(dictionaries); + if (null == dictionaries) { + mDictionaries = new CopyOnWriteArrayList(); + } else { + mDictionaries = new CopyOnWriteArrayList(dictionaries); + } } public DictionaryCollection(Collection dictionaries) { diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java index bba331868..a35b0f5b0 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java @@ -52,13 +52,23 @@ public class DictionaryFactory { } final List dictList = new LinkedList(); - for (final AssetFileAddress f : BinaryDictionaryGetter.getDictionaryFiles(locale, - context, fallbackResId)) { - dictList.add(new BinaryDictionary(context, f.mFilename, f.mOffset, f.mLength, null)); + final List assetFileList = + BinaryDictionaryGetter.getDictionaryFiles(locale, context, fallbackResId); + if (null != assetFileList) { + for (final AssetFileAddress f : assetFileList) { + dictList.add( + new BinaryDictionary(context, f.mFilename, f.mOffset, f.mLength, null)); + } } - if (null == dictList) return null; - return new DictionaryCollection(dictList); + // null == dictList is not supposed to be possible, but better safe than sorry and it's + // safer for future extension. In this case, rather than returning null, it should be safer + // to return an empty DictionaryCollection. + if (null == dictList) { + return new DictionaryCollection(); + } else { + return new DictionaryCollection(dictList); + } } /**