Here is a simplified version, compile with speed optimizations:
#include <stdio.h>
#include <string.h>
//#pragma function( strlen )
int main( void )
{
char testfile[512] = { 0 };
strcpy( testfile, "xy" );
printf("%d, should be 2\n" , strlen(testfile) ) ;
strcpy(testfile, "this is a test");
printf("%d, should be 14\n" , strlen(testfile) ) ;
return 0 ;
}
Only happens with speed optimizations, size optimization is not affected.
Removing the first call to strlen removes the bug.
Looking in assembly, the old value of strlen stored in a register is used, instead checking the length again.
After trying the intrinsic options, I found the culprit to be strlen. If you enable speed optimizations and disable intrinsic for strlen you remove the bug.