Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Non-Compliant Coding Example

In this non-compliant example a the insert() function is used to add values to a buffer in a modulo fashion, that is inserting values into the beginning of the buffer once the end is reached. However, the variable used to index the array, index, is not guaranteed to be positive. If index is negative then the result of (index+1)%size will also be negative. This causes a new value to be inserted in to a negative offset within list.

Code Block
bgColor#FFCCCC

i % j
int insert(int index, int *list, int size, int value) { 
  index = ( index + 1) % size;
  list[index] = value;
  return index;
}

Implementation Details

Microsoft Visual Studio

...

Wiki Markup
\[[Beebe 05|AA. C References#Beebe 05]\] Nelson H. F. Beebe [Re: Remainder ( % ) operator and GCC|http://gcc.gnu.org/ml/gcc-help/2005-11/msg00141.html] 2005.
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.5.5, "Multiplicative operators"
\[[Microsoft 07|AA. C References#Microsoft 07]\]  [C Multiplicative Operators|http://msdn2.microsoft.com/en-us/library/efa0csed(VS.80).aspx]
\[[Sun 05|AA. C References#Sun 05]\]  C User's Guide Sun Studio 11 819-3688-10 [http://docs.sun.com/source/819-3688/]. 2005. [Appendix E, "Implementation-Defined ISO/IEC C90 Behavior"|http://docs.sun.com/source/819-3688/c90.implementation.app.html]