lnt-arithmetic-overflow: 하위 식은 더 광범위한 형식에 할당되기 전에 오버플로될 수 있습니다
C 및 C++에서 산술 연산은 결과에 할당된 형식의 너비가 아니라 가장 광범위한 피연산자 형식을 사용하여 계산됩니다. 결과가 더 광범위한 형식으로 변환되면 개발자가 작업에서 더 좁은 형식의 피연산자를 오버플로할 수 있음을 나타냅니다.
예제
void overflow(int a, int b) {
int64_t mul = a * b; // Flagged: 32-bit operation may overflow.
int64_t shift = a << 34; // Flagged: Shift would overflow.
int64_t mul2 = mul + b; // OK: 'mul' is 64-bit so the addition expression is
// evaluated using 64-bit operations.
}
해결방법
void overflow(int a, int b) {
int64_t mul = static_cast<int64_t>(a) * b;
int64_t shift = static_cast<int64_t>(a) << 34;
}
lnt-arithmetic-overflow
Visual Studio C++ IntelliSense Linter 검사 lnt-arithmetic-overflow에 대한 참조입니다.
learn.microsoft.com
'Language > C++' 카테고리의 다른 글
c++ 포인터 기초 (0) | 2022.01.31 |
---|---|
c++ 실행 속도 높이기 (0) | 2021.12.18 |
c++ wntdll.pdb 오류 해결하기 - 기호 서버 체크 (0) | 2021.11.07 |