r/cprogramming • u/Interesting_Buy_3969 • Oct 07 '26
Using -fsanitize=address feels illegal.
Just saying. The -fsanitize=address compiler flag is absolute cheating and should be prohibited by governments of all countries.
Plus there are also other useful runtime sanitizers, such as integer overflow, but... Integer overflow is much easier to notice, usually.
Of course, this is by no means assuming that -fsanitize=address guarantees that the code it inspects is completely safe and correct and won't ever fail on any input data. You still have to test tricky edge cases. But this sanitizer has improved and sped up the bug discovery process disgustingly much for me.
I'm wondering, how often, if ever, do you need tools such as -fsanitize=address or valgrind? Or are you a real man who stores everything in static memory only?
P.S. Forgive my terrible English.
54
u/tastygames_official Oct 07 '26 •
better than
-fantasize=dress25
u/flatfinger Oct 07 '26
Systems over the years have had a variety of ways of detecting memory usage problems. In the 1990s, the classic Apple Macintosh could produce a list of all regions of storage that had been allocated during an applications lifetime and had not yet been freed, and it was quite satisfying to set a breakpoint just before program exit and have that list include nothing beyond a few essentially static allocations (the Macintosh limited programs to 32767 bytes of actual static-duration storage, so programs that would need more would often acquire a larger chunk of storage, keep a a static-duration pointer to it, and use it like static-duration storage throughout the lifetime of the app; the fact that such allocations would appear on a list of live allocations when a program terminated was normal and expected).
While such techniques are not as common today as in years past, some systems (including the 1980s-1990s Mac OS) encouraged programmers to keep handles to storage rather than pointers, and construct pointers to storage as near as possible to the point of use. Although it would tend to slow program execution to a crawl, the debugger included a feature which would, at any time when the system would be allowed to relocate the storage associated with a handle, cause the system to relocate the storage associated with every unlocked handle. This would catch places where a program should have locked a handle but failed to do so.
Use of whatever tools a system can provide to detect constructs that use memory incorrectly is good if such tools allow one to find problems more effectively than they could have been found via other means.
9
u/pdath Oct 07 '26
I tend to have a "make test" target. It first builds everything with "-fsanitize=address,undefined" and runs all the unit and functional tests, and then rebuilds and uses afl++ for fuzzing on everything.
I like to do development in a separate branch, and I have a rule that before a dev branch can be merged back into main, it must pass the test target.
Even within a dev branch, I probably use "make test" several times a day.
I also have a "make dev" target that makes the code more debugger-friendly, and a "make check" target that does nothing but run the test suite once, without recompiling any binaries. Then I can quickly test the production build and the dev build.
I used to use valgrind more, but the current project I am working on has almost zero dynamic memory allocation.
5
u/ConstructionHot6883 Oct 08 '26
Honestly, I didn't even notice.
13
u/andynzor Oct 07 '26
Not available on embedded. Sad to say this but frontier LLMs are excellent at catching memory errors.
0
u/dkopgerpgdolfg Oct 07 '26
https://www.reddit.com/r/C_Programming/comments/1wz3pkv/using_fsanitizeaddress_feels_illegal/
How often are you going to post this?
4
u/vip17 Oct 07 '26
How long have you been in the software industry and how long of that is in complex software? Have you heard that many governments suggest a memory safe language like Rust, Go or C# for a reason?
1
u/SipsTheJuice Oct 07 '26
We started using it a few years back at my company and caught a number of bugs that had likely been causing occasional crashes or pdd behavior for who knows how long. Super helpful for sure.
1
u/Interesting_Debate57 Oct 07 '26
-lefence
1
u/catbrane Oct 08 '26
tsanis the one that really messes up your CI :(Who wants to be told about subtle bugs! You then have to fix them -- what a waste of valuable time I could instead be spending adding more unreliable and weakly tested features.
1
u/Whole-Low-2995 Oct 10 '26
I actively use it. I sometimes use LLMs when transcribing my pseudo code sketches and they make many mistakes. I needed it for de-slopifying it. I use valgrind too. And if you want to develop fast and fix later you can use libgc when prototyping.
0
u/abu_shawarib Oct 07 '26
Language full of footguns is better when you have a runtime foot shot detector.