Many people are focusing on buffer overflows, but that’s just one type of bug or exploit. There are plenty of other more sophisticated ones. And eradicating them is a very hard problem.
One reason that bugs are so difficult to eradicate is that you can only fix a bug you know exists, and finding bugs is harder than you think. Testing software systems is not like testing physical systems because the behavior of physical systems is (generally) continuous with a few specific discontinuities, and the behavior of software is discontinuous.
If you have, say, an analog electrical circuit that expects 1V input, you could test it at 0.9V, 1V, and 1.1V of input, and if it works within spec for all of them, you can be fairly confident that it will work at 1.03V, and at 0.97V, and that there will be a relatively smooth response between those.
If you have a building model, and you test it with 10mph winds and 80mph winds and it behaves as expected, you can be reasonably sure that it’s also going to be safe at 11mph and 43mph winds.
Those aren’t hard and fast rules. You can certainly design very complicated electronic circuits that don’t respond (we call them computers!), and material things can have surprising behavior in some circumstances. But most systems are not chaotic in the chaos theory sense of the word, where a minute change in inputs results in a dramatic difference in output.
That all goes out the window with software.
If you have a software system and you test it with inputs 0x0001 and 0xF000, you have determined only what it does with those inputs. You have zero information about what it might do if you give it 0x0002, or 0xF3A2. Now, for incredibly simple systems, you can simply test all possible inputs. But for any system complicated enough to be useful in the real world, you can’t do that.
There are all sorts of sophisticated methods of testing software to try to get around this. I don’t want to imply that it’s not possible to test software. But it’s never going to be easy as testing something that must comport itself according to the laws of physics, because its range is the entirety of computable mathematics.