[comp.sys.amiga] Lattice "enhancement"

joemu@nscpdc.UUCP (02/04/87)

Here's a little gotcha in the Lattice library. Strncpy always NUL terminates
the destination string and the count argument does not include the NUL
terminator. You can demonstrate the problem via:
	char foo[6];	/* larger than the strncpy count or you may guru */
	strncpy(foo,"123456789", 5);
	printf("%s\n", foo);

Under most systems, you'll get an unterminated string of the chars 12345 in
foo and foo[5] is unchanged. Under Lattice you get 12345\0. Notice that foo[5]
has been modified. I typically have something like this in my (non-lattice) C
code:
	strncpy(foo, bar, sizeof(foo));	/* make sure I don't overflow foo */
	if (strlen(bar) >= sizeof(foo))
		foo[sizeof(foo) - 1] = '\0';	/* NUL terminate foo */

If your code is flaky, check for calls to strncpy! I could forgive them if the
count included the NUL because it would save me from having to check and rewrite
the last character to NUL, but it's current form is dangerous!

						Joe Mueller
						..!nsc!nscpdc!joemu