Buffer Overflow Causes. ©2002, Jedidiah R. Crandall, Susan L. Gerhart, Jan G. Hogle.  http://sfsecurity.pr.erau.edu
Off-by-one errors - Recommendations.
•If you have a 512 byte buffer you can only store 511 characters in the string (the last character is a NULL).
•If you use scanf() to read into a buffer you also have to account for the NULL: use scanf(“%511s”, &My512ByteBuffer) instead of scanf(“%512s”, &My512ByteBuffer) which is unsafe.
•If you declare an array as int A[100], remember that you can’t access A[100], the highest index you can access is A[99] and the lowest is A[0].
•The best defense against off-by-one errors of any kind is a thorough combination of testing and code inspection.