Επεξεργασία

Κοινή χρήση μέσω


Warning C26863

Return value from a date-time handling function func is ignored

This rule was added in Visual Studio 2022 17.8.

Remarks

It's important to verify the return value of a function that transforms a date structure when the year, month, or date input argument was manipulated without proper leap year handling. Otherwise, the function may have failed and execution continues with an output parameter containing invalid data.

The following is a list of the functions that this warning covers:

Code analysis name: DATETIME_MANIPULATION_FUNCTION_RETURN_IGNORED

Example

The following code tries to get current system time, advance the month field by one month, and get the file time that corresponds to the updated system time via SystemTimeToFileTime. However, SystemTimeToFileTime might fail, as the updated system time may become invalid:

#include <Windows.h> 
 
void foo() 
{ 
    FILETIME ft; 
    SYSTEMTIME st; 
    GetSystemTime(&st); 
    st.wMonth++; // Advance month by one 
    // Get the file time 
    SystemTimeToFileTime(&st, &ft);    // C26863 
}

To fix the problem, always check the return value from date-time manipulation functions and handle failures appropriately:

#include <Windows.h> 
  
void foo() 
{ 
    FILETIME ft; 
    SYSTEMTIME st; 
    GetSystemTime(&st); 
    
    st.wMonth++; // Advance month by one 
    // Get file time 
    if (SystemTimeToFileTime(&st, &ft)) 
    { 
        // Use file time 
    } 
}

Heuristics

This rule only recognizes the Windows SYSTEMTIME struct and the C tm struct.

This rule is enforced regardless of whether the input arguments were validated before calling these functions. If all the input arguments are validated before calling the function, this rule can report false warning.

This rule is an opt-in rule, meaning that code analysis should use a ruleset file, and the rule should be explicitly included in the ruleset file, and enabled for it to be applied. For more information on creating a custom ruleset for code analysis, see Use Rule Sets to Specify the C++ Rules to Run.

See also

C6393
C6394
C26861
C26862
C26864