News for hackers. Stuff that matters.
Re:Fil-C: A memory-safe C implementation by kragen on Wednesday October 29, 2025 @02:03AM (#45743179)
Hopefully Pizlo will correct me if I get this wrong, but I don't think Fil-C's pointer tagging enforces constness, which isn't needed for C in any case. This C code compiles with no warnings and outputs "Howlong\n" with GCC 12.2.0-14 -ansi -pedantic -Wall -Wextra:

    #include <stdio.h>

    int main()
    {
      const char c[] = "Howling\n";
      char *p = (char*)c;
      p[4] = 'o';
      printf("%s", c);
      return 0;
    }
Somewhat to my surprise, it still compiles successfully with no warnings as C++ (renaming to deconst.cc and compiling with g++). I don't know C++ that well, since I've only been using it for 35 years, which isn't nearly long enough to learn the whole language unless you write a compiler for it.

Same results with Debian clang (and clang++) version 14.0.6 with the same options.

Of course, if you change c[] to *c, it will segfault. But it still compiles successfully without warnings.

Laundering your pointer through an integer is evidently not necessary.

Re:Fil-C: A memory-safe C implementation by pizlonator on Wednesday October 29, 2025 @03:14AM (#45743617)
Fil-C capabilities have a read only bit of this purpose.
Re:Fil-C: A memory-safe C implementation by kragen on Wednesday October 29, 2025 @03:18AM (#45743638)
Interesting, so you really can enforce constness on references? Does that mean the above code will crash on Fil-C?
Re:Fil-C: A memory-safe C implementation by mbrock on Wednesday October 29, 2025 @07:16AM (#45745290)
I tried it and it doesn't crash when compiled with Fil-C, FYI. But I guess it wouldn't be too hard to implement.
Re:Fil-C: A memory-safe C implementation by pizlonator on Wednesday October 29, 2025 @08:32AM (#45745990)
Just using const in C isn’t enough, since C’a const is unsound, so I ignore it.

But if you try to write to a readonly global constant then you’ll panic. And there are a handful of ways to allocate readonly data via Fil-C’s APIs.

Re:Fil-C: A memory-safe C implementation by jacquesm on Sunday November 02, 2025 @03:21PM (#45793070)
> Just using const in C isn’t enough, since C’a const is unsound, so I ignore it.

And now I have confused faces looking at me why I just laughed out loud.

Re:Fil-C: A memory-safe C implementation by jibal on Wednesday October 29, 2025 @02:19AM (#45743270)
Did you compile with -Wcast-qual?
Re:Fil-C: A memory-safe C implementation by kragen on Wednesday October 29, 2025 @02:32AM (#45743352)
No, that does give a warning.
Re:Fil-C: A memory-safe C implementation by einsteinx2 on Wednesday October 29, 2025 @07:34AM (#45745427)
> I don't know C++ that well, since I've only been using it for 35 years

Ok that got a chuckle out of me haha

Re:Fil-C: A memory-safe C implementation by cryptonector on Wednesday October 29, 2025 @12:44PM (#45749435)
> > I don't know C++ that well, since I've only been using it for 35 years, which isn't nearly long enough to learn the whole language unless you write a compiler for it.

No one person could write a compiler for it, and even if they could they would forget as much in doing so as they could learn.

Re:Fil-C: A memory-safe C implementation by kragen on Wednesday October 29, 2025 @02:08PM (#45750766)
Walter Bright did, although maybe C++ has gotten enough bigger since then that your statement has become true.