modernc.org/sqlite: 修复 pre_update_hook 空指针解引用
pre_update_hook.go, pre_update_hook_test.go: return SQLite's error when the...
Go 开发者若依赖此库的预更新钩子处理数据库变更,必须升级以规避空指针崩溃风险。该修复解决了可能导致进程意外终止的破坏性缺陷,建议立即更新版本。
pre_update_hook.go, pre_update_hook_test.go: return SQLite's error when the hook asks for a row value SQLite cannot give
pre_update_hook.go、pre_update_hook_test.go:当钩子请求 SQLite 无法提供的行值时,返回 SQLite 的错误
SQLitePreUpdateData.Old and New did not look at what sqlite3_preupdate_old and sqlite3_preupdate_new return, and used the value pointer in any case. SQLite reads the old row from the database only when the hook first asks for it. For a row it cannot read, one whose overflow chain leaves the file for example, it returns SQLITE_CORRUPT and stores no pointer, and the wrapper went on with the slot as malloc had left it: a nil pointer dereference inside the hook, which ends the process unless the application recovers there.
SQLitePreUpdateData.Old 和 New 没有检查 sqlite3_preupdate_old 和 sqlite3_preupdate_new 的返回值,而是无论如何都使用了该值指针。SQLite 仅在钩子首次请求时才从数据库中读取旧行。对于它无法读取的行(例如溢出链超出文件范围的行),它会返回 SQLITE_CORRUPT 且不存储任何指针,而包装器则继续使用该槽位中 malloc 遗留的状态:即钩子内部出现空指针解引用,除非应用程序在此处进行恢复,否则这将导致进程终止。
value() now checks the code and returns SQLite's error from Old or New. The slot serves every column of a row, so it is cleared before each call, and a nil pointer after SQLITE_OK is an error as well; neither can be reached while SQLite keeps its contract and the code is checked. The BLOB branch used the pointer of sqlite3_value_blob unchecked, which is nil when SQLite cannot make the bytes; that is an error now too.
value() 现在会检查代码并从 Old 或 New 返回 SQLite 的错误。由于一个槽位服务于行的每一列,因此在每次调用前都会将其清空;在 SQLITE_OK 之后若仍为空指针也属于错误情况;只要 SQLite 遵守其契约且代码经过检查,这两种情况都不会发生。BLOB 分支曾未经检查地使用 sqlite3_value_blob 的指针,当 SQLite 无法获取字节时该指针为 nil;这现在也被视为错误。
TestPreUpdateHookRowUnreadable builds a database, points the first overflow page of a row past the end of the file and runs DELETE and UPDATE with a hook that calls Old. Old returns SQLITE_CORRUPT, as the statement itself does. On the code before this commit both cases fail with the dereference.
TestPreUpdateHookRowUnreadable 构建了一个数据库,将某行的第一个溢出页指向文件末尾之外,并运行带有调用 Old 的钩子的 DELETE 和 UPDATE 操作。Old 返回 SQLITE_CORRUPT,正如语句本身所做的那样。在此次提交之前的代码中,这两种情况均因解引用而失败。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力