Skip to content

Commit 3838e80

Browse files
pppanghu77deepin-bot[bot]
authored andcommitted
fix(docparser): prevent exception escape in doConvertFile from aborting host process
- Move createParser() call and nullptr check inside the try block in doConvertFile() so parser constructor exceptions are also caught - Replace throwing std::logic_error with a log + return {} for unsupported extensions, aligning with tryConvertWithTruncation() - Prevents std::logic_error from escaping DocParser::convertFile, which previously triggered std::terminate -> abort in callers without try-catch (e.g. dde-file-manager TextExtractor::extract) - Add binary test file test_crash_unsupported_ext.dat to reproduce/regress the unsupported-extension crash path 修复(docparser): 防止 doConvertFile 中异常逃逸导致宿主进程崩溃 - 将 createParser() 调用和 nullptr 检查移入 doConvertFile() 的 try 块内,确保解析器构造期异常也被捕获 - 不支持的扩展名由抛出 std::logic_error 改为打印日志并返回空串,与 tryConvertWithTruncation() 写法保持一致 - 防止 std::logic_error 从 DocParser::convertFile 逃逸,原先会在无 try-catch 的调用方(如 dde-file-manager 的 TextExtractor::extract)触发 std::terminate -> abort - 新增二进制测试文件 test_crash_unsupported_ext.dat,用于复现/回归不支持扩展名的崩溃路径 Log: 修复 doConvertFile 中异常逃逸导致宿主进程 std::terminate -> abort 的问题,将不支持扩展名的处理由抛异常改为返回空串,并把 createParser 调用纳入 try 保护 Task: https://pms.uniontech.com/task-view-391293.html
1 parent 4ac50cd commit 3838e80

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

src/docparser.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,18 @@ static std::string doConvertFile(const std::string &filename, std::string suffix
283283
std::transform(suffix.begin(), suffix.end(), suffix.begin(),
284284
[](unsigned char c) { return std::tolower(c); });
285285

286-
std::unique_ptr<fileext::FileExtension> document = createParser(filename, suffix);
287-
if (!document) {
288-
throw std::logic_error("Unsupported file extension: " + filename);
289-
}
290-
286+
// createParser() 的调用和 nullptr 检查都必须在 try 块内:
287+
// 1) 构造期可能抛异常(如某些解析器在打开损坏文件时);
288+
// 2) 不支持的扩展名不应抛异常——上层调用方(如 dde-file-manager 的
289+
// TextExtractor::extract)可能没有 try-catch,异常一旦逃逸会触发
290+
// std::terminate -> abort。此处返回空串,与 tryConvertWithTruncation 一致。
291291
try {
292+
std::unique_ptr<fileext::FileExtension> document = createParser(filename, suffix);
293+
if (!document) {
294+
std::cout << "Unsupported file extension: " << filename << std::endl;
295+
return {};
296+
}
297+
292298
document->convert();
293299
// Use move semantics to avoid copying
294300
return std::move(document->m_text);
19 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)