Examples of errors detected by the V507 diagnostic.


V507. Pointer to local array 'X' is stored outside the scope of this array. Such a pointer will become invalid.


TortoiseSVN

V507 Pointer to local array 'stringbuf' is stored outside the scope of this array. Such a pointer will become invalid. mainwindow.cpp 277


LRESULT CALLBACK CMainWindow::WinMsgHandler(....)
{
  ...
  if (pNMHDR->code == TTN_GETDISPINFO)
  {
    LPTOOLTIPTEXT lpttt;

    lpttt = (LPTOOLTIPTEXT) lParam;
    lpttt->hinst = hResource;

    // Specify the resource identifier of the
    // descriptive text for the given button.
    TCHAR stringbuf[MAX_PATH] = {0};
    ...
    lpttt->lpszText = stringbuf;
  }
  ...
}

It's not that simple with this code. Theoretically, the code contains an error. Practically, it works well. The V507 diagnostic message warns you that an object is used after being destroyed. The 'stringbuf' buffer will be used after exiting the body of the 'if' operator. If 'stringbuf' was an object of the class std::string, for instance, its behavior would be incorrect. We would use an already destroyed object in that case. But here 'stringbuf' is an array created in the stack. The Visual C++ compiler doesn't use this stack area again, so the buffer will exist until the 'CMainWindow::WinMsgHandler' function finishes its work. Thus, no error occurs, although the code is potentially dangerous.

Similar errors can be found in some other places:

  • V507 Pointer to local array 'stringbuf' is stored outside the scope of this array. Such a pointer will become invalid. picwindow.cpp 443

Pixie

V507 Pointer to local array 'var' is stored outside the scope of this array. Such a pointer will become invalid. ri renderer.cpp 5974


class CVariable {
public:
  char  name[64];  // Name as it is referenced
  ...
};

void CRendererContext::RiGeometryV(
  char *type,int n,char *tokens[],void *params[])
{
  ...
  } else {
    CVariable var;
    if (parseVariable(&var,NULL,tokens[i])) {
      tokens[i] = var.name;
      i--;
    } else {
      error(CODE_BADTOKEN,
            "Unrecognised implicit parameter: %s\n",tokens[i]);
    }
  }
  ...
}

Similar errors can be found in some other places:

  • V507 Pointer to local array 'var' is stored outside the scope of this array. Such a pointer will become invalid. ri renderer.cpp 6010

MySQL

V507 Pointer to local array 'buf' is stored outside the scope of this array. Such a pointer will become invalid. myisam ha_myisam.cc 1156


int ha_myisam::assign_to_keycache(THD* thd,
                                  HA_CHECK_OPT *check_opt)
{
  ...
  const char *errmsg= 0;
  ...
  if ((error= mi_assign_to_key_cache(file, map, new_key_cache)))
  {
    char buf[STRING_BUFFER_USUAL_SIZE];
    my_snprintf(buf, sizeof(buf),
      "Failed to flush to index file (errno: %d)", error);
    errmsg = buf;
    error = HA_ADMIN_CORRUPT;
  }
  ...
}

It's not that simple with this code. Theoretically, the code contains an error. Practically, it works well. The V507 diagnostic message warns you that an object is used after being destroyed. The 'buf' buffer will be used after exiting the body of the 'if' operator. If 'buf' was an object of the class std::string, for instance, its behavior would be incorrect. We would use an already destroyed object in that case. But here 'buf' is an array created in the stack. The Visual C++ compiler doesn't use this stack area again, so the buffer will exist until the 'ha_myisam::assign_to_keycache' function finishes its work. Thus, no error occurs, although the code is potentially dangerous.


Source Engine SDK

V507 Pointer to local array 'szActName' is stored outside the scope of this array. Such a pointer will become invalid. Server (HL2) baseflex.cpp 1326


static Activity DetermineExpressionMoveActivity(
  CChoreoEvent *event, CAI_BaseNPC *pNPC )
{
  ....
  const char *pszAct = Q_strstr( sParam2, " " );
  if ( pszAct )
  {
    char szActName[256];
    Q_strncpy( szActName, sParam2, sizeof(szActName) );
    szActName[ (pszAct-sParam2) ] = '\0';
    pszAct = szActName;
  }
  ....
}

Scilab

V507 Pointer to local array 'strfl' is stored outside the scope of this array. Such a pointer will become invalid. sci_champ.c 103


int sci_champ_G(....)
{
  ....
  char * strf = NULL ;
  ....
  if ( isDefStrf( strf ) )
  {
    char strfl[4];
    strcpy(strfl,DEFSTRFN);
    strf = strfl;
    if ( !isDefRect( rect ) )
    {
      strf[1]='5';
    }
  }

  (*func)(stk(l1 ),stk(l2 ),stk(l3 ),stk(l4 ),
    &m3,&n3,strf,rect, arfact, 4L);
  ....
}

Similar errors can be found in some other places:

  • V507 Pointer to local array 'strfl' is stored outside the scope of this array. Such a pointer will become invalid. sci_fec.c 111
  • V507 Pointer to local array 'strfl' is stored outside the scope of this array. Such a pointer will become invalid. sci_grayplot.c 94
  • V507 Pointer to local array 'strfl' is stored outside the scope of this array. Such a pointer will become invalid. sci_matplot.c 84

Miranda NG

V507 Pointer to local array 'str' is stored outside the scope of this array. Such a pointer will become invalid. Miranda genmenu.cpp 973


HMENU BuildRecursiveMenu(....)
{
  ....
  if (GetKeyState(VK_CONTROL) & 0x8000) {
    TCHAR str[256];

    mir_sntprintf(str, SIZEOF(str),
      _T("%s (%d, id %x)"), mi->pszName,
      mi->position, mii.dwItemData);

    mii.dwTypeData = str;
  }
  ....
}

Similar errors can be found in some other places:

  • V507 Pointer to local array 'str' is stored outside the scope of this array. Such a pointer will become invalid. Miranda genmenu.cpp 988
  • V507 Pointer to local array 'szFiles' is stored outside the scope of this array. Such a pointer will become invalid. ICQ icq_proto.cpp 1323
  • V507 Pointer to local array 'jidreason' is stored outside the scope of this array. Such a pointer will become invalid. Jabber jabber_iqid_muc.cpp 121
  • And 4 additional diagnostic messages.

MAME

V507 Pointer to local array 'label' is stored outside the scope of this array. Such a pointer will become invalid. usrintrf.c 2121

V507 Pointer to local array 'setting' is stored outside the scope of this array. Such a pointer will become invalid. usrintrf.c 2122


static int settraksettings(struct mame_bitmap *bitmap,
                           int selected)
{
  const char *menu_item[40];
  const char *menu_subitem[40];
  ....
  for (i = 0;i < total2;i++)
  {
    if (i < total2 - 1)
    {
      char label[30][40];
      char setting[30][40];
      ....
      menu_item[i] = label[i];
      menu_subitem[i] = setting[i];
    }
  }
  ....
}

GNU Octave

V507 Pointer to local array 'dirbuf' is stored outside the scope of this array. Such a pointer will become invalid. tmpdir.c 128


int path_search(const char *dir, ....)
{
  ....
  if (....)
  {
    char dirbuf[PATH_MAX];
    ....
    dir = dirbuf;
  }
  ....
  dlen = strlen (dir);
}

Inkscape

V507 Pointer to local array 'n' is stored outside the scope of this array. Such a pointer will become invalid. inkscape.cpp 582


void
Application::crash_handler(int /*signum*/)
{
  ....
  if (doc->isModifiedSinceSave()) {
    const gchar *docname;
  ....
  if (docname) {
    ....
    if (*d=='.' && d>docname && dots==2) {
      char n[64];
      size_t len = MIN (d - docname, 63);
      memcpy (n, docname, len);
      n[len] = '\0';
      docname = n;
    }
  }
  if (!docname || !*docname) docname = "emergency";
  ....
}

Similar errors can be found in some other places:

  • V507 Pointer to local array 'in_buffer' is stored outside the scope of this array. Such a pointer will become invalid. inkjar.cpp 371
  • V507 Pointer to local array 'out_buffer' is stored outside the scope of this array. Such a pointer will become invalid. inkjar.cpp 375

GCC

V507 Pointer to local array 'buf' is stored outside the scope of this array. Such a pointer will become invalid. hsa-dump.c 704


static void
dump_hsa_symbol (FILE *f, hsa_symbol *symbol)
{
  const char *name;
  if (symbol->m_name)
    name = symbol->m_name;
  else
  {
    char buf[64];
    sprintf (buf, "__%s_%i", hsa_seg_name (symbol->m_segment),
       symbol->m_name_number);
     name = buf;
  }

  fprintf (f, "align(%u) %s_%s %s",
           hsa_byte_alignment (symbol->m_align),
           hsa_seg_name(symbol->m_segment),
           hsa_type_name(symbol->m_type & ~BRIG_TYPE_ARRAY_MASK),
           name);
  ....
}

Tizen

V507 Pointer to local array 'buffer' is stored outside the scope of this array. Such a pointer will become invalid. media_codec_test.c 793


void extract_input_aacdec_m4a_test(
  App * app, unsigned char **data, int *size, bool * have_frame)
{
  ....
  unsigned char buffer[100000];
  ....
DONE:
  *data = buffer;
  *have_frame = TRUE;
  if (read_size >= offset)
    *size = offset;
  else
    *size = read_size;
}


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++, and C#

goto PVS-Studio;