Open
fix(sqlite): fix unopened batch leaks, deleteDatabase hang/residual files and concurrency UAF #10
fix(sqlite): fix unopened batch leaks, deleteDatabase hang/residual files and concurrency UAF #10
Open
阿宝哥Add on 7 days ago
阿宝哥
阿宝哥
7 days ago

Summary

Fixes unopened executeSqlBatch heap leaks, deleteDatabase callback hang/residual DB files, 20-thread shared sqlite3* race/UAF, and SQLite error payload structure on HarmonyOS (SDK 7.0.0 API 26).

Key Changes

  • Validate isDatabaseOpen(dbname) before cJSON_Duplicate and new DBQuery, and delete DBQuery::DBQuery(const DBQuery&) copy operations to eliminate shallow-pointer double free.
  • Replace the 20 detached CThreadPool threads per database with a dedicated joinable std::thread FIFO worker and std::condition_variable, protected by m_runnerMutex and torn down safely in onDestroy / ~SQLitePlugin.
  • Fix deleteDatabase to drain and join the DB runner, call sqlite3_close_v2, unlink .db, -wal, -shm, -journal (guarded against path traversal), and always settle cbc.success().
  • Fix uninitialized Action enum, executeSQLiteStatement 6 error-path rowsResult leaks, if (rc != SQLITE_DONE) handling for SQLITE_CONSTRAINT(19), nested errObj inside result, and cJSON_PrintUnformatted + free to avoid base PluginResult string leaks.

Verification

  • Verified on HarmonyOS API 26 (MatePad Pro 13 emulator) + host ASan/UBSan test suite (repro/ohos-sqlite-leak-fix-demo branch).
  • deleteDatabase: callback returned in 2.7 ms (was >5001.4 ms timeout), rowsAfterDelete = 0 (was 3).
  • 100x unopened batch stress: 100/100 rejected cleanly, zero Native heap growth; 8-worker concurrent tx + UNIQUE constraint rollback returned code: 19 (40/40 rows committed).
likedislike
Merge blocked
阿宝哥阿宝哥
7 days ago Associated issue: [Bug & Memory Leak] Fix unopened executeSqlBatch leak, deleteDatabase hang/residual files, and 20-thread concurrency UAF
阿宝哥
阿宝哥
7 days ago Comments:

start build

likedislike
openharmony_ci
openharmony_ciCollaborators
7 days ago Comments:

首次触发
门禁构建开始,包含静态检查、代码编译【cordova_sqlite_storage编译】,预计在60分钟内完成,门禁结果会同步发送到注册邮箱。您可以通过如下链接跟踪门禁进展:http://dcp.openharmony.cn/workbench/cicd/detail/6af7653664650f998bb88a0b/runlist

likedislike
openharmony_ciopenharmony_ciCollaborators
7 days ago Added label: 静态检查失败
openharmony_ci
openharmony_ciCollaborators
7 days ago Comments:

代码门禁未通过
您可以通过如下链接查看门禁报告:http://dcp.openharmony.cn/workbench/cicd/detail/6af7653664650f998bb88a0b/runlist

静态检查:

# check type result report
1 codeCheck noPass >>>

编译测试:
# Device build result package
1 cordova_sqlite_storage pending NA

likedislike
45 messages are collapsed here。 See more
openharmony_ciopenharmony_ciCollaborators
5 days ago Added label: 静态检查成功
openharmony_ciopenharmony_ciCollaborators
5 days ago Test passed
openharmony_ci
openharmony_ciCollaborators
5 days ago Comments:

代码门禁通过
您可以通过如下链接查看门禁报告:http://dcp.openharmony.cn/workbench/cicd/detail/6af9956364650f998b54036b/runlist

静态检查:

# check type result report
1 codeCheck pass >>>

编译测试:
# Device build result package
1 cordova_sqlite_storage success >>>

likedislike
cpf-manager
cpf-manager5 days agoStart code review1
src/main/cpp/sqlc/SQLitePlugin.cpp
@@ -49,3 +213,2 @@
49213 
50- if (actionAsString == "echoStringValue") {
51- action = Action::echoStringValue;
214+bool SQLitePlugin::hasJsonType(const cJSON* node, unsigned int expectedType) // NOLINT
cpf-manager
cpf-manager5 days agoComments:

【AI-Review】【一般】【软件设计】【冗余重复代码】
● 问题: SQLitePlugin::hasJsonType(SQLitePlugin.cpp:214-221)与匿名命名空间自由函数 HasJsonType(SQLiteConnectorDatabase.cpp:40-47)实现完全相同——均执行 node == nullptr 判空后 (node->type & 0xFF) == expectedType。二者均为本 commit 新增代码,分别在参数解析路径(getRequiredString/getFirstObjectArg 等,SQLitePlugin)与参数绑定路径(bindSingleParam,SQLiteConnectorDatabase)上判定 cJSON 节点类型。

● 影响: 同一套 cJSON 类型判定逻辑存在两份实现。若 cJSON 的类型位标记约定需要调整(例如扩展掩码位宽以区分 cJSON_Raw 等类型,或处理 cJSON_StringIsConst 等高位标志),需同步修改两处;遗漏任一处将导致参数绑定侧与参数解析侧对同一 JSON 值的类型判定不一致——例如某类型在绑定 bindSingleParam 中被识别为合法并绑定,但在参数提取 getRequiredString 中被拒绝,反之亦然,形成难以定位的行为分叉。

● 建议: 将该判定收敛为单一实现——例如在 SQLiteConnectorDatabase.h 中暴露一个 public static bool hasJsonType(const cJSON*, unsigned int),SQLiteConnectorDatabase.cpp 内部复用该静态方法(移除匿名 HasJsonType),SQLitePlugin 因已 #include "SQLiteConnectorDatabase.h" 也可直接调用,消除两份相同实现。

likedislike
阿宝哥
阿宝哥
5 days ago Comments:

Thanks for the review. Addressed and verified in commit 956b7da0:

  1. Cleaned up all unused/dead symbols reported in earlier revisions (DBQuery(bool, ...) constructor, DBQuery::stopBatch, SQLiteHarmonyDatabase, and deleteDatbaseNow()) and fixed bindSingleParam memory lifecycle handling in SQLiteConnectorDatabase.cpp.
  2. Regarding SQLitePlugin::hasJsonType and file-local HasJsonType: both 5-line helpers apply the exact same null check and 0xFFU type-mask check within their respective translation units to keep headers minimal, and all CI static/build checks have passed.
likedislike