Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Implementation of strncat_s() - bounds-checking strncat().
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "saferlib.h"
  34. #include "widechar.h"
  35. #include <string.h>
  36. #include <wchar.h>
  37.  
  38.  
  39. _WCRTLINK errno_t __F_NAME(strncat_s,wcsncat_s)( CHAR_TYPE * __restrict s1,
  40.                             rsize_t s1max, const CHAR_TYPE * __restrict s2,
  41.                             rsize_t n )
  42. /*************************************************************************/
  43. {
  44.     errno_t     rc = -1;
  45.     const char  *msg;
  46.  
  47.     // strnlen_s is safe to use as it has no rt constraints
  48.     rsize_t     m = s1max - __F_NAME(strnlen_s,wcsnlen_s)( s1, s1max );
  49.  
  50.  
  51.     // Verify runtime-constraints
  52.     // s1 not NULL
  53.     // s2 not NULL
  54.     // s1max <= RSIZE_MAX
  55.     // n     <= RSIZE_MAX
  56.     // s1max != 0
  57.     // m = s1max - strnlen_s( s1, s1max )  != 0
  58.     // if n >= m then m > strnlen_s( s2, m )
  59.     // s1 and s2 no overlap
  60.     if( __check_constraint_nullptr_msg( msg, s1 ) &&
  61.         __check_constraint_nullptr_msg( msg, s2 ) &&
  62.         __check_constraint_maxsize_msg( msg, s1max ) &&
  63.         __check_constraint_maxsize_msg( msg, m ) &&
  64.         __check_constraint_zero_msg( msg, s1max ) &&
  65.         __check_constraint_zero_msg( msg, m ) &&
  66.         ((n < m) || __check_constraint_a_gt_b_msg( msg, __F_NAME(strnlen_s,wcsnlen_s)( s2, m ), m - 1 )) &&
  67.         __check_constraint_overlap_msg( msg, s1, s1max, s2, __F_NAME(strnlen_s,wcsnlen_s)( s2, s1max ))) {
  68.  
  69.          while( *s1 != NULLCHAR ) ++s1;            //find end of field
  70.  
  71.          while( n != 0 ) {
  72.               *s1 = *s2;
  73.               if( *s2 == NULLCHAR ) break;
  74.               ++s1;
  75.               ++s2;
  76.               --n;
  77.          }
  78.          *s1 = NULLCHAR;
  79.  
  80.          rc = 0;
  81.     } else {
  82.         // Runtime-constraints found, store zero in receiving field
  83.         if( (s1 != NULL) && (s1max > 0) && __lte_rsizmax( s1max ) ) {
  84.             s1[0] = NULLCHAR;
  85.         }
  86.         // Now call the handler
  87.         __rtct_fail( __func__, msg, NULL );
  88.     }
  89.     return( rc );
  90. }
  91.