Re:A gentle introduction to static analyzers for C
by bjourne on Wednesday September 27, 2023 @04:37PM
(#37680956)
What I'm missing from static analyzers are English awareness:
Get is a verb that means "to retrive" so "get_foobar" should return something.
Why a signed type? A negative number of bytes is nonsense. In fact a whole language designed around assigning semantic meaning to names would be cool. f has to be a function, n has to be an integer >= 0, M has to be a square matrix of some type, has_xyz must be a boolean, and so on.
[ Parent
| on HN ]
Re:A gentle introduction to static analyzers for C
by cozzyd on Wednesday September 27, 2023 @04:48PM
(#37681101)
get_foobar() could update some thread_local static value, for example.... or print something to screen.
n_bytes could easily be negative if a byte offset, or as a sentinel.
This is why we have a type system, to express these things :)
Re:A gentle introduction to static analyzers for C
by bjourne on Thursday September 28, 2023 @03:53PM
(#37694884)
Nah, those are type errors. Some languages use case so that FOO can never have the same type as foo. Nothing prevents a type system from similarly distinguishing between get_foo() and set_foo().
Re:A gentle introduction to static analyzers for C
by neutrono on Wednesday September 27, 2023 @05:26PM
(#37681566)
In C23 you can use the attribute
[[nodiscard]]
on functions to mark that the result from function shouldn't be discarded.
I think there is also a [[reproducible]]
attribute for saying a function doesn't do side effects.
Re:A gentle introduction to static analyzers for C
by flohofwoe on Thursday September 28, 2023 @02:42AM
(#37686147)
> A negative number of bytes is nonsense.
That's true, but mixing signed and unsigned integers opens a much bigger can of worms (for instance it's not unusual to add a negative quantity to an unsigned integer). It's often better to just stick to signed integer types which are big enough to cover the required positive range (e.g. you only loose one bit, and 63 bits ought to be enough for anybody).
...also an optimizing compiler will usually turn something like:
assert((index >= 0) && (index < size));
...into a single comparison, since on the assembly level it's free to interpret that 'index' as unsigned.