V540. Member 'x' should point to string terminated by two 0 characters.


In Windows API, there are structures where string-pointers must end with a double zero. For example, such is the lpstrFilter member in the OPENFILENAME structure.

Here is the description of lpstrFilter in MSDN:

"LPCTSTR

A buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters."

It follows from this description that we must add one more zero at the end of the string. For example: lpstrFilter = "All Files\0*.*\0";

However, many programmers forget about this additional zero. This is a sample of incorrect code we found in one application:

lofn.lpstrFilter = L"Equalizer Preset (*.feq)\0*.feq";

This code will cause generating rubbish in the filter field in the file dialogue. This is the correct code:

lofn.lpstrFilter = L"Equalizer Preset (*.feq)\0*.feq\0";

We added 0 at the end of the string manually while the compiler will add one more zero. Some programmers write this way to make it clearer:

lofn.lpstrFilter     = L"Equalizer Preset (*.feq)\0*.feq\0\0";

But here we will get three zeroes instead of two. It is unnecessary yet well visible to the programmer.

There are also some other structures besides OPENFILENAME where you might make such mistakes. For instance, the strings lpstrGroupNames and lpstrCardNames in structures OPENCARD_SEARCH_CRITERIA, OPENCARDNAME must end with a double zero too.

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

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


Bugs Found

Checked Projects
367
Collected Errors
13 552