Like (where possible) detecting strict aliasing bugs, and making sure the source code actually checks the value return by "malloc()", and array indexes, and various signed integer shifts, and whether the input/output parameters for a "extern void foo(void)" declaration actually matches the definition, and probably hundreds of other corner cases.alexfru wrote:True. IOW, the compiler can help only so much with broken code.Brendan wrote: a) C is bad because it has too many rules that are not enforceable by the compiler
Like what? I know that several years ago it didn't warn about indexing an array with an invalid (too large) index in code something like "int a[10]; a[11] = 1;". I also know that it doesn't always spot things like "a[ i] = i++;" (a bit more complex expression will cause it miss the problem). What else?Brendan wrote: b) GCC is bad because it doesn't detect or report "invalid input" where it could (C's rules that are enforceable by the compiler)
Think of it like this. For C there's about 50 different static analysers. For just one of them (from the Coverty wikipedia page) we get things like "the tool was used to examine over 150 open source applications for bugs; 6000 bugs found by the scan were fixed, across 53 projects". Of those maybe about 15% are bugs that the compiler could have detected but failed to mention (basically everything where a static analyser can find the bug with no "false negatives"), and the remaining bugs are problems in the C standard.
I used to think it was mostly a problem with the programmers too (and in some cases it is). However; people with far more experience using C than I'll (hopefully) ever have are still creating the same bugs as everyone else. Mostly, it's easy to write code in C that "seems to work" that doesn't comply with the language's specification and has subtle bugs that might not be noticed for 10+ years even if/when hundreds of C programmers look at the code.alexfru wrote:For a long time I thought it was mainly a problem of teaching/learning the language properly, of availability of good books and articles. The language is clearly much less intuitive than others, less well defined than assembly language / CPU architecture and makes math even more inhumane.Brendan wrote: c) the combination of bad language and bad compiler unnecessarily increases the number of bugs and security vulnerabilities users are exposed to for no reason whatsoever
It's much worse than that. For a simple test, see if you can write a 100% correct/valid version of this code:alexfru wrote:I'm not sure if it's the largest. You have to be pretty much in a paranoid mode when writing or fixing security-sensitive code. It's not a typical mindset/mode for most software developers. We also tend to overcomplicate things to the point at which it becomes hard to not miss an important edge case as such and not get overwhelmed by the amount of code we're dealing with. From my experience with Windows code I can tell that missing/insufficient/incorrect checks/validation were around the top issues implementation-wise. Even decent C/C++ coders from time to time will forget to check this or that.Brendan wrote: d) this is probably the single largest "root cause" of security vulnerabilities
Code: Select all
int saturatingAdd(int a, int b) {
if(a + b > INT_MAX) return INT_MAX;
if(a + b < INT_MIN) return INT_MIN;
return a + b;
}
Maybe, yes. It depends too much on what you change and how. For a simple example, if the C standard said signed integer overflow causes wrapping (and isn't undefined) it'd probably be faster on most CPUs.alexfru wrote:You'd have to trade some performance to make C well/better defined.Brendan wrote: e) with a better language and better compiler the majority of security vulnerabilities could've been avoided without any significant disadvantages
Cheers,
Brendan