Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Visual Studio Code Analysis

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jun 23, 2010 4:26 pm    Post subject: Visual Studio Code Analysis Reply with quote

So this is me yet again with my code OCD or whatever. I discovered Visual Studio's 'code analysis' today and ran it on a project I'm working on right now. BOOM and about 10 warnings came up. After a minute or two of fixing, I got that down to 4. This is the entire output :
Code:
1>------ Rebuild All started: Project: SysAdminTool, Configuration: Release Win32 ------
1>Build started 23/06/2010 22:09:09.
1>_PrepareForClean:
1>  Deleting file "Release\SysAdminTool.lastbuildstate".
1>InitializeBuildStatus:
1>  Creating "Release\SysAdminTool.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  Available.cpp
1>  Logs.cpp
1>  Parser.cpp
1>  PsPasswd.cpp
1>  Results.cpp
1>  Settings.cpp
1>  Setup.cpp
1>  SysAdminTool.cpp
1>  Running Code Analysis for C/C++...
1>c:\users\michael\documents\visual studio 2010\projects\sysadmintool\sysadmintool\sysadmintool.cpp(137): warning C6031: Return value ignored: 'InitializeCriticalSectionAndSpinCount'
1>c:\users\michael\documents\visual studio 2010\projects\sysadmintool\sysadmintool\sysadmintool.cpp(138): warning C6031: Return value ignored: 'InitializeCriticalSectionAndSpinCount'
1>c:\users\michael\documents\visual studio 2010\projects\sysadmintool\sysadmintool\sysadmintool.cpp(139): warning C6031: Return value ignored: 'InitializeCriticalSectionAndSpinCount'
1>c:\users\michael\documents\visual studio 2010\projects\sysadmintool\sysadmintool\parser.cpp(66): warning C6386: Buffer overrun: accessing '*lpArray', the writable size is 'numLines*sizeof(?/* Sorry I don't currently handle AST_TYPESPECIFIER nodes! */?)' bytes, but '8' bytes might be written: Lines: 4, 5, 6, 7, 9, 10, 11, 12, 13, 16, 17, 20, 22, 24, 29, 31, 37, 38, 37, 41, 42, 44, 49, 51, 52, 54,55, 56, 58, 63, 64, 66
1>Link:
1>  Generating code
1>  Finished generating code
1>  SysAdminTool.vcxproj -> C:\Users\michael\Documents\Visual Studio 2010\Projects\SysAdminTool\Release\SysAdminTool.exe
1>RunCodeAnalysis:
1>  Running Code Analysis...
1>  Code Analysis Complete -- 0 error(s), 0 warning(s)
1>FinalizeBuildStatus:
1>  Deleting file "Release\SysAdminTool.unsuccessfulbuild".
1>  Touching "Release\SysAdminTool.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:21.05
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========


So I sort of really really would like to get rid of those. The first 3 are from some code which is creating a critical section. The documentation clearly states that the function always returns a nonzero value. Only on older operating systems did it ever return zero. I have no idea why I'm supposed to read the return value. IMO there is nothing I can gain out of making a variable to take it. So I guess I want a way to suppress those first 3 warnings. Possibly they're reading from an old documentation or something ? Idk. I'm using Visual Studio 2010 with the corresponding distribution.

The second thing is that 4th error.. I don't understand why it's saying that. The only thing I can think of is that it is warning me for the case that I port the project to x64. That line is only :
Code:
    (*lpArray)[numLines] = newHost;


This source file just consists of a basic text parser :
Code:
#include "SysAdminTool.h"

bool ParseFile( LPSTR fileName, bool bFullPath, char*** lpArray, unsigned int* lpNumItems ) {
  HANDLE hFile;
  char   szItem[MAX_PATH] = {0};
  DWORD  dwFileLength, dwBytesRead;
  DWORD  numLines = 1;

  if( !bFullPath ) {
    GetModuleFileName( NULL, szItem, _countof( szItem ) );
    PathRemoveFileSpec( szItem );
    PathAddBackslash( szItem );
    strcat_s( szItem, _countof( szItem ), fileName );
  }

  hFile = CreateFile( szItem, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  if( hFile == NULL )
    return false;

  dwFileLength = GetFileSize( hFile, NULL ); // not supporting files larger than 2^32 bytes

  char* szFileContents = ( char* )malloc( dwFileLength + 1 );

  if( szFileContents == NULL ) {
    MessageBox( 0, "Out of memory..", 0, 0 );
    return false;
  }

  RtlZeroMemory( szFileContents, dwFileLength + 1 );

  if( !ReadFile( hFile, szFileContents, dwFileLength, &dwBytesRead, NULL ) ) {
    free( szFileContents );
    CloseHandle( hFile );
    return false;
  }

  for( unsigned int i = 0; i < dwFileLength; i++ )
    if( szFileContents[i] == '\n' )
      numLines++;

  *lpNumItems = numLines;
  *lpArray    = ( char** )malloc( numLines * sizeof DWORD );

  if( *lpArray == NULL ) {
      MessageBox( 0, "Out of memory..", 0, 0 );
      return false;
  }

  numLines = 0;

  char* context = NULL;
  char* pch     = strtok_s( szFileContents, "\n", &context );

  while( pch != NULL ) {
    int   cbLength  = strlen( pch ) + 1;
    char* newHost   = ( char* )malloc( cbLength );

    if( newHost == NULL ) {
      MessageBox( 0, "Out of memory..", 0, 0 );
      return false;
    }

    RtlZeroMemory( newHost, cbLength );
    strcpy_s( newHost, cbLength, pch );

    (*lpArray)[numLines] = newHost;

    for( int i = 0; i < cbLength; i++ )
      if( (*lpArray)[numLines][i] == '\r' )
        (*lpArray)[numLines][i] = '\0';

    numLines++;
    pch = strtok_s( NULL, "\n", &context );
  }

  free( szFileContents );
  CloseHandle( hFile );
  return true;
}


Would greatly appreciate any help on getting rid of those warnings..
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Wed Jun 23, 2010 6:59 pm    Post subject: Reply with quote

I don't know about the warnings, but just a thing I noticed after a quick look at the code:
Code:
  *lpArray    = ( char** )malloc( numLines * sizeof DWORD );

  if( *lpArray == NULL ) {
      MessageBox( 0, "Out of memory..", 0, 0 );
      return false;
  }

There should be a CloseHandle somewhere there before the return.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jun 23, 2010 7:06 pm    Post subject: Reply with quote

Ah yes, good point. I just added those null pointer checks quickly as a result of the static code analysis too. Changed, thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites