create_input_stream leaks the buffer global ref when malloc fails

Author: QiuYucheng2003Created Sep 22, 2026Updated Sep 22, 2026

create_input_stream promotes the read buffer to a JNI global ref, then returns NULL on malloc failure without DeleteGlobalRef. The caller never receives an InputStream, so it cannot clean that ref up.

Where

app/src/main/cpp/jni/stream/input_stream.c (edcbd95)

  • Line 43: NewByteArray creates the buffer
  • Line 44: NewGlobalRef stores it back into buffer
  • Lines 51–53: malloc fails and the function returns NULL
  • Lines 67–68: destroy_input_stream does call DeleteGlobalRef on is and buffer, but only when an InputStream already exists

What leaks

On the malloc failure path the global ref in buffer is dropped on the floor. The local ref from NewByteArray is released when the JNI call returns; the global ref is not. destroy_input_stream cannot run, because the only pointer that holds buffer was never returned.

Impact

This is the out-of-memory path for a small InputStream allocation, so it is rare. When it does happen, the global ref and the byte array it keeps alive stay in the global reference table for the rest of the process.

Suggested fix

Call DeleteGlobalRef(buffer) before return NULL in the malloc failure branch. Alternatively, call NewGlobalRef only after malloc succeeds.

Source: xiaojieonly/Ehviewer_CN_SXJ