Yes, it strncpy()can come back NULL. Just a mmaprecordable page with the address zero in the system, which defines it NULLas a pointer with zero as its numerical value:
#include <sys/mman.h>
#include <stdio.h>
#include <strings.h>
int main( int argc, char **argv )
{
char *p;
char *buf = mmap( ( void * ) 0, 4096,
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0 );
p = strncpy( buf, "string", strlen( "string" ) );
printf( "p: %p\n", p );
printf( "NULL: %p\n", NULL );
return( 0 );
}
Yes, this intentionally violates the standard C requirement, which NULLshould not point to a valid object, therefore this behavior is undefined.
But this is CAN .
source
share