From 2f86de4c1b4f90e3621e7df0dc67859e05085800 Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Sat, 21 May 2016 22:50:06 -0400 Subject: [PATCH] Protect against unexpected failures when encoding barcodes It was observed that some barcode encoders will fail if the data passed to them is not valid for the format. For example, the ITF encoder will throw an ArrayIndexOutOfBoundsException on the input "this is a test". --- .../card_locker/BarcodeImageWriterTask.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java b/app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java index d71bee2ac..a456f7239 100644 --- a/app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java +++ b/app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java @@ -45,7 +45,17 @@ class BarcodeImageWriterTask extends AsyncTask BitMatrix bitMatrix; try { - bitMatrix = writer.encode(cardId, format, imageWidth, imageHeight, null); + try + { + bitMatrix = writer.encode(cardId, format, imageWidth, imageHeight, null); + } + catch(Exception e) + { + // Cast a wider net here and catch any exception, as there are some + // cases where an encoder may fail if the data is invalid for the + // barcode type. If this happens, we want to fail gracefully. + throw new WriterException(e); + } final int WHITE = 0xFFFFFFFF; final int BLACK = 0xFF000000; @@ -86,9 +96,9 @@ class BarcodeImageWriterTask extends AsyncTask return bitmap; } - catch (WriterException | IllegalArgumentException e) + catch (WriterException e) { - Log.e(TAG, "Failed to generate barcode", e); + Log.e(TAG, "Failed to generate barcode of type " + format + ": " + cardId, e); } return null;