[previous] 2847 [next] Apparent: Maximum number of characters to be written is larger than the target buffer size.
Arrays

An apparent anomaly has been detected.

Certain standard library functions specify a maximum number (M) of characters to be written to a char array (A).

function A parameter numberM parameter number
asctime_s 12
ctime_s 12
snprintf 12
snprintf_s 12
strcpy_s 12
strncat 13
strncat_s 12
strncat_s 14
strncat_s 34
strncpy 13
strncpy_s 14

This message is issued whenever a non constant expression is used for M and its value is sometimes greater than the size of object A. This may result in an access outside the bounds of A.

For example, in the code shown below, the presence of the if statement suggests that m is likely to be greater than sizeof (a). Of course, if the value of m passed to function foo is never greater than 10, the upper bound of a will not be exceeded; but this would imply that the "if" statement is redundant and any code contained in the "if" block is also redundant.


/*PRQA S 1-9999 ++*/
/*PRQA S 2847 --*/

#include <string.h>

extern char a[10];
extern char b[11];

void foo (int m)
{
  if (m > sizeof (a))
  {
  }
  
  strncpy (a, b, m);   /* 2847 */
}


MISRA-C:2004 Rules applicable to message 2847:

Rule  21.1  (Required) Minimisation of run-time failures shall be ensured by the use of at least one of (a) static analysis tools/techniques; (b) dynamic analysis tools/techniques; (c) explicit coding of checks to handle run-time faults


See also:

QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents