[mw-devel] CVS Update at silver.sucs.org: mw

Justin Mitchell arthur at sucs.org
Sat Jul 27 09:22:19 BST 2002


>Realised that 'snprintf' and 'vsnprintf' dont terminate a string with a \0
>if it reaches the end of the buffer. Had to make quite a few corrections to
>fix this, but scripts seem quite a bit more stable now!

Your wrong. I just tested snprintf to be absolutely sure.

If you you said your string has 20 characters, snprintf will make
sure that there is at most 19 chars of data (truncating it)
so that the 20th char can be the \0

Your problem is off by one errors, not using snprintf wrongly.
You can now have fun rolling that change back out again and
fixing it properly this time.

main()
{
	unsigned char text[10];
	int i;

	/* fill with letters */
	for(i=0;i<10;i++) text[i]='A'+i;

	/* insert overly long data */
	snprintf(text, 6, "%d", 45671234 );

	for(i=0;i<10;i++) 
		printf("%d:'%c' ", text[i], text[i]);
	printf("\n");
}	

This demonstrates that text[5] == '\0' even though the data
being printed was supposed to be much larger.




More information about the mw-devel mailing list