r/C_Programming • • Aug 26 '26

Reliability Lessons From SQLite - Richard Hipp | SSW 2026

https://www.youtube.com/watch?v=V_qzqY1bb7I
39 Upvotes

15 comments sorted by

View all comments

Show parent comment

2

u/thradams Aug 26 '26

I think __builtin_assume is for optimization not safety. I think it makes less safe. because it may overrides some information

2

u/8d8n4mbo28026ulk Aug 26 '26

It is for optimisation, but it can help you catch bugs anyway: if the compiler optimises aggresively based on the assumptions it was given, it'll reveal further design flaws in your code when you test it and get back garbage.

But what is your expectation of an assert() that fails? Turning an assertion into an error-check makes no sense. We use errors when a situation is recoverable from. If an invariant is violated, there's no way to recover from that. If you return an "error" to the caller, what's the caller supposed to do? There's nothing that can be done, except fixing the code.

A safer alternative to __builtin_assume() is to just abort(), but that can incur significant runtime overhead (4x in the case of SQLite, for example).

1

u/thradams Aug 26 '26

My problem is something like:

if (predicate1(p))
{
   //if predicate1(p) is true, then 
   //*p is not null.
   //unless code of predicate1 changes..
   //
   *p->x = 1;
}


if (predicate1(p))
{
   assert(p);
   *p->x = 1;
}


if (predicate1(p))
{
   assume(p);
   *p->x = 1;
}

Ideally we should be able to connect things. this assert is true if you don't change if (predicate1(p)) and if you don't change the implementation of predicate1

2

u/8d8n4mbo28026ulk Aug 26 '26

If you want practical advice, for the above and any such code, just use an assert() and don't think about it. If predicate1() changes and p ends up being NULL, you want a loud signal that the code is buggy and you need to fix it.

Check for errors if the situation is truly recoverable from. If you try to open a file and that file doesn't exist, that's obviously a recoverable situation (and likely not your fault). What I mean is, it's an expected state that your program may enter. If p is NULL after predicate1(), that isn't at all a state your program is expected to be in, because you subsequently fetch ->x from it.

Here's a quote I like from Carmack:

A large fraction of the flaws in software development are due to programmers not fully understanding all the possible states their code may execute in.

If your code enters an invalid state, error-checking at runtime will generally not save you -- you have to fix the code. Use an assert() and perhaps abort() on release builds if you're really worried.

1

u/thradams Aug 27 '26

I am writing a static analyzer, and I think the best approach is to move the assert from the caller into the function itself. However, we need a syntax for that. (just a sample)

bool  is_empty(struct X* p) 
   if (_R) {
      assert(p->a) ;
   }
   else 
   {
   };

Maybe natural language grammar? (looks natural but is grammar, not AI)

/*returns 
    true if the list is empty. In this case plist->hread is not null
    otherwise return false and plist->head is not null.
*/
bool  is_empty(struct list* plist);

The idea is that, if I change the function is_empty changed the contract, the places are using the information will not override the assert. they will use the assert that is part of the contract.

Why not C++ contracts? I don't like the idea of individual and independent contracts-predicates. Normally the function contracts are like the sample I wrote.. and we have pos conditions that are related to function result.