@@ -1667,3 +1667,49 @@ void strcpy_no_overflow_2(char *y) {
1667
1667
strcpy (x , "12\0" );
1668
1668
}
1669
1669
#endif
1670
+
1671
+ #ifndef SUPPRESS_OUT_OF_BOUND
1672
+ void testStrcpyDestinationWritableFirstByte (void ) {
1673
+ char dst [10 ];
1674
+ char * p = dst - 8 ;
1675
+ strcpy (p , "src" ); // expected-warning {{String copy function overflows the destination buffer}}
1676
+ }
1677
+
1678
+ void CWE124_Buffer_Underwrite__malloc_char_cpy () {
1679
+ char * dataBuffer = (char * )malloc (100 * sizeof (char ));
1680
+ if (dataBuffer == NULL ) return ;
1681
+ memset (dataBuffer , 'A' , 100 - 1 );
1682
+ dataBuffer [100 - 1 ] = '\0' ;
1683
+ char * data = dataBuffer - 8 ;
1684
+ char source [100 ];
1685
+ memset (source , 'C' , 100 - 1 ); // fill with 'C's
1686
+ source [100 - 1 ] = '\0' ; // null terminate
1687
+ strcpy (data , source ); // expected-warning {{String copy function overflows the destination buffer}}
1688
+ free (dataBuffer );
1689
+ }
1690
+ #endif
1691
+
1692
+ #ifndef SUPPRESS_OUT_OF_BOUND
1693
+ void testStrncpyDestinationWritableFirstByte (void ) {
1694
+ char source [100 ];
1695
+ use_string (source ); // escape
1696
+ char buf [100 ];
1697
+ char * p = buf - 8 ;
1698
+ strncpy (p , source , 100 - 1 ); // expected-warning {{String copy function overflows the destination buffer}}
1699
+ }
1700
+
1701
+ void CWE124_Buffer_Underwrite__malloc_char_ncpy () {
1702
+ char * dataBuffer = (char * )malloc (100 * sizeof (char ));
1703
+ if (dataBuffer == 0 ) return ;
1704
+ memset (dataBuffer , 'A' , 100 - 1 );
1705
+ dataBuffer [100 - 1 ] = '\0' ;
1706
+ char * data = dataBuffer - 8 ;
1707
+
1708
+ char source [100 ];
1709
+ memset (source , 'C' , 100 - 1 ); // fill with 'C's
1710
+ source [100 - 1 ] = '\0' ; // null terminate
1711
+ strncpy (data , source , 100 - 1 ); // expected-warning {{String copy function overflows the destination buffer}}
1712
+ data [100 - 1 ] = '\0' ; // null terminate
1713
+ free (dataBuffer );
1714
+ }
1715
+ #endif
0 commit comments