Skip to content

Commit 2138147

Browse files
committed
prevent crash on invalid base64 encoding
1 parent d594bcb commit 2138147

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

app/src/main/java/co/tinode/tindroid/MediaControl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,13 @@ boolean ensurePlayerReady(final int seq, Map<String, Object> data,
143143
return false;
144144
}
145145
} else if ((val = data.get("val")) instanceof String) {
146-
byte[] source = Base64.decode((String) val, Base64.DEFAULT);
147-
mAudioPlayer.setDataSource(new MemoryAudioSource(source));
146+
try {
147+
byte[] source = Base64.decode((String) val, Base64.DEFAULT);
148+
mAudioPlayer.setDataSource(new MemoryAudioSource(source));
149+
} catch (IllegalArgumentException ex) {
150+
Log.w(TAG, "Unable to play audio: invalid data");
151+
Toast.makeText(mContext, R.string.unable_to_play_audio, Toast.LENGTH_SHORT).show();
152+
}
148153
} else {
149154
mAudioControlCallback.reset();
150155
Log.w(TAG, "Unable to play audio: missing data");

app/src/main/java/co/tinode/tindroid/UiUtils.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,11 +1457,20 @@ static public Uri getUriVal(String name, Map<String, Object> data) {
14571457
return url != null ? Uri.parse(url.toString()) : null;
14581458
}
14591459

1460+
static public @Nullable byte[] decodeByteArray(Object val) {
1461+
byte[] bits = null;
1462+
if (val instanceof String) {
1463+
try {
1464+
bits = Base64.decode((String) val, Base64.DEFAULT);
1465+
} catch (IllegalArgumentException ignored) {}
1466+
} else {
1467+
bits = val instanceof byte[] ? (byte[]) val : null;
1468+
}
1469+
return bits;
1470+
}
1471+
14601472
static public @Nullable byte[] getByteArray(String name, @NotNull Map<String, Object> data) {
1461-
Object val = data.get(name);
1462-
return val instanceof String ?
1463-
Base64.decode((String) val, Base64.DEFAULT) :
1464-
val instanceof byte[] ? (byte[]) val : null;
1473+
return decodeByteArray(data.get(name));
14651474
}
14661475

14671476
static public @NotNull List<String> getRequiredCredMethods(@NotNull Tinode tinode, @NotNull String forAuthLevel) {

app/src/main/java/co/tinode/tindroid/format/FullFormatter.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import android.util.DisplayMetrics;
3434
import android.util.Log;
3535
import android.util.TypedValue;
36-
import android.view.Gravity;
3736
import android.view.View;
3837
import android.widget.TextView;
3938

@@ -230,7 +229,9 @@ protected SpannableStringBuilder handleAudio(final Context ctx, List<SpannableSt
230229
Object val = data.get("preview");
231230
byte[] preview = null;
232231
if (val instanceof String) {
233-
preview = Base64.decode((String) val, Base64.DEFAULT);
232+
try {
233+
preview = Base64.decode((String) val, Base64.DEFAULT);
234+
} catch (IllegalArgumentException ignored) {}
234235
}
235236

236237
if (preview != null && preview.length > MIN_AUDIO_PREVIEW_LENGTH) {
@@ -390,9 +391,10 @@ private CharacterStyle createImageSpan(final Context ctx, final Object val, fina
390391
boolean usePreviewAsMainImage = false;
391392
// If the message is not yet sent, the bits could be raw byte[] as opposed to
392393
// base64-encoded.
393-
byte[] bits = (val instanceof String) ?
394-
Base64.decode((String) val, Base64.DEFAULT) : (byte[]) val;
395-
bmpPreview = BitmapFactory.decodeByteArray(bits, 0, bits.length);
394+
byte[] bits = UiUtils.decodeByteArray(val);
395+
if (bits != null) {
396+
bmpPreview = BitmapFactory.decodeByteArray(bits, 0, bits.length);
397+
}
396398
if (bmpPreview != null) {
397399
// Check if the inline bitmap is big enough to be used as primary image.
398400
int previewWidth = bmpPreview.getWidth();
@@ -658,9 +660,7 @@ protected SpannableStringBuilder handleAttachment(final Context ctx, final Map<S
658660

659661
int byteCount = getIntVal("size", data);
660662
if (byteCount <= 0) {
661-
Object val = data.get("val");
662-
byte[] bits = (val instanceof String) ?
663-
Base64.decode((String) val, Base64.DEFAULT) : (byte[]) val;
663+
byte[] bits = UiUtils.decodeByteArray(data.get("val"));
664664
if (bits != null) {
665665
byteCount = bits.length;
666666
}

app/src/main/java/co/tinode/tindroid/format/QuoteFormatter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ private CharacterStyle createImageSpan(Context ctx, Object val, String ref,
9292
try {
9393
// If the message is not yet sent, the bits could be raw byte[] as opposed to
9494
// base64-encoded.
95-
byte[] bits = (val instanceof String) ?
96-
Base64.decode((String) val, Base64.DEFAULT) : (byte[]) val;
97-
Bitmap bmp = BitmapFactory.decodeByteArray(bits, 0, bits.length);
95+
byte[] bits = UiUtils.decodeByteArray(val);
96+
Bitmap bmp = null;
97+
if (bits != null) {
98+
bmp = BitmapFactory.decodeByteArray(bits, 0, bits.length);
99+
}
98100
if (bmp != null) {
99101
if (dim.square) {
100102
thumbnail = new BitmapDrawable(res,

0 commit comments

Comments
 (0)