Live data from Hacker News

-fbounds-safety: Enforcing bounds safety for C

clang.llvm.org

51–60 of 129 posts

Re: -fbounds-safety: Enforcing bounds safety for C

#51
post #23

this is amazing, counter to what most ppl think, majority of memory bugs are from out of bounds access, not stuff like forgetting to free a pointer or some such

"Majority" could mean a few things; I wouldn't be surprised if the majority of discovered memory bugs are spatial, but I'd expect the majority of widely exploited memory bugs to be temporal (or pseudo-temporal, like type confusions).

Re: -fbounds-safety: Enforcing bounds safety for C

#52
post #23

this is amazing, counter to what most ppl think, majority of memory bugs are from out of bounds access, not stuff like forgetting to free a pointer or some such

Personally, as someone in C and C++ for the last few years, memory access is almost never the root bug. It's almost always logic errors. Not accounting for all paths, not handling edge cases, not being able to handle certain combinations of user or file input, etc.

Occasionally an out-of-bounds access pops up, but they're generally so blindingly obvious and easy to fix that it's never been the slow part of bug fixing.

Re: -fbounds-safety: Enforcing bounds safety for C

#54

template struct Slice { T* data = nullptr; size_t size = nullptr; T& operator[](size_t index) { if (index >= size) crash_the_program(); return data[index]; } }; If you're considering this extension, just use C++ and 5 lines of standard, portable, no-weird-annotations code instead.

  size_t size = nullptr;
wat

Re: -fbounds-safety: Enforcing bounds safety for C

#55
post #23

this is amazing, counter to what most ppl think, majority of memory bugs are from out of bounds access, not stuff like forgetting to free a pointer or some such

Personally, as someone in C and C++ for the last few years, memory access is almost never the root bug. It's almost always logic errors. Not accounting for all paths, not handling edge cases, not being able to handle certain combinations of user or file input, etc. Occasionally an out-of-bounds access pops up, but they're generally so blindingly obvious and easy to fix that it's never been the slow part of bug fixing…

I've been programming for long; the ratio of memory errors to logic bugs in production is so low as to be non-existent.

My last memory error in C code in production was in 2018. Prior to that it I had a memory error in C code in production in 2007 or 2008.

In C++, I eventually gave up trying to ship the same level of quality and left the language altogether.

Re: -fbounds-safety: Enforcing bounds safety for C

#56
post #30

Has any progress been made on this? I remember seeing this proposal 3 or 4 years ago but it looks like it still hasn't been implemented. It's a shame because it seems like a useful feature. It looks like Microsoft has something similar ( https://learn.microsoft.com/en-us/cpp/code-quality/understan... ) but it would be nice to have something that worked on other platforms.

Microsoft's SAL annotations are meant to inform the static analyzer how the parameters are meant to be used so any violations of the contract can be diagnosed at compile time. The LLVM proposal is different in that it is checked at run time and will stop your program before it makes an out of bounds access. Static analyzers can obviously use the information in the type to help diagnose a subset of such problems at compile time.

Re: -fbounds-safety: Enforcing bounds safety for C

#57
post #23

this is amazing, counter to what most ppl think, majority of memory bugs are from out of bounds access, not stuff like forgetting to free a pointer or some such

Personally, as someone in C and C++ for the last few years, memory access is almost never the root bug. It's almost always logic errors. Not accounting for all paths, not handling edge cases, not being able to handle certain combinations of user or file input, etc. Occasionally an out-of-bounds access pops up, but they're generally so blindingly obvious and easy to fix that it's never been the slow part of bug fixing…

logic errors aren't memory errors, unless you have some complex piece of logic for deallocating resources, which, yeah, is always tricky and should just generally be avoided

Re: -fbounds-safety: Enforcing bounds safety for C

#58
post #16

Earlier quoted context omitted.

Or just do it in C. #define span(T) struct span_##T { size_t len; T *data; } #define span_access(T, x, i) (*({ \ span(T) *_v = (x); \ auto _i = (i); \ if (((size_t)_i) >= _v->len) abort(); \ &_v->data[_i]; \ })) https://godbolt.org/z/TvxseshGc

Still requires a gcc/clang specific extension (although this one I'd be very happy to see standardized)

Only statement expressions, but one can also implement this without them.

Re: -fbounds-safety: Enforcing bounds safety for C

#59
post #47
post #16

Earlier quoted context omitted.

Or just do it in C. #define span(T) struct span_##T { size_t len; T *data; } #define span_access(T, x, i) (*({ \ span(T) *_v = (x); \ auto _i = (i); \ if (((size_t)_i) >= _v->len) abort(); \ &_v->data[_i]; \ })) https://godbolt.org/z/TvxseshGc

The fact that pointer types can't be used with this pattern without typedef still seems kinda primitive to me.

You can use pointer types by using a typedef first, but I agree this not nice (I hope we will fix this in future C). But then, I think this is a minor inconvenience for having an otherwise working span type in C.

Re: -fbounds-safety: Enforcing bounds safety for C

#60
> As local variables are typically hidden from the ABI, this approach has a marginal impact on it.

I'm skeptical this is workable... it's pretty common in systems code to take the address of a local variable and pass it somewhere. Many event libraries implement waiting for an event that way: push a pointer to a futex on the stack to a global list, and block on it.

They address it explicitly later:

> Although simply modifying types of a local variable doesn’t normally impact the ABI, taking the address of such a modified type could create a pointer type that has an ABI mismatch

That breaks a lot of stuff.

The explicit annotations seem like they could have real value for libraries, especially since they can be ifdef'd away. But the general stack variable thing is going to break too much real world code.

Post reply on HN