Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/docparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,18 @@ static std::string doConvertFile(const std::string &filename, std::string suffix
std::transform(suffix.begin(), suffix.end(), suffix.begin(),
[](unsigned char c) { return std::tolower(c); });

std::unique_ptr<fileext::FileExtension> document = createParser(filename, suffix);
if (!document) {
throw std::logic_error("Unsupported file extension: " + filename);
}

// createParser() 的调用和 nullptr 检查都必须在 try 块内:
// 1) 构造期可能抛异常(如某些解析器在打开损坏文件时);
// 2) 不支持的扩展名不应抛异常——上层调用方(如 dde-file-manager 的
// TextExtractor::extract)可能没有 try-catch,异常一旦逃逸会触发
// std::terminate -> abort。此处返回空串,与 tryConvertWithTruncation 一致。
try {
std::unique_ptr<fileext::FileExtension> document = createParser(filename, suffix);
if (!document) {
std::cout << "Unsupported file extension: " << filename << std::endl;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Avoid writing directly to std::cout in a lower-level conversion helper; prefer a logging facility or std::cerr, or make logging configurable.

This ties a low-level parsing routine to user-facing stdout, which can be ignored or reserved for structured output in GUIs, services, or libraries. Prefer a logging facility, std::cerr, or delegating logging to the caller, potentially behind a debug/verbose flag, so normal use doesn’t emit unexpected console output.

return {};
}

document->convert();
// Use move semantics to avoid copying
return std::move(document->m_text);
Expand Down
Binary file added tests/file/test_crash_unsupported_ext.dat
Binary file not shown.
Loading