It is hard to maintain complex C code free of bugs. Numerous tools have been developed to help, and we have thrown many of them at the PostgreSQL source code.
(originally presented at PGCon 2014, http://www.pgcon.org/2014/schedule/events/703.en.html)