Can someone tell me what I'm doing wrong?

Kenichi Okuyama okuyamak at
Wed May 23 05:13:09 GMT 2001

>>>>> "CRH" == Christopher R Hertel <crh at> writes:
CRH> The strncpy() copy function is *dangerous* because it does not guarantee
CRH> that the target string will be NUL-terminated.  You should always do this: 
CRH>   strncpy( target, source, strlen(source) + 1 );

1) You mean :

target[GETTING_SIZEOF_ TARGET_SOMEHOW( target ) - 1]	= '\0';
strncpy( target, source, GETTING_SIZEOF_ TARGET_SOMEHOW( target ) - 1 );

is the safer way.
# simply getting 'strlen(source)+1' means it's same as strcpy().

Rather, dangerous point was that workgroup and other pointers were
being passed without size definition.

2) if you know that source is string literal, you can use

CRH>   strncpy( target, SOURCE, sizeof(SOURCE) );

instead. They'll give you the same result.

Kenichi Okuyama

More information about the samba-technical mailing list