[gs-commits] mupdf 1.16.1.106 jni: Use same code pattern when opening
[email protected] (Tor Andersson) Tue, 22 Oct 2019 11:29:12 +0000 (UTC)
| Newsgroups | gmane.comp.printing.ghostscript.cvs |
|---|---|
| Message-ID | <[email protected]> |
commit 75dbbd97826e7191ee539f9934e607eb93247a42 Author: Sebastian Rasmussen <[email protected]> Date: Sat Oct 19 01:03:02 2019 +0200 jni: Use same code pattern when opening and saving documents. diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c index 5063398..c1d521e 100644 --- a/platform/java/mupdf_native.c +++ b/platform/java/mupdf_native.c @@ -7852,7 +7852,7 @@ FUN(PDFDocument_nativeSaveWithStream)(JNIEnv *env, jobject self, jobject jstream SeekableStreamState *state = NULL; jobject stream = NULL; jbyteArray array = NULL; - fz_output *out; + fz_output *out = NULL; const char *options = NULL; pdf_write_options pwo; @@ -7889,18 +7889,25 @@ FUN(PDFDocument_nativeSaveWithStream)(JNIEnv *env, jobject self, jobject jstream fz_try(ctx) { - state = fz_malloc(ctx, sizeof(SeekableStreamState)); - state->stream = stream; - state->array = array; - - out = fz_new_output(ctx, sizeof state->buffer, state, SeekableOutputStream_write, NULL, SeekableOutputStream_drop); - out->seek = SeekableOutputStream_seek; - out->tell = SeekableOutputStream_tell; - - /* these are now owned by 'out' */ - state = NULL; - stream = NULL; - array = NULL; + if (jstream) + { + /* No exceptions can occur from here to stream owning state, so we must not free state. */ + state = fz_malloc(ctx, sizeof(SeekableStreamState)); + state->stream = stream; + state->array = array; + + /* Ownership transferred to state. */ + stream = NULL; + array = NULL; + + /* Stream takes ownership of state. */ + out = fz_new_output(ctx, sizeof state->buffer, state, SeekableOutputStream_write, NULL, SeekableOutputStream_drop); + out->seek = SeekableOutputStream_seek; + out->tell = SeekableOutputStream_tell; + + /* these are now owned by 'out' */ + state = NULL; + } pdf_parse_write_options(ctx, &pwo, options); pdf_write_document(ctx, pdf, out, &pwo); @@ -7908,15 +7915,14 @@ FUN(PDFDocument_nativeSaveWithStream)(JNIEnv *env, jobject self, jobject jstream } fz_always(ctx) { + fz_drop_output(ctx, out); if (options) (*env)->ReleaseStringUTFChars(env, joptions, options); - fz_drop_output(ctx, out); } fz_catch(ctx) { - if (stream) (*env)->DeleteGlobalRef(env, stream); - if (array) (*env)->DeleteGlobalRef(env, array); - fz_free(ctx, state); + (*env)->DeleteGlobalRef(env, array); + (*env)->DeleteGlobalRef(env, stream); jni_rethrow(env, ctx); } } http://git.ghostscript.com/?p=mupdf.git;a=commit;h=75dbbd97826e7191ee539f9934e607eb93247a42 -- MuPDF library Artifex Software, Inc.