That's why so much emphasis is now placed on trying to make incorrect software either fail to build or fail at runtime in a way that doesn't let people take over the computer. C makes it extremely likely that a random correctness issue will be a security issue. Newer languages much less so. They're far from perfect but it's a lot better.
Also sandboxing and strong permission boundaries ie when things fail, contain the damage