Skip to content

Commit 7f3a881

Browse files
pppanghu77deepin-bot[bot]
authored andcommitted
fix: enhance error handling in formula evaluation
- Added boundary checks to ensure valid memory access when evaluating target names, preventing access to invalid indices in the name object list. - Introduced default error text handling for unknown error codes to enhance robustness and user feedback. Log: enhance error handling in formula evaluation Bug: https://pms.uniontech.com/bug-view-327367.html
1 parent 19beb6c commit 7f3a881

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

3rdparty/libs/fileext/excel/formula.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -743,26 +743,34 @@ void Formula::evaluateFormula(Name& name, int nameIndex, int level) {
743743
// tName
744744
else if (opCode == 0x03) {
745745
unsigned short targetNameIndex = m_book->readByte<unsigned short>(data, pos+1, 2) - 1;
746-
// Only change with BIFF version is number of trailing UNUSED bytes!
747-
Name& targetName = m_book->m_nameObjList[targetNameIndex];
748-
// Recursive
749-
if (!targetName.m_evaluated)
750-
evaluateFormula(targetName, targetNameIndex, level+1);
751746

752-
Operand res(oUNK);
753-
if (!targetName.m_stack.empty() && !(targetName.m_macro || targetName.m_isBinary || targetName.m_hasError))
754-
res = targetName.m_stack[0];
755-
res.m_rank = LEAF_RANK;
756-
757-
if (targetName.m_scope == -1) {
758-
res.m_text = targetName.m_name;
759-
hasError = (hasError || targetName.m_macro || targetName.m_isBinary || targetName.m_hasError);
760-
hasRelation = (hasRelation || targetName.m_hasRelation);
747+
// 添加边界检查,防止访问无效内存
748+
if (targetNameIndex >= m_book->m_nameObjList.size()) {
749+
hasError = true;
750+
stack.push_back(errorOp);
761751
}
762752
else {
763-
res.m_text = m_book->m_sheetNames[targetName.m_scope] + "%s!" + targetName.m_name;
753+
// Only change with BIFF version is number of trailing UNUSED bytes!
754+
Name& targetName = m_book->m_nameObjList[targetNameIndex];
755+
// Recursive
756+
if (!targetName.m_evaluated)
757+
evaluateFormula(targetName, targetNameIndex, level+1);
758+
759+
Operand res(oUNK);
760+
if (!targetName.m_stack.empty() && !(targetName.m_macro || targetName.m_isBinary || targetName.m_hasError))
761+
res = targetName.m_stack[0];
762+
res.m_rank = LEAF_RANK;
763+
764+
if (targetName.m_scope == -1) {
765+
res.m_text = targetName.m_name;
766+
hasError = (hasError || targetName.m_macro || targetName.m_isBinary || targetName.m_hasError);
767+
hasRelation = (hasRelation || targetName.m_hasRelation);
768+
}
769+
else {
770+
res.m_text = m_book->m_sheetNames[targetName.m_scope] + "%s!" + targetName.m_name;
771+
}
772+
stack.push_back(res);
764773
}
765-
stack.push_back(res);
766774
}
767775
// tRef
768776
else if (opCode == 0x04) {

0 commit comments

Comments
 (0)