From b51f50b96b48a6833a6e44eed7e94a143c3d3743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Sun, 22 Mar 2026 12:48:27 +0100 Subject: [PATCH] Address review: fix NULL asymmetry in backend sync decisions (PR #5511) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - BackendSyncDecision.cpp: treat asymmetric NULLs as mismatches (client=NULL + backend="bob" → SYNC_USER, not SYNC_NONE) - Tests: replace weak null assertions (a >= 0 always true) with specific checks for SYNC_USER/SYNC_SCHEMA on asymmetric NULLs, both-null → no sync, schema asymmetric null (+2 tests, plan 15→17) --- lib/BackendSyncDecision.cpp | 13 +++++++++++-- test/tap/tests/unit/backend_sync_unit-t.cpp | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/BackendSyncDecision.cpp b/lib/BackendSyncDecision.cpp index 04375b1b4..b9c64e402 100644 --- a/lib/BackendSyncDecision.cpp +++ b/lib/BackendSyncDecision.cpp @@ -20,7 +20,12 @@ int determine_backend_sync_actions( int actions = SYNC_NONE; // Username mismatch → CHANGE USER required - if (client_user && backend_user) { + // Asymmetric NULLs (one set, other not) count as mismatch + if (client_user == nullptr && backend_user != nullptr) { + actions |= SYNC_USER; + } else if (client_user != nullptr && backend_user == nullptr) { + actions |= SYNC_USER; + } else if (client_user && backend_user) { if (strcmp(client_user, backend_user) != 0) { actions |= SYNC_USER; } @@ -29,7 +34,11 @@ int determine_backend_sync_actions( // Schema mismatch → USE required // Only check if usernames match (user change handles schema too) if (!(actions & SYNC_USER)) { - if (client_schema && backend_schema) { + if (client_schema == nullptr && backend_schema != nullptr) { + actions |= SYNC_SCHEMA; + } else if (client_schema != nullptr && backend_schema == nullptr) { + actions |= SYNC_SCHEMA; + } else if (client_schema && backend_schema) { if (strcmp(client_schema, backend_schema) != 0) { actions |= SYNC_SCHEMA; } diff --git a/test/tap/tests/unit/backend_sync_unit-t.cpp b/test/tap/tests/unit/backend_sync_unit-t.cpp index c67d5efd5..8dd26dbe2 100644 --- a/test/tap/tests/unit/backend_sync_unit-t.cpp +++ b/test/tap/tests/unit/backend_sync_unit-t.cpp @@ -52,15 +52,24 @@ static void test_multiple_mismatches() { static void test_null_handling() { // null users — no crash + // Asymmetric NULL: one side null, other not → mismatch int a = determine_backend_sync_actions(nullptr, "user", "db", "db", true, true); - ok(a == SYNC_NONE || a >= 0, "null client_user: no crash"); + ok((a & SYNC_USER) != 0, "null client_user + non-null backend → SYNC_USER"); a = determine_backend_sync_actions("user", nullptr, "db", "db", true, true); - ok(a == SYNC_NONE || a >= 0, "null backend_user: no crash"); + ok((a & SYNC_USER) != 0, "non-null client_user + null backend → SYNC_USER"); + + // Both null → no mismatch + a = determine_backend_sync_actions(nullptr, nullptr, "db", "db", true, true); + ok(a == SYNC_NONE, "both users null → no sync"); + + // Schema asymmetric null + a = determine_backend_sync_actions("user", "user", nullptr, "db", true, true); + ok((a & SYNC_SCHEMA) != 0, "null client_schema + non-null backend → SYNC_SCHEMA"); } int main() { - plan(15); + plan(17); int rc = test_init_minimal(); ok(rc == 0, "test_init_minimal() succeeds"); @@ -70,8 +79,8 @@ int main() { test_user_and_schema_mismatch(); // 2 test_autocommit_mismatch(); // 2 test_multiple_mismatches(); // 2 - test_null_handling(); // 2 - // Total: 1+2+2+2+2+2+2+2 = 15 + test_null_handling(); // 4 + // Total: 1+2+2+2+2+2+2+4 = 17 test_cleanup_minimal(); return exit_status();