Skip to content

Commit

Permalink
Fix crashes caused by null field extraction
Browse files Browse the repository at this point in the history
This happened in 1.3.1 to incomingStyleName in
DebatingTimerFragment#showDialogToConfirmImport

also change "(unknown)" to "[name unknown]"
  • Loading branch information
czlee committed Oct 31, 2021
1 parent 9371823 commit 03acbfd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ void checkForExistingFile(FormatXmlFilesManager filesManager, DebateFormatFieldE
return;
}

if (versionStr == null) {
Log.e(TAG, "No version found in " + this.filename);
this.state = DownloadState.UPDATE_AVAILABLE;
return;
}

int existingVersion;
try {
existingVersion = Integer.parseInt(versionStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1876,17 +1876,21 @@ private void showDialogToConfirmImport() {
FormatXmlFilesManager filesManager = new FormatXmlFilesManager(context);
boolean exists = filesManager.exists(incomingFilename);

String incomingStyleName, existingStyleName;
String incomingStyleName = null, existingStyleName = null;

try {
incomingStyleName = nameExtractor.getFieldValue(is);
is.close();
} catch (IOException | SAXException e) {
} catch (IOException e) {
showSnackbar(Snackbar.LENGTH_LONG, R.string.importDebateFormat_snackbar_error_generic);
return;
} catch (SAXException e) {
Log.e(TAG, "showDialogToConfirmImport: error parsing incoming file");
// continue with unknown file name
}
if (incomingStyleName == null)
incomingStyleName = getString(R.string.importDebateFormat_placeholder_unknownStyleName);

existingStyleName = null;
if (exists) {
// If there's an existing file, grab its style name and prompt to replace. (We don't
// give an option not to replace.
Expand All @@ -1895,9 +1899,10 @@ private void showDialogToConfirmImport() {
existingStyleName = nameExtractor.getFieldValue(existingIs);
existingIs.close();
} catch (IOException | SAXException e) {
existingStyleName = getString(R.string.importDebateFormat_placeholder_unknownStyleName);
Log.e(TAG, "showDialogToConfirmImport: error parsing existing file");
}
if (existingStyleName == null)
existingStyleName = getString(R.string.importDebateFormat_placeholder_unknownStyleName);

} else {
// If it wasn't found, check if the style name happens to match any other file, since we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ private void importIncomingFilePrompt(@Nullable Uri uri) {
if (!filename.endsWith(".xml"))
filename = filename + ".xml";

String existingStyleName;
String existingStyleName = null;

if (mFilesManager.exists(filename)) {
DebateFormatFieldExtractor nameExtractor = new DebateFormatFieldExtractor(context, R.string.xml2elemName_name);
Expand All @@ -681,8 +681,10 @@ private void importIncomingFilePrompt(@Nullable Uri uri) {
} catch (IOException | SAXException e) {
Log.e(TAG, "Couldn't open existing file, even though one exists");
e.printStackTrace();
existingStyleName = "???";
}
if (existingStyleName == null)
existingStyleName = getString(R.string.importDebateFormat_placeholder_unknownStyleName);

DialogFragment fragment = ConfirmOverwriteDialogFragment.newInstance(uri, filename, existingStyleName);
fragment.show(getChildFragmentManager(), DIALOG_TAG_CONFIRM_OVERWRITE + filename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import android.content.res.Resources;
import android.util.Xml;

import androidx.annotation.Nullable;

import net.czlee.debatekeeper.R;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

Expand Down Expand Up @@ -63,7 +66,10 @@ public DebateFormatFieldExtractor(Context context, int fieldNameResId) {
* @param is an {@link InputStream}
* @return the name of the style, e.g. "British Parliamentary", or null if the file is not a
* valid debate format XML file.
* @throws IOException if thrown by {@link Xml#parse(InputStream, Xml.Encoding, ContentHandler)}
* @throws SAXException if thrown by {@link Xml#parse(InputStream, Xml.Encoding, ContentHandler)}
*/
@Nullable
public String getFieldValue(InputStream is) throws IOException, SAXException {
mFieldValue = null;

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/debating_timer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<string name="importDebateFormat_dialog_addendum_overwriteExistingDifferentName">
This will overwrite the existing custom style \"%1$s\", because its file has the same name.
If you don\'t want to lose %1$s, cancel and then import using a different file name.</string>
<string name="importDebateFormat_placeholder_unknownStyleName">(unknown)</string>
<string name="importDebateFormat_placeholder_unknownStyleName">[name unknown]</string>
<string name="importDebateFormat_dialog_button_yes">Import</string>
<string name="importDebateFormat_dialog_button_no">Cancel</string>
<string name="replaceDebateFormat_dialog_title">Replace debate style?</string>
Expand Down

0 comments on commit 03acbfd

Please sign in to comment.