From b37a3d100b413fe3e17c26a45eae01d850695c9e Mon Sep 17 00:00:00 2001 From: Anthony Howe Date: Tue, 12 Nov 2024 07:44:08 -0500 Subject: [PATCH] GH-71 Use Throw instead of FatalError to avoid core dump. --- jni/Post4.c | 4 +++- jni/Post4Exception.java | 4 +++- jni/makefile.in | 7 +++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/jni/Post4.c b/jni/Post4.c index b13c3f3..d276629 100644 --- a/jni/Post4.c +++ b/jni/Post4.c @@ -923,7 +923,9 @@ Java_post4_jni_Post4_p4Create(JNIEnv *env, jobject self, jobject opts) (*env)->DeleteLocalRef(env, clazz); if (ctx == NULL) { - (*env)->FatalError(env, "cannot create Post4 context"); + /* GH-71 Use Throw instead of FatalError to avoid core dump. */ + (*env)->Throw(env, post4Exception(env, P4_THROW_GENERIC)); + return 0L; } p4HookInit(ctx, jHooks); diff --git a/jni/Post4Exception.java b/jni/Post4Exception.java index a532407..99045f3 100644 --- a/jni/Post4Exception.java +++ b/jni/Post4Exception.java @@ -100,6 +100,8 @@ public class Post4Exception extends Exception /* -4095..-256 reserved for the system (that's us). */ + public final static int THROW_SIGTERM = (-256); + public final static int THROW_WORDLIST = (-257); /* Out of word list space; invalid wid. */ public final static int THROW_GENERIC = (-4095); /* Unknown, generic, WTF. */ private final static String[] messages = { @@ -192,7 +194,7 @@ public Post4Exception() public Post4Exception(int code) { - super(code+" thrown: "+(code <= 0 ? messages[-code] : "(unknown)")); + super(code+" thrown: "+(THROW_future < code && code <= 0 ? messages[-code] : "(unknown)")); this.code = code; } diff --git a/jni/makefile.in b/jni/makefile.in index c1d56a3..1aac20a 100644 --- a/jni/makefile.in +++ b/jni/makefile.in @@ -123,3 +123,10 @@ testbye: build cygtest: build cp ${LIBNAME} post4jni.dll POST4_PATH="${POST4_PATH}"; java -Djava.library.path=".;..\\src" -cp ../.. ${MAIN} ../test/units.p4 + +# GH-71 Should cleanly terminate with an error. +test_badpath: ${PROG} + @printf "== %s == Bad POST4_PATH\n" $@ + ! POST4_PATH="/invalid" java -Djava.library.path="$${POST4_PATH}" -cp ../.. ${MAIN} + @echo "-OK-" +