V569. Truncation of constant value.


The analyzer detected a potential error: a constant value is truncated when it is assigned into a variable. Consider this sample:

int A[100];
unsigned char N = sizeof(A);

The size of the 'A' array (in Win32/Win64) is 400 bytes. The value range for unsigned char is 0..255. Consequently, the 'N' variable cannot store the size of the 'A' array.

The V569 warning tells you that you have chosen a wrong type to store this size or that you actually intended to calculate the number of items in the array instead of the array's size.

If you have chosen a wrong type, you may correct the code this way:

size_t N = sizeof(A);

If you intended to calculate the number of items in the array, you should rewrite the code this way:

unsigned char N = sizeof(A) / sizeof(*A);

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-197.

You can look at examples of errors detected by the V569 diagnostic.


Bugs Found

Checked Projects
334
Collected Errors
12 668