From 00b077fffe6e2a0211a285fd7cd7f37614187d26 Mon Sep 17 00:00:00 2001 From: Karl Nelson Date: Sat, 5 Sep 2026 19:12:33 -0700 Subject: [PATCH] Fix remaining null-to-empty-CefString CHECK() crash sites (#19/#20/#21) PR #43 already guarded CefRequest_N.cpp's SetURL and SetHeaderByName's name param against a non-null-but-empty Java String reaching CEF's internal CHECK(!x.empty()) (a hard abort in a Debug/coverage build, silently permitted in Release since CEF's own generated ctocpp wrapper no-ops on empty right after the CHECK -- which never runs, because CHECK aborts first). This is the same bug's remaining call sites, all of which already had a null-Java-string guard but not the empty-string one: CefPostDataElement_N.cpp's SetToFile, CefRequest_N.cpp's SetMethod and Set(), and CefResponse_N.cpp's SetHeaderByName. Backport of coverage/phase1-value-objects-phase2-handlers' 92aed53, adapted to this branch's existing null-guard structure rather than a literal cherry-pick (that commit predates PR #43's null-vs-empty split for SetURL/SetHeaderByName). Adds four regression tests to the existing NullParameterEdgeCaseTest.java (no separate test file, since master doesn't yet have PR #43's MalformedInputEdgeCaseTest.java where the future branch's own equivalent tests would otherwise belong). Verified: full rebuild (native + Java), 18/18 tests pass running NullParameterEdgeCaseTest together with CefDownloadItemTest in the same JVM (NullParameterEdgeCaseTest creates CEF objects without ever creating a browser, and per TestSetupExtension's own documented design only reaches CEF's fully-initialized browser-process state once some test in the same run has created one -- confirmed this is a pre-existing run-in-isolation limitation of that test file, not something this change introduces, by reproducing the same crash on an untouched pre-existing test method with no relation to this fix). Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01HBFmQs6JDypYP5qdC99yDq --- .../junittests/NullParameterEdgeCaseTest.java | 36 +++++++++++++++++++ native/CefPostDataElement_N.cpp | 9 ++++- native/CefRequest_N.cpp | 20 +++++++++-- native/CefResponse_N.cpp | 10 ++++-- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/java/tests/junittests/NullParameterEdgeCaseTest.java b/java/tests/junittests/NullParameterEdgeCaseTest.java index ccd22edc..eab26350 100644 --- a/java/tests/junittests/NullParameterEdgeCaseTest.java +++ b/java/tests/junittests/NullParameterEdgeCaseTest.java @@ -154,4 +154,40 @@ void messageRouterConfigWithNullFunctionNamesDoesNotThrowOnCreate() { assertDoesNotThrow(() -> router[0] = CefMessageRouter.create(config)); if (router[0] != null) router[0].dispose(); } + + // Regression tests for issue #21's remaining call sites: a non-null but + // empty Java String reaches the same CEF-internal CHECK(!x.empty()) a + // null string does (the null-vs-empty distinction only matters on the + // JNI marshaling side, not to CEF itself). N_SetURL and the request + // N_SetHeaderByName's name param already got this guard in an earlier + // fix; these four remaining call sites (CefPostDataElement's + // N_SetToFile, CefRequest's N_SetMethod/N_Set, CefResponse's + // N_SetHeaderByName) previously only guarded the null case, not empty. + @Test + void postDataElementSetToFileWithEmptyStringDoesNotThrow() { + CefPostDataElement element = CefPostDataElement.create(); + assertDoesNotThrow(() -> element.setToFile("")); + element.dispose(); + } + + @Test + void requestSetMethodWithEmptyStringDoesNotThrow() { + CefRequest request = CefRequest.create(); + assertDoesNotThrow(() -> request.setMethod("")); + request.dispose(); + } + + @Test + void requestSetWithEmptyUrlAndMethodDoesNotThrow() { + CefRequest request = CefRequest.create(); + assertDoesNotThrow(() -> request.set("", "", null, null)); + request.dispose(); + } + + @Test + void responseSetHeaderByNameWithEmptyNameDoesNotThrow() { + CefResponse response = CefResponse.create(); + assertDoesNotThrow(() -> response.setHeaderByName("", "value", true)); + response.dispose(); + } } diff --git a/native/CefPostDataElement_N.cpp b/native/CefPostDataElement_N.cpp index 3a8c4d8b..fbeed0d0 100644 --- a/native/CefPostDataElement_N.cpp +++ b/native/CefPostDataElement_N.cpp @@ -66,7 +66,14 @@ Java_org_cef_network_CefPostDataElement_1N_N_1SetToFile(JNIEnv* env, // string. if (!jfilename) return; - dataElement->SetToFile(GetJNIString(env, jfilename)); + // A non-null but empty Java string reaches the same DCHECK -- CEF's own + // CHECK(!fileName.empty()) aborts the Debug/coverage build (silently + // permitted in Release). See Thrameos/java-cef#20/#21 and + // CefRequest_N.cpp's matching guard. + CefString fileName = GetJNIString(env, jfilename); + if (fileName.empty()) + return; + dataElement->SetToFile(fileName); } JNIEXPORT void JNICALL diff --git a/native/CefRequest_N.cpp b/native/CefRequest_N.cpp index 2457ecae..821a555d 100644 --- a/native/CefRequest_N.cpp +++ b/native/CefRequest_N.cpp @@ -213,7 +213,13 @@ Java_org_cef_network_CefRequest_1N_N_1SetMethod(JNIEnv* env, // string rather than passing through an empty CefString. if (!jmethod) return; - request->SetMethod(GetJNIString(env, jmethod)); + // A non-null but empty Java string reaches the same DCHECK -- CEF's own + // CHECK(!method.empty()) aborts the Debug/coverage build (silently + // permitted in Release). See Thrameos/java-cef#21. + CefString method = GetJNIString(env, jmethod); + if (method.empty()) + return; + request->SetMethod(method); } JNIEXPORT jobject JNICALL @@ -326,9 +332,17 @@ Java_org_cef_network_CefRequest_1N_N_1Set(JNIEnv* env, // being empty right after), so skip the call entirely for a null Java // string -- the SetHeaderMap() call above has no such restriction and // should still happen regardless. + // + // A non-null but empty Java string reaches the same DCHECK -- CEF's own + // CHECK(!url.empty())/CHECK(!method.empty()) abort the Debug/coverage + // build (silently permitted in Release). See Thrameos/java-cef#21 and + // this file's other matching guards. if (jurl && jmethod) { - request->Set(GetJNIString(env, jurl), GetJNIString(env, jmethod), - postDataObj.GetCefObject(), headerMap); + CefString url = GetJNIString(env, jurl); + CefString method = GetJNIString(env, jmethod); + if (!url.empty() && !method.empty()) { + request->Set(url, method, postDataObj.GetCefObject(), headerMap); + } } } diff --git a/native/CefResponse_N.cpp b/native/CefResponse_N.cpp index c21deee0..dfc10919 100644 --- a/native/CefResponse_N.cpp +++ b/native/CefResponse_N.cpp @@ -156,8 +156,14 @@ Java_org_cef_network_CefResponse_1N_N_1SetHeaderByName(JNIEnv* env, // name. if (!jname) return; - return response->SetHeaderByName(GetJNIString(env, jname), - GetJNIString(env, jvalue), + // A non-null but empty Java name reaches the same DCHECK -- CEF's own + // CHECK(!name.empty()) aborts the Debug/coverage build (silently + // permitted in Release). See Thrameos/java-cef#19/#20/#21 and + // CefRequest_N.cpp's matching guard. + CefString name = GetJNIString(env, jname); + if (name.empty()) + return; + return response->SetHeaderByName(name, GetJNIString(env, jvalue), joverride != JNI_FALSE); }