Buffer Overflow Causes. ©2002, Jedidiah R. Crandall, Susan L. Gerhart, Jan G. Hogle.  http://sfsecurity.pr.erau.edu
What can cause buffer overflows?
Careless use of buffers without bounds checking.
Formatting and logical errors.
Unsafe library function calls.
Off-by-one errors.
Old code used for new purposes (like UNICODE international characters).
All sorts of other far-fetched but deadly-serious things you should think about.