safestr.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 434 行 · 第 1/2 页

C
434
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Non-exhaustive test of the C library safe string functions.
*
****************************************************************************/


#define __STDC_WANT_LIB_EXT1__ 1       // Enable Safer C interfaces

#include <errno.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#ifdef __SW_BW
    #include <wdefwin.h>
#endif

#define VERIFY( exp )   if( !(exp) ) {                                      \
                            printf( "%s: ***FAILURE*** at line %d of %s.\n",\
                                    ProgramName, __LINE__,                  \
                                    strlwr(__FILE__) );                     \
                            NumErrors++;                                    \
                            exit( -1 );                                     \
                        }


char    ProgramName[128];   /* executable filename */
int     NumErrors = 0;      /* number of errors */
int     NumViolations = 0;  /* runtime-constraint violation counter */


/* Runtime-constraint handler for tests; doesn't abort program. */
void my_constraint_handler( const char *msg, void *ptr, errno_t error )
{
    fprintf( stderr, "Runtime-constraint in %s", msg );
    ++NumViolations;
}

/* Test memcpy_s(), memmove_s()                                       */
/* Test strcpy_s(), strcat_s(), strnlen_s(), strncpy_s(), strncat_s() */
/* Test strtok_s()                                                    */

void TestMove_s( void )
{

    char    buf[128];
    char    s2[] = "VALUE";
    char    str[] = "VALUE";

    char    src1[100] = "hello";
    char    src2[7] = {'g', 'o', 'o', 'd', 'b', 'y', 'e'};
    char    dst1[6], dst2[5], dst3[5];

    char    sc1[100] = "good";
    char    sc2[6] = "hello";
    char    sc3[6] = "hello";
    char    sc4[7] = "abc";
    char    sc5[1000] = "bye";

    int     violations = NumViolations;

    /*   strtok_s */
    static char     str1[] = "?a???b,,,#c";
    static char     str2[] = "\t \t";
    static char     str3[] = "?a???b,,,#c";
    char            *t, *ptr1, *ptr2, *ptr3;
    rsize_t         max1 = sizeof( str1 );
    rsize_t         max2 = sizeof( str2 );
    rsize_t         max3 = sizeof( str3 );


    /***********************************************************************/
    /*  set constraint-handler                                             */
    /***********************************************************************/

    set_constraint_handler_s( my_constraint_handler );

    /***********************************************************************/
    /*  memcpy_s                                                           */
    /***********************************************************************/

    /* Test the "good" case */
    VERIFY( memcpy_s( buf, sizeof( buf ), s2, 0 ) == 0 );
    VERIFY( memcpy_s( buf, sizeof( buf ), s2, 1 + strlen( s2 ) ) == 0 );
    VERIFY( memcpy_s( buf, strlen( s2 ) + 2, s2, 1 + strlen( s2 ) ) == 0 );

    VERIFY( strlen( buf ) == strlen( "VALUE" ) );
    VERIFY( strcmp( buf, "VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( memcpy_s( buf, 3, s2, strlen( s2 ) ) != 0 );
    VERIFY( buf[0] == '\0' );
    VERIFY( NumViolations == ++violations );

    VERIFY( memcpy_s( NULL, sizeof( buf ), s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memcpy_s( buf, sizeof( buf ), NULL, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memcpy_s( buf, sizeof( buf ), s2, sizeof( buf ) + 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memcpy_s( buf, sizeof( buf ), buf + 1, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( memcpy_s( buf, sizeof( buf ), s2, ~0 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/
    /*  memmove_s                                                          */
    /***********************************************************************/

    /* Test the "good" cases */
    VERIFY( memmove_s( buf, sizeof( buf ) , s2, 0 ) == 0 );
    VERIFY( memmove_s( buf, sizeof( buf ) , s2, 1 + strlen( s2 ) ) == 0 );

    VERIFY( memmove_s( buf, sizeof( buf ), buf + 1, 1 + strlen( s2 ) ) == 0 );

    VERIFY( memmove_s( buf, 1 + strlen( s2 ), s2, 1 + strlen( s2 ) ) == 0 );

    VERIFY( strlen( buf ) == strlen( "VALUE" ) );
    VERIFY( strcmp( buf, "VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( memmove_s( buf, 3, s2, strlen( s2 ) ) != 0 );
    VERIFY( buf[0] == '\0' );
    VERIFY( NumViolations == ++violations );

    VERIFY( memmove_s( NULL, sizeof( buf ), s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memmove_s( buf, sizeof( buf ), NULL, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memmove_s( buf, sizeof( buf ), s2, sizeof( buf ) + 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( memmove_s( buf, ~0, s2, strlen( s2 ) ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( memmove_s( buf, sizeof( buf ), s2, ~0 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/
    /*  strcpy_s                                                           */
    /***********************************************************************/

    /* Test the "good" cases */
    VERIFY( strcpy_s( buf, sizeof( buf ), s2 ) == 0 );
    VERIFY( strcpy_s( buf, sizeof( buf ), s2 ) == 0 );
    VERIFY( strcpy_s( buf, strlen( s2 ) + 1, s2 ) == 0 );


    VERIFY( strlen( buf ) == strlen( "VALUE" ) );
    VERIFY( strcmp( buf, "VALUE" ) == 0 );
    VERIFY( NumViolations == violations );

    /* Test various failing cases */
    /* Test runtime-constraint violations */
    VERIFY( strcpy_s( buf, 3, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );
    VERIFY( buf[0] == '\0' );

    VERIFY( strcpy_s( NULL, sizeof( buf ), s2 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strcpy_s( buf, sizeof( buf ), NULL ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strcpy_s( buf, 5, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );

    VERIFY( strcpy_s( buf, sizeof( buf ), buf + 1 ) != 0 );
    VERIFY( NumViolations == ++violations );

#if RSIZE_MAX != SIZE_MAX
    VERIFY( strcpy_s( buf, ~0, s2 ) != 0 );
    VERIFY( NumViolations == ++violations );
#endif

    /***********************************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?