r/cpp • • Oct 24 '24 • Take My Silver 1

Why Safety Profiles Failed

https://www.circle-lang.org/draft-profiles.html
180 Upvotes

347 comments sorted by

View all comments

Show parent comment

-1

u/germandiago Oct 26 '24 edited Oct 26 '24

Sure, but std::sort is still in the standard library and third-party libraries exist, so you need a strategy to deal with iterator-based algorithms anyways.

We need to deal with sort(begin, end) and cannot recommend people to change a single line of code to sort(rng) that is there since C++17 but we can ask everyone to rewrite their code in Safe C++ or otherwise give up safety and wait for a std2 cpp library implementation so that the new std::sort(beg, end) can even work?! Seriously?!

Something does not match here for me.

6

u/ts826848 Oct 26 '24 edited Oct 27 '24

We need to deal with sort(begin, end) and cannot recommend people to change a single line of code to sort(rng) that is there since C++17 but we can ask everyone to rewrite their code in Safe C++ or otherwise give up safety and wait for a std2 cpp library implementation so that the new std::sort(beg, end) can even work?! Seriously?!

All this is basically irrelevant, because by focusing on the use of std::sort and worrying about incremental rewrites you've basically completely missed the point of the example.

What the example is trying to demonstrate is a soundness flaw in the profiles proposal. The claim is that there are functions for which soundness preconditions cannot be expressed in C++'s type system, and without some explicit marker indicating the presence of these preconditions there's no way for profiles to reject code that doesn't promise to uphold those preconditions. std::sort is an example of such a function, but there's an infinite universe of possible functions that could be used as an example instead, so having an alternative to std::sort does not fix the underlying issue.

If anything, just forget std::sort was used. Pretend it's some random function with soundness preconditions:

#include <memory>
#include <vector>

// SAFETY: v1 and v2 must have the same length.
void maybe_cause_ub(std::vector<int>& v1, std::vector<int>& v2);

int main() {
    std::vector<int> v1, v2;
    v1.push_back(1);
    v1.push_back(2);

    // UB!
    maybe_cause_ub(v1, v2);
}

This criticism of profiles is completely independent of the existence of Safe C++. Pretend Safe C++ doesn't exist for all it matters - the example does not depend on it except as a point of comparison, and it provides Rust and C# as further comparisons anyways.