4 ms·
Unfortunately, int main(){ char buffer[10]; buffer[10] = 0; } are so rare they are hardly worth bothering with. The more usual case is
by WalterBright 3y ago
Unfortunately,
int main(){
char buffer[10];
buffer[10] = 0;
}
are so rare they are hardly worth bothering with. The more usual case is:
int get(char* buffer)
{
return buffer[10];
}
void test() {
char buffer[10];
get(buffer);
}
I.e. the array bounds for buffer get lost in the function call. I have proposed a fix:
https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
that is compatible with existing code.
And yet, nobody cares. Oh well! Instead, we have overly complex solutions like this:
https://developers.redhat.com/articles/2022/09/17/gccs-new-fortification-level https://developers.redhat.com/articles/2022/09/17/gccs-new-f...
- philzook 3y agoYour example does not pass the verifier cbmc /tmp/walter.c --bounds-check --pointer-check --function test ... [get.pointer_dereference.5] line 3 dereference failure: pointer outside object bounds in buffer[(signed long int)10]: FAILURE ... There are many footguns. People love their guns. Makes them feel powerful.
- WalterBright 3y agoint get(char* buffer, int i) { return buffer[i]; } #include <stdio.h> void test() { char buffer[10]; get(buffer, getc(stdin)); }
- nanolith 3y agoCBMC's default contract for getc returns a non-deterministic integer value. A non-deterministic integer value basically counts as "pick a value that would cause this de-reference to crash the machine model". So, it will find a counter-example.
- philzook 3y agoYes, it returned the same "FAILURE" along with a couple new checks about getc that do pass.
- WalterBright 3y agoSounds like it's doing a solid job of data flow analysis. But I can still produce cases where it cannot determine either the range of the index or the length of the array being pointed to. The programmer will have to add code to keep track of the length of the array, and add code to check the value of the index. The good thing is the analyzer will tell you where you'll need to add those checks. With the proposal I made, none of this is necessary. It just works. Over two decades of experience with this approach in D is it-just-works.
- nanolith 3y agoI'm curious, have you used CBMC? The machine model tracks the array with its size and compares this to any index. This is loaded into an SMT solver. This is not a standard static analyzer. It's compiling the code to an abstract machine model and then running through this model with an SMT solver. CBMC isn't perfect, but the strategies you are talking about are meant to defeat a linter or a shallow analyzer in a compiler. CBMC isn't a linter.
- WalterBright 3y agoI have never heard of CBMC before this thread. SMT cannot solve cases where it simply cannot know what the length of an array is. For example, when array is allocated in code not available to the solver, or is set by the environment. I remember trying out Spark a few years ago, which advertised its compile time checking abilities. I tried using an integer equation that used an OR, and it gave up on it. If you need to add in range checking code to help the solver along, then the range check itself is a source of bugs, and the range limit has to be made available somewhere and be correct. In my proposal, the array length is set at the time of creation of the array, and the length is carried along with the pointer. The solver's role then becomes one of eliminating the bounds check in the cases where it can prove the index is within range. The user doesn't have to do anything. We've been using it in the implementation of the D compiler for a very long time now, and problems with buffer overflows are a faded memory. P.S. I also added manual integer overflow checks when passing the size to malloc(), no matter how unlikely an overflow would be :-)
- mhh__ 3y agoA lot of static analysis tools explicitly support forcing you to check the value of "tainted" values such as return values of getc and so on i.e. even with slices you want to be able to catch this kind of thing ahead of time.
- Joker_vD 3y ago> There are many footguns. People love their guns. Makes them feel powerful. Well, duh: if you remove the most egregious footguns from C, you end up basically with Pascal, and here is Why Pascal Is Not My Favorite Programming Language. And when you try to explain to them that C's abstract machine has semantics of "when you omit a safety check, you actually makes a promise to the compiler that this check is redundant, that you'll promise that you'll make sure in some other way that in no possible execution will the illegal values ever show up in this place, and the compiler will actually hold you to this promise" — they throw tantrums and try to argue that no, it hasn't this semantics, and even if has, it's not the semantics as originally intended by K&R and it is the authors of the C compilers who are wrong (even though the authors of C compilers are the people who actually write both the C standards and the C compilers...) Must have to do something with imprintment: I've learned C as my 3rd language, and from the start I was taught that its UB is absolutely insane — and I've never felt "nah, it should just do what I meant, dangit" denial about it, only "this is sad, how can I even know while writing code when I am making a baseless promise especially when it's made by omitting something?" But like you've said, people like their footguns.
- GuestHNUser 3y ago> Must have to do something with imprintment Eh, I think most C programmers frustrations with UB stem from knowing that the C standard has fundamental flaws and modern compilers abuse that fact for "optimizations" on UB. This paper covers the topic pretty well[0]. I fully support Casey Muratori's viewpoint that undefined behavior should not exist in the standard[1]. Instead, the C standard should enumerate all valid behaviors compliant compilers can implement. This would allow compilers to make unintuitive optimizations for the platforms that need them, but still allow programmers to be certain that their program semantics will not change in different versions of the said compiler. [0] https://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_2015_submission_29.pdf https://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_20... [1] https://youtu.be/dyI0CwK386E?si=vsqJ8uWHY8xkGmFm https://youtu.be/dyI0CwK386E?si=vsqJ8uWHY8xkGmFm
- Joker_vD 3y ago> I think most C programmers frustrations with UB stem from knowing that the C standard has fundamental flaws and modern compilers abuse that fact for "optimizations" on UB Do they actually know that? I don't think so. Let's take [0] for instance (the author is an expreienced C programmer who loves the language and wrote a re-implementation of bc in it): There seemed to be a lot of misunderstandings; I could not get a handle on what this person thought UB meant. I finally figured it out: this person’s definition of UB was not “the language spec can’t guarantee anything.” Instead, it was “compilers can assume UB does not exist and optimize accordingly.” Wat. Yep. Apparently, that was news to him, even though C implementations have been behaving like that for about 30 years already, and you can read the rest of the post for the "this is evil, we the users must do something about it" take. And the proposed "something" is not "we should instead use a language with actually defined semantics", oh no. It's "use compiler flags to force more reasonable behaviour, hopefully" and "somebody should write boringcc, unfortunately, I am myself a bit too busy for that". Well, despite numerous pleas and several attempts, nobody has managed to write boringcc which is telling of something, I'm just not sure of what exactly. So... I think it is about imprinting: "Oh, it's a wonderful language, well, it would be if it was actually implemented the way I used to think it is implemented (and I still think it should be implemented that way) but still, it's a wonderful language if only not for that pesky realitiy" is forcing unfounded expectations onto reality, and where do these expectations even come from in the first place? And yeah, I fully agree with you that C standard should've probably done that. But it didn't happen, and it can't happen because backwards compatibility [1]. But even then, C programs would still be non-portable, in a sense that you have to use #ifdef's for tinkering with platform-specific behaviour for anything interesting, because C standard even today leaves a lot of stuff completely up to implementation, see [2] for an especially apalling example, even without touching UB. [0] https://gavinhoward.com/2023/08/the-scourge-of-00ub/ https://gavinhoward.com/2023/08/the-scourge-of-00ub/ [1] https://thephd.dev/your-c-compiler-and-standard-library-will-not-help-you https://thephd.dev/your-c-compiler-and-standard-library-will... [2] https://thephd.dev/conformance-should-mean-something-fputc-and-freestanding https://thephd.dev/conformance-should-mean-something-fputc-a...
- sigsev_251 3y agoWith all respect, please don't say nobody cares. There many people in the C community that want this but it's not easy to add a fat pointer to C after all these years. Though, there are people who are planning to work on this exact solution [1]. The best thing the rest of the ecosystem can do, is support them. [1]: https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Non-owning%20Sized%20Arrays.html https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Non-own...
- WalterBright 3y agoI've never gotten a positive response for it. I thank you for your interest on it and the reference, that's good! BTW, it's simple to implement.
- sigsev_251 3y ago(Just to be clear, I am not the author of the draft I posted in the parent comment, but it's the feature I want the most to be added in C2y. I was actually meaning of writting a proposal, but realized that I don't know a thing about compilers and language design.) I have examined many other ways one could add such functionality in C. I found that the most pleasant one is a combination of your syntax and some runtime operators to retrieve the length. As for the lack of implementation burden, I would send an email to thephd in your position. I think that having that comment coming from you would be really beneficial for the paper.
- teo_zero 3y agoYou don't need new runtime operators to retrieve the length. Once you remove the decay issue, you can use sizeof: sizeof(a) sizeof(a)/sizeof(a[0])
- sigsev_251 3y agoI get what you mean and I'm not saying that not having extra operators is a dealbreaker that will make spans unusable. I just find it more pleasant/convenient for the following reasons: 1. Helps clarify intent 2. Makes teaching the feature easier, since it's name is closer to what the user wants to write. Also, rejecting pointers helps people not make mistakes. 3. Keeps sizeof consistent. The operator sizeof is supposed to return the size of the storage occupied by the object. A span is a pointer and some sizes (one could also design a span to be strided, meaning more integers to the mix), so sizeof could just return the size of that pointer ond the integers. Having sizeof be somewhat overloaded for spans could be surprising to nome people. But again, just having sizeof is fine
- uecker 3y agoYou could pass a pointer to the array instead of letting it decay: int get(int n, char (*buffer)[n]) { return buffer[10]; } int main() { char buffer[10]; get(10, &buffer); } Then a run-time bounds checker or a model checker can find the bug: https://godbolt.org/z/a4ExKMn1s https://godbolt.org/z/a4ExKMn1s
- teo_zero 3y agoIf you "just" want bounds checking, then you don't need a pointer to array. GCC is able to detect the out-of-bound even in this case: int get(int n, char buf[n]) { return buf[10]; } It's if you want to use sizeof that you need a pointer to array. But if you have n, why would you need to query the size of your array?
- sigsev_251 3y agoA possible answer is that one could modify n inside the function body, which means that the length is lost. But honestly, one could just use const to avoid this. Though, pointers to VLAs are really useful and convenient when allocating dynamic arrays, especially multidimensional.
- uecker 3y agoGCC only works in the first case anyway: https://godbolt.org/z/jo57r1MW4 https://godbolt.org/z/jo57r1MW4 One could add in the other too, but language semantics currently do not allow this. Changing the n later has no impact on the size of the arrays later and the compiler is perfectly able to remember the original size.