gpt4 book ai didi

c++ - C26451 访问 CStringArray 中的项的算术溢出

转载 作者:搜寻专家 更新时间:2023-10-31 01:24:38 25 4
gpt4 key购买 nike

得到这段代码:

CString CMeetingScheduleAssistantApp::UpdateDateFormatString(COleDateTime& rDate, CString strDateFormatString)
{
CString strDayNumber, strNewDateFormatString = strDateFormatString;

if (theApp.UseTranslationINI())
{
strNewDateFormatString.Replace(_T("%B"),
m_aryDateTrans[DATE_TRANS_MONTH][rDate.GetMonth() - 1]);
strNewDateFormatString.Replace(_T("%A"),
m_aryDateTrans[DATE_TRANS_DAY][rDate.GetDayOfWeek() - 1]);
strNewDateFormatString.Replace(_T("%b"),
m_aryDateTrans[DATE_TRANS_MONTH_SHORT][rDate.GetMonth() - 1]);
strNewDateFormatString.Replace(_T("%a"),
m_aryDateTrans[DATE_TRANS_DAY_SHORT][rDate.GetDayOfWeek() - 1]);

strDayNumber = rDate.Format(_T("%d"));
strNewDateFormatString.Replace(_T("%d"), strDayNumber);
strDayNumber = rDate.Format(_T("%#d"));
strNewDateFormatString.Replace(_T("%#d"), strDayNumber);
}

return strNewDateFormatString;
}

对于四行 Replace 代码,我收到代码分析警告:

Warning C26451

Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2).

[] 括号内的第一个值是 int 类型,但第二个是 1 的字面值:

Code

变量m_aryDateTrans定义为:

CStringArray m_aryDateTrans[NUM_DATE_TRANS];

所以我不确定我需要在这里进行什么样的转换来抑制这个警告。

我正在 32 位和 64 位环境中编译。

最佳答案

[] operator CStringArray 接受一个 INT_PTR 参数,它在 x64 构建中是 64 位,在 x86 中是 32 位。因此,您应该使用下面显示的(相当笨拙的)代码:

strNewDateFormatString.Replace(_T("%B"),
m_aryDateTrans[DATE_TRANS_MONTH][INT_PTR(rDate.GetMonth()) - INT_PTR(1)]);

(您也许可以在没有第二次施法的情况下逃脱 - 试试看!)

关于c++ - C26451 访问 CStringArray 中的项的算术溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57987271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com