From 7e0f5238e6d58d601549a5637a6191d2289c2c70 Mon Sep 17 00:00:00 2001 From: Matan Ziv-Av Date: Wed, 9 Oct 2024 17:48:10 +0300 Subject: [PATCH] Catch another out of memory error in image code --- .../src/main/java/com/termux/terminal/TerminalBitmap.java | 2 +- .../main/java/com/termux/terminal/TerminalEmulator.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalBitmap.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalBitmap.java index 77c75144d3..9758705bdc 100644 --- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalBitmap.java +++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalBitmap.java @@ -105,7 +105,7 @@ public TerminalBitmap(int num, byte[] image, int Y, int X, int cellW, int cellH, } else { try { bm = BitmapFactory.decodeByteArray(image, 0, image.length); - } catch (Exception e) { + } catch (OutOfMemoryError e) { Logger.logWarn(null, LOG_TAG, "Out of memory, cannot decode image"); } } diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java index 13e9aa2ee1..a4c6581d01 100644 --- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java +++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java @@ -197,6 +197,7 @@ public final class TerminalEmulator { private boolean ESC_P_sixel = false; private ArrayList ESC_OSC_data; private int ESC_OSC_colon = 0; + private boolean ESC_OSC_outofmem = false; private final SavedScreenState mSavedStateMain = new SavedScreenState(); private final SavedScreenState mSavedStateAlt = new SavedScreenState(); @@ -2089,7 +2090,9 @@ private void doOsc(int b) { // Collect base64 data for OSC 1337 ESC_OSC_colon = mOSCOrDeviceControlArgs.length(); ESC_OSC_data = new ArrayList(65536); + ESC_OSC_outofmem = false; } else if (ESC_OSC_colon >= 0 && mOSCOrDeviceControlArgs.length() - ESC_OSC_colon == 4) { + if (!ESC_OSC_outofmem) { try { byte[] decoded = Base64.decode(mOSCOrDeviceControlArgs.substring(ESC_OSC_colon), 0); for (int i = 0 ; i < decoded.length; i++) { @@ -2097,6 +2100,11 @@ private void doOsc(int b) { } } catch(Exception e) { // Ignore non-Base64 data. + } catch(OutOfMemoryError e) { + // Out of memory + // Keep decoding, but fo not collect the data + ESC_OSC_outofmem = true; + } } mOSCOrDeviceControlArgs.setLength(ESC_OSC_colon);