C6204
This page is specific to:.NET Framework Version:2.03.54.0
Visual Studio Team System
C6204

warning C6204: possible buffer overrun in call to <function>: use of unchecked parameter <variable>

This warning indicates that a function call is being made that could potentially lead to an overrun of a stack buffer, depending on the possible values of parameters to the function being analyzed. This defect might cause an exploitable buffer overrun or crash.

It is a good idea to review the code, as well as the callers to this function, to see whether the function can ever be called with unexpected data. If it is not clear that all calls are safe, it might be appropriate to validate the input to the function by checking the length of any input strings or by annotating the function parameter using appropriate properties.

Example

The following code generates this warning because the input parameter (pCh) might contain invalid data:

#include<string.h>

void f(char *pCh)
{
  char buff[10];
  strcpy(buff, pCh);
}

This warning can be corrected by validating the size as shown in the following code:

#include<string.h>

void f(char *pCh)
{ 
  char buff[10];
  if (strlen(pCh) >= sizeof buff)
    return;
  strcpy (buff, pCh);
}

The preceding code might fail if a bad pointer (pCh) is passed. To make the preceding code more resilient, use annotation and safe string manipulation function as shown in the following code:

#include<string.h>
#include <codeanalysis\sourceannotations.h>
void f([Pre(NullTerminated=SA_Yes, Null=SA_No)] char* pCh)
{
  char buff[15];
  if (strlen(pCh) > sizeof buff)
    return;
  strcpy_s(buff,sizeof(buff), pCh);
}

Because the analysis tool does not consider the set of all possible callers to the function being analyzed, it is possible that the code is completely safe.

See Also

Concepts

Reference

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View