Access Violation

12.10.2011

You may see messages containing "access violation" words when segmentation faults occur.

A segmentation fault (segfault in abbreviated form) is a software error occurring when a program tries to access memory addresses unavailable for writing or when a program tries to modify memory using an illegal method.

Segmentation is one of the approaches to memory management and protection in an operating system. In most systems it has been replaced by paged memory, but documentations traditionally use the term "Segmentation fault".

In UNIX-like operating systems, a process accessing invalid memory addresses receives a SIGSEGV signal. In Microsoft Windows, a process accessing invalid memory addresses raises an exception STATUS_ACCESS_VIOLATION and usually launches the Dr. Watson program which shows the user a window prompting to send the error report to Microsoft.

Memory access violation is most often caused by such errors in programs as array overruns or usage of a null pointer.

Let's examine a defect in a C++ program that can cause this type of errors. This error was found by our analyzer PVS-Studio in the Chromium project.

bool ChromeFrameNPAPI::Invoke(...)
{
  ChromeFrameNPAPI* plugin_instance =
    ChromeFrameInstanceFromNPObject(header);
  if (!plugin_instance &&
      (plugin_instance->automation_client_.get()))
    return false;
  ...  
}

This code should check the value of the 'plugin_instance' pointer and call the function if the pointer is not equal to zero. The error here is that the priority of the operator '!' is higher than that of the '&&' operator. As a result, the code behaves in an unexpected way. Arranging parentheses clarifies the point:

if ( (!plugin_instance) && 
     (plugin_instance->automation_client_.get()))
  return false;

It turns out that we will use a null pointer. Handling a null pointer will cause a segmentation fault and an exception will be thrown.

References