Secure Coding mailing list archives

Re: New Microsoft Security Tool for developers


From: Steve Litt <slitt () troubleshooters com>
Date: Sat, 13 Dec 2003 04:10:22 +0000

Oh Oh, what about this:

if(noOverflow("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) {
  printf("No overflow unless the 10th char of the string was \\0\n");
}

SteveT


bool noOverflow(char *str)
{
     char buffer[10];

     if( str == NULL )
     {
         /* We should never have a NULL string */
          assert( false );
          return false;
     }

     /* Lets prep our buffer to check for an overflow. Lets nullify the end
char first */
     buffer[sizeof(buffer)-1] = '\0';

    /* Lets copy the string in, max of buffer size */
    strncpy( buffer, str, sizeof(buffer) );

    /* Now lets check if the null at the end of the buffer has been
trampled */
    if( buffer[sizeof(buffer)-1] != '\0' )
    {
          /* We know of an unsafe string. This has overflowed! */
          return false;
     }

     /* Avoiding buffer flow with the above two lines */
     return true;
}









Current thread: