r/C_Programming • u/heavymetalmixer • Jul 06 '26
Question How can I protect library code against bugs?
Thanks to many people in this subreddit (on a previous post I made) I learned different ways to handle errors, and along the way I also learned that bugs aren't errors. Yeah, that sounds really basic but I'm self-taught so it's difficult to know details like that one on my own.
Now, with this difference now clear, and the fact that assserts are really good, when it comes to writing library code (not application code) how do y'all protect your libraries' code against bugs besides just putting a lot of asserts on the debug builds?
26
u/bothunter Jul 06 '26
Same way you avoid bugs in any other code -- you write automated tests and run them regularly.
4
u/Remarkable_Bike_1148 Jul 07 '26 edited Jul 07 '26
A lot of folks mentioned automated tests, it's a good start, but there are additional available tools that you can leverage in your project to help you write bug-free code.
Frama-C can be a tool to assist you in verifying whether your function is absent of any bugs. You'll need to write your own annotation for that function, this is not for the faint of heart and require A LOT of time invested to learn the rope on how to use it.
MISRA-C is basically the most extreme end of the spectrum for defensive programming in C language, but you could pick them up as a PDF for about 15 pounds each. I recommends checking it out when you're able and maybe adopt some of it's lessons into your own coding practices.
3
u/f0xw01f Jul 07 '26
Write a test function for every library function. Ensure that the tests exercise every possible code path.
For each source module, have a function that calls all the relevant test functions.
Then for the library as a whole, have a function that calls each module's test function.
3
u/TheOtherBorgCube Jul 07 '26
In addition to writing tests to exercise your code, don't forget about test coverage.
https://en.wikipedia.org/wiki/Gcov
Having 100's of tests doesn't mean much if it only hits 50% of the code. In an ideal world, you hit every line of code and every branch. More pragmatically, you might want to aim at say 95% of the functional (ie, non error path) code.
So for code never executed (marked with
#####), you might be asking yourself "How do I get here?" or "Do I even need this?".Also, each time you get a new bug report, the first thing you do is update your tests to find the bug. Then you'll know when you've fixed it, and make sure it doesn't come back.
3
u/mykesx Jul 07 '26
Don't make bugs. Seriously though, the code I write is effectively 99% bug free. The 1% that isn't bug free has to be fixed.
Unit testing isn't going to find that 1%, which are likely found in the wild. What the tests will do is assure a reported bug has been fixed and then later hasn't regressed. Nobody can predict every path through the code when writing tests, especially with unexpected data or values are encountered.
5
u/Kindly-Department206 Jul 06 '26
The only strategy is exhaustive testing.
2
u/didntplaymysummercar Jul 07 '26
Unit tests, including with address sanitizer, mutation tests if you're ambitious, static analyzers (GCC and visual studio both have basic ones but especially GCC one had some false positives), hard to misuse APIs, error checking, etc.
2
u/JustBoredYo Jul 07 '26
You need to harden your code by checking values for validity, especially for functions that are going to be exposed to the user who may give you absolute garbage.
Also a good way to write reliable code, be it a library or an application, is to never trigger any assert. Asserts should only ever be triggered during development and never when deployed.
This is because a triggered assert means you didn't check your values and develop a proper exit strategy in the case of them being invalid, which may lead to unexpected behavior on the user side. This unexpected behavior may then lead to other bugs in the user eritten code, meaning your code createe bugs by proxy.
I prefer to wrap logging calls for debugging and asserts in an
#ifdef DEBUGwhich makes it easy to switch between debug and production code after I'm done.2
u/pjl1967 Jul 07 '26
In addition to automated tests that others have mentioned, those are best paired with code coverage testing to see if your test suite actually tests all of your code.
This shows how to do code coverage using Autotools; I personally have no idea of how to do it using CMake, but it's certainly possible.
2
u/Key_River7180 Jul 07 '26
Simple: I don't. When writing a library, I just use it in a simple program and see if it catastrophically fails or not.
1
u/pjl1967 Jul 07 '26
assertis universally good. The type of code is irrelevant.2
u/OtherOtherDave Jul 08 '26
I write test suites to test the functionality and make sure it behaves as expected.
0
u/[deleted] Jul 06 '26
[deleted] — view deleted comment
3
u/[deleted] Jul 06 '26
[removed] — view removed comment