r/C_Programming • • 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

22 comments sorted by

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

5

u/mlugo02 8h ago

Same but without the /s

3

u/Apprehensive-End6779 7h ago

This is probably vibecoded, but a good idea.

1

u/AWonderingWizard 7h ago

This is my concern

2

u/Apprehensive-End6779 7h ago

It's not that AI is inherently bad, it's that there's probably issues in the AI implementation that a slopcoder wouldn't have caught, which will lead to... unsafe code on a compiler that should be safe 😂

2

u/AWonderingWizard 7h ago

I don't think the outputs are necessarily bad just because an AI made them, but I don't like to trust any code that was generated by AI without being genuinely checked by humans too. But doesn't this effectively reduce the benefits from using an AI in the first place?

2

u/Apprehensive-End6779 7h ago

I think I like this stance, yeah

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_t

2

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.

void * [[nullable]] foo;

Since unknown attributes will be ignored on unsupported compilers. Alternatively, _Nullable could just be defined to be nothing if not supported, which will prevent a bunch of "unrecognized attribute" warnings.

#ifdef _NOT_SLOP_PROJECT_
#define _Nullable [[nullable]]
#else
// define it to be erased by the preprocessor
#define _Nullable
#endif

IMO, every new qualifier should have an inverse. If we have _Nullable, we should have a _NotNull, for _Owned we should have a _Shared, an so forth. By default, a pointer with none of these qualifier is _Nullable and _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 _NotNull as default, like C# does with #nullable enable - which should be #pragma nullable enable for C. If non-null pointers are turned on, that's when you need the _Nullable to be explicit. Similar to C# there should also be #pragma nullable disable and #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 to void *_Nullable. The nullable version is a supertype - since it can contain everything the not-null one can, plus null. Converting void *_Nullable to void *_NotNull however, should be explicit - in the form of definite assignment analysis.

void foo(void *_NotNull);

void *_Nullable bar;
if (bar != NULL)
    // At this point, `bar` can be treated as `_NotNull` because we've done the runtime check.
    foo(bar); // Ok.
else
    foo(bar); // Not ok - bar is definitely nullable - a subtype of nullable even: `nullptr_t`.

Anyway, this project will fail for the same reason others have:

  • It's not C, it's a non-backward compatible dialect
  • It adds features rather than removing or constraining them
  • Code written for it won't compile with stock Clang or GCC.
  • It's slop.

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....

3

u/johnwcowan 7h ago

If that's what you want, assembly language is too high level.

1

u/HalifaxRoad 6h ago

yeah I mean fair, assembly still does quite a bit for you lol.

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 🤷

10

u/BrodoSaggins 8h ago

brother we are on r/C_Programming

2

u/GuidingMoth 8h ago

Wow I really kicked the hornets nest 😅

4

u/AWonderingWizard 7h ago

lol you did come down to the home turf

5

u/AWonderingWizard 8h ago

Rustacean triggered! +1