r/C_Programming • u/ozuq06 • 9h ago
Discussion Thoughts on safe-c
Has anyone here come across the safe-c project? If so, what do you think of it? If not, do you think a project like this could actually make C (and C++) safer and more attractive in certain fields? Curious to hear what people think.
8
Upvotes
6
u/whizzter 8h ago
Honestly imho safe-c is useless/chatty compared to real code and probably won't be used since it'd require way too much rework.
HOWEVER, safe-C++ is similar to what I've thought about myself however, basically something rust/C#'ish ontop of already existing semantics via the existing standard library (even if I think std::span and some others parts should become intrinsics to speed up compile times of C++ codebases, they are probably half-hardcoded already with this safe-C++ if the checker should be able to do things).
That way, the older parts of a codebase can be migrated over in piecemeal parts and people can keep producing new code according to the specification without stopping production updates whilst the upgrade is done in parallell.
People who have adopted (modern) C++ should mostly be acceptable of it since the primitives of safe-C++ should already be in use (assuming they've been allowed to use modern compilers), pure C programmers however really don't go around using similar primitives and if they haven't adopted C++ in the first place I have a hard time seeing them adopt this.
11
u/Stickhtot 8h ago
Safety is for ****** I like my pointers RAW /s
3
u/Apprehensive-End6779 7h ago
This is probably vibecoded, but a good idea.
2
u/ComradeGibbon 6h ago
I think the standard library is really the issue not the language so much. Much of the standard library requires the code that calls it to insure the operations are safe beforehand. And that's terrible from a security standpoint.
They should add slice and buffer types to the standard library. And then provide alternate safe functions that return errors when called with bad arguments. A bonus would be to change the language where types can decay to slices.
You should be able to do this
int Foo(slice_t bas);struct bas boo = { stuff};Foo(boo); // boo gets passed as a slice_t2
u/ewmailing 6h ago
I haven't heard of safe-c, but I have heard of Fil-C, and I am throughly impressed with Fil-C.
Casey Muratori was a guest-cohost on Wookash Podcast (Łukasz Ściga), interviewing Fil-C creator, Filip Pizło, about it. https://www.youtube.com/watch?v=6Maoe-GMynM
Filip Pizlo presented a talk on it at Software Should Work conference: https://www.youtube.com/watch?v=5F-2Y1LPRek
2
u/WittyStick 6h ago edited 5h ago
Cake C already implemented some of this a couple of years ago - though it is now also a slop project.
The ideas themselves aren't bad - if you were going to augment pointers with additional things, then adding new qualifiers is the obvious choice - though, they should be optional, perhaps implemented with C23 attributes.
Since unknown attributes will be ignored on unsupported compilers. Alternatively,
_Nullablecould just be defined to be nothing if not supported, which will prevent a bunch of "unrecognized attribute" warnings.IMO, every new qualifier should have an inverse. If we have
_Nullable, we should have a_NotNull, for_Ownedwe should have a_Shared, an so forth. By default, a pointer with none of these qualifier is_Nullableand_Shared- since that's what a regular C pointer is. If you start changing the defaults you're no longer working with C, but a forked language - incompatible with existing tooling. If we want null checking on a pointer (with our own front-end) we should mark it_NotNull- but if compiling with some other compiler it does nothing - it's merely an annotation to inform the reader.However, one should also be able to opt into having
_NotNullas default, like C# does with#nullable enable- which should be#pragma nullable enablefor C. If non-null pointers are turned on, that's when you need the_Nullableto be explicit. Similar to C# there should also be#pragma nullable disableand#pragma nullable restore. Again - for the same reason as using attributes - unsupported pragmas are ignored - it doesn't prevent the code from compiling with an existing compiler.If we have a
void *_NotNull, then it should be implicitly coercible tovoid *_Nullable. The nullable version is a supertype - since it can contain everything the not-null one can, plus null. Convertingvoid *_Nullabletovoid *_NotNullhowever, should be explicit - in the form of definite assignment analysis.Anyway, this project will fail for the same reason others have:
2
u/HalifaxRoad 7h ago
Its really not that hard to not have memory safety problems if you know how the cpu actually works... Instead of high order languages hiding the functionality behind some bullshit. People should really start with asm, it will make you appreciate how this shit actually works....
1
u/kansetsupanikku 7h ago
Thoughts? It's difficult, and the idea is as old as C. New vibe-coded toy is not going to address it.
1
u/AWonderingWizard 8h ago
Following this to hear what others that know more might have to say.
-8
u/GuidingMoth 8h ago
Just use Rust 🤷