From fe466d4c954941d8208e76cb2366a84c35e95672 Mon Sep 17 00:00:00 2001 From: Zhang Sheng Date: Fri, 7 Nov 2025 15:02:54 +0800 Subject: [PATCH] fix: add defensive checks in excel formula evaluation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Added null check for targetName.m_stack before accessing first element to prevent potential crashes 2. Added error handling when stack is empty by returning oERR operand and setting error flag 3. Improved exception handling by ensuring name.m_stack is never empty after exceptions 4. Added proper error state flags (m_hasError) when errors occur Log: Fixed potential crashes in Excel formula evaluation with invalid references Influence: 1. Test formulas referencing non-existent names or invalid references 2. Verify error handling when formula stack becomes empty 3. Test exception scenarios to confirm proper error state is maintained 4. Verify calculations continue normally when references are valid fix: 在Excel公式计算中添加防御性检查 1. 在访问第一个元素前添加对 targetName.m_stack 的空检查以防止潜在崩溃 2. 当栈为空时通过返回 oERR 操作数并设置错误标志添加错误处理 3. 通过确保异常后 name.m_stack 不会为空来改进异常处理 4. 发生错误时添加正确的错误状态标志(m_hasError) Log: 修复了引用无效对象时Excel公式计算可能崩溃的问题 Influence: 1. 测试引用不存在名称或无效引用的公式 2. 验证当公式栈为空时的错误处理 3. 测试异常场景以确认正确的错误状态被维护 4. 当引用有效时验证计算是否正常继续 --- 3rdparty/libs/fileext/excel/formula.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/3rdparty/libs/fileext/excel/formula.cpp b/3rdparty/libs/fileext/excel/formula.cpp index 5af0b49..8ae8763 100644 --- a/3rdparty/libs/fileext/excel/formula.cpp +++ b/3rdparty/libs/fileext/excel/formula.cpp @@ -988,7 +988,13 @@ void Formula::evaluateFormula(Name& name, int nameIndex, int level) { hasRelation = (hasRelation || targetName.m_hasRelation); } else { - res = targetName.m_stack[0]; + // 防御性检查:确保 stack 不为空 + if (!targetName.m_stack.empty()) + res = targetName.m_stack[0]; + else { + res = Operand(oERR); + hasError = true; + } } res.m_rank = LEAF_RANK; @@ -1021,6 +1027,12 @@ void Formula::evaluateFormula(Name& name, int nameIndex, int level) { name.m_evaluated = true; } catch (const std::exception &e) { std::cout << e.what() << std::endl; + // 确保异常后 m_stack 不为空,避免后续访问崩溃 + if (name.m_stack.empty()) { + name.m_stack.push_back(Operand(oERR)); + } + name.m_hasError = true; + name.m_evaluated = true; } }