Buffer Underwrite ('Buffer Underflow')

The software writes to a buffer using an index or pointer that references a memory location prior to the beginning of the buffer.


This typically occurs when a pointer or its index is decremented to a position before the buffer, when pointer arithmetic results in a position before the beginning of the valid memory location, or when a negative index is used.


The following examples help to illustrate the nature of this weakness and describe methods or techniques which can be used to mitigate the risk.

Note that the examples here are by no means exhaustive and any given weakness may have many subtle varieties, each of which may require different detection methods or runtime controls.

Example One

In the following C/C++ example, a utility function is used to trim trailing whitespace from a character string. The function copies the input string to a local character string and uses a while statement to remove the trailing whitespace by moving backward through the string and overwriting whitespace with a NUL character.

char* trimTrailingWhitespace(char *strMessage, int length) {

  char *retMessage;
  char *message = malloc(sizeof(char)*(length+1));

  // copy input string to a temporary string
  char message[length+1];
  int index;
  for (index = 0; index < length; index++) {
    message[index] = strMessage[index];
  message[index] = '\0';

  // trim trailing whitespace
  int len = index-1;
  while (isspace(message[len])) {
    message[len] = '\0';

  // return string without trailing whitespace
  retMessage = message;
  return retMessage;


However, this function can cause a buffer underwrite if the input character string contains all whitespace. On some systems the while statement will move backwards past the beginning of a character string and will call the isspace() function on an address outside of the bounds of the local buffer.

Example Two

The following is an example of code that may result in a buffer underwrite, if find() returns a negative value to indicate that ch is not found in srcBuf:

int main() {
  strncpy(destBuf, &srcBuf[find(srcBuf, ch)], 1024);

If the index to srcBuf is somehow under user control, this is an arbitrary write-what-where condition.

See Also

Memory Buffer Errors

Weaknesses in this category are related to the handling of memory buffers within a software system.

SFP Secondary Cluster: Faulty Buffer Access

This category identifies Software Fault Patterns (SFPs) within the Faulty Buffer Access cluster (SFP8).

Comprehensive CWE Dictionary

This view (slice) covers all the elements in CWE.

Weaknesses Introduced During Implementation

This view (slice) lists weaknesses that can be introduced during implementation.

Weaknesses Introduced During Design

This view (slice) lists weaknesses that can be introduced during design.

Common Weakness Enumeration content on this website is copyright of The MITRE Corporation unless otherwise specified. Use of the Common Weakness Enumeration and the associated references on this website are subject to the Terms of Use as specified by The MITRE Corporation.