December 02, 2021

On Thursday, 2 December 2021 at 13:19:09 UTC, Steven Schveighoffer wrote:

>

On 12/2/21 7:15 AM, Johan wrote:

>

On Thursday, 2 December 2021 at 11:01:07 UTC, Imperatorn wrote:

>

On Thursday, 2 December 2021 at 08:09:18 UTC, Paulo Pinto wrote:

>

[...]

Bottom line:
Use D instead of C 😎

Sorry to rain on the party here, but D is of course not at all immune to this problem.
It was not hard to find out-of-bounds memory access in the D compiler, using the fuzz techniques mentioned in the article.

The D compiler is not @safe.

Using D isn't enough, you need to use @safe D.

Even that isn't enough, because most of the time people misuse @trusted (because it's so easy to misuse).

-Steve

Wish the @safe by default DIP had passed :(
Any hope of reviving it and merging into master??

December 02, 2021

On Thursday, 2 December 2021 at 16:44:42 UTC, Tejas wrote:

>

Wish the @safe by default DIP had passed :(
Any hope of reviving it and merging into master??

Only if someone can (a) come up with a better solution for handling extern(C) functions, and (b) convince Walter to accept it.

I think a more likely path forward is to allow the programmer to specify default attributes in such a way that they can still be overriden by inference. For example:

// Make @safe the default for the rest of this scope
default(@safe):

// defaults to @safe
int foo(int n) { return n; }

// error: can't cast integer to pointer in a @safe function
int* bar(int n) { return cast(int*) n; }

// ok: inferred as @system (because of `auto`)
auto baz(int n) { return cast(int*) n; }

This is the same idea as Adam D. Ruppe's attribute proposal, but with new syntax to avoid potential breakage to existing code.

As Adam explains in his article, we cannot do this by simply applying the @safe attribute globally, because doing so will override the compiler's attribute inference, and cause compilation of functions like baz to fail. (Example: https://run.dlang.io/is/s9iuKq)

December 02, 2021

On Thursday, 2 December 2021 at 17:11:09 UTC, Paul Backus wrote:

>

On Thursday, 2 December 2021 at 16:44:42 UTC, Tejas wrote:

>

Wish the @safe by default DIP had passed :(
Any hope of reviving it and merging into master??

Only if someone can (a) come up with a better solution for handling extern(C) functions, and (b) convince Walter to accept it.

I think a more likely path forward is to allow the programmer to specify default attributes in such a way that they can still be overriden by inference. For example:
[snip]

+1

Will help prevent any breaking changes if if they ever do decide to go to @safe by default.

December 02, 2021

On Thursday, 2 December 2021 at 17:11:09 UTC, Paul Backus wrote:

>

On Thursday, 2 December 2021 at 16:44:42 UTC, Tejas wrote:

>

Wish the @safe by default DIP had passed :(
Any hope of reviving it and merging into master??

Only if someone can (a) come up with a better solution for handling extern(C) functions, and (b) convince Walter to accept it.

I think a more likely path forward is to allow the programmer to specify default attributes in such a way that they can still be overriden by inference. For example:

// Make @safe the default for the rest of this scope
default(@safe):

// defaults to @safe
int foo(int n) { return n; }

// error: can't cast integer to pointer in a @safe function
int* bar(int n) { return cast(int*) n; }

// ok: inferred as @system (because of `auto`)
auto baz(int n) { return cast(int*) n; }

This is the same idea as Adam D. Ruppe's attribute proposal, but with new syntax to avoid potential breakage to existing code.

As Adam explains in his article, we cannot do this by simply applying the @safe attribute globally, because doing so will override the compiler's attribute inference, and cause compilation of functions like baz to fail. (Example: https://run.dlang.io/is/s9iuKq)

Hmm... not a fan of that solution

Still feel marking extern (C) stuff as @trusted is better.

Introducing a new feature for such a fundamental, yet obvious thing seems overkill, IMHO. Forcing not @safe stuff to be annotated seems better to me.

Maybe we should relent and just let Walter mark the extern stuff as @safe even though it's not... ideal??

Or disallow marking any extern function as @safe, only @trusted? That's again introducing unnecessary complexity though :(

Gah, I say let him have his way, it's not worth adding a new feature/syntax for, but not something we should just let sit on ice either, else "it will break old code" will rear it's ugly head again

December 02, 2021
On Thu, Dec 02, 2021 at 05:11:09PM +0000, Paul Backus via Digitalmars-d wrote:
> On Thursday, 2 December 2021 at 16:44:42 UTC, Tejas wrote:
> > 
> > Wish the `@safe` by default DIP had passed :(
> > Any hope of reviving it and merging into master??
> 
> Only if someone can (a) come up with a better solution for handling
> `extern(C)` functions, and (b) convince Walter to accept it.

The solution is very simple: extern(C), extern(C++), and extern anything
except extern(D) really, should be @system, and extern(D) (the default)
should be @safe.

The real issue is convincing Walter to accept this.


T

-- 
Elegant or ugly code as well as fine or rude sentences have something in common: they don't depend on the language. -- Luca De Vitis
December 02, 2021

On Thursday, 2 December 2021 at 17:36:18 UTC, H. S. Teoh wrote:

>

On Thu, Dec 02, 2021 at 05:11:09PM +0000, Paul Backus via Digitalmars-d wrote:

>

On Thursday, 2 December 2021 at 16:44:42 UTC, Tejas wrote:

>

Wish the @safe by default DIP had passed :(
Any hope of reviving it and merging into master??

Only if someone can (a) come up with a better solution for handling
extern(C) functions, and (b) convince Walter to accept it.

The solution is very simple: extern(C), extern(C++), and extern anything
except extern(D) really, should be @system, and extern(D) (the default)
should be @safe.

The real issue is convincing Walter to accept this.

T

You'll have to atleast create @trusted wrappers around each such function though, if you want normal D functions to call them :(

That's why I feel we should allow @trusted to be annotated on extern functions.

December 02, 2021
On Thu, Dec 02, 2021 at 11:44:28AM +0000, IGotD- via Digitalmars-d wrote:
> On Thursday, 2 December 2021 at 11:27:01 UTC, H. S. Teoh wrote:
> > 
> > Another nail in the coffin of C.  Still many more nails to go, but the inevitable draws ever nearer.
[...]
> Would it be impossible to add bounds checking in C?
> 
> It's been over 4 decades and it seems like there is some profound resistance to add this.

C wouldn't be C anymore once it has bounds checking.  You can bet your life that the moment somebody proposes such a thing, complaints about "efficiency" will flare up all over the intarweb.  99% of the C userbase will revolt, and the proposal will be DOA.

Not to mention, how *do* you add bounds checking to a language where arrays automatically and implicitly decay to pointers?  No length information is present for you to do bounds checking on. The only way to fix this is to reengineer the language from scratch to introduce bounds-checked arrays.  But that does not fix the problems with the existing standard C libraries which take bare pointers for arrays. *Replacing* current arrays with bounds-checked ones essentially turns it into D, at which point it's not C anymore (see previous paragraph).


T

-- 
Just because you can, doesn't mean you should.
December 02, 2021
On Thu, Dec 02, 2021 at 05:39:11PM +0000, Tejas via Digitalmars-d wrote:
> On Thursday, 2 December 2021 at 17:36:18 UTC, H. S. Teoh wrote:
[...]
> > The solution is very simple: extern(C), extern(C++), and extern
> > anything except extern(D) really, should be @system, and extern(D)
> > (the default) should be @safe.
> > 
> > The real issue is convincing Walter to accept this.
[...]
> You'll have to atleast create `@trusted` wrappers around each such function though, if you want normal D functions to call them :(
> 
> That's why I feel we should allow `@trusted` to be annotated on `extern` functions.

I thought that was obvious?  We're talking about @system **by default** here.  It does not preclude marking extern(C) declarations as @trusted.

Though TBH, that in itself is suspect; a function like memcpy, for example, CANNOT be marked @trusted because it does not have a safe API: the caller can easily pass a wrong argument to memcpy and it will overrun the buffer.  Marking such a thing as @trusted undermines @safe, and we're back to square one w.r.t. memory safety.

Unfortunately, this is exactly what most people will do, because they just want the compile error to go away and stop bothering them. So in name their code is @safe, but it doesn't mean very much in actuality because there are badly-tagged @trusted functions inside where who knows what can happen.


T

-- 
Живёшь только однажды.
December 02, 2021

On Thursday, 2 December 2021 at 17:21:58 UTC, Tejas wrote:

>

On Thursday, 2 December 2021 at 17:11:09 UTC, Paul Backus wrote:

>

On Thursday, 2 December 2021 at 16:44:42 UTC, Tejas wrote:

>

Wish the @safe by default DIP had passed :(
Any hope of reviving it and merging into master??

Only if someone can (a) come up with a better solution for handling extern(C) functions, and (b) convince Walter to accept it.

I think a more likely path forward is to allow the programmer to specify default attributes in such a way that they can still be overriden by inference. For example:

// Make @safe the default for the rest of this scope
default(@safe):

// defaults to @safe
int foo(int n) { return n; }

// error: can't cast integer to pointer in a @safe function
int* bar(int n) { return cast(int*) n; }

// ok: inferred as @system (because of `auto`)
auto baz(int n) { return cast(int*) n; }

This is the same idea as Adam D. Ruppe's attribute proposal, but with new syntax to avoid potential breakage to existing code.

As Adam explains in his article, we cannot do this by simply applying the @safe attribute globally, because doing so will override the compiler's attribute inference, and cause compilation of functions like baz to fail. (Example: https://run.dlang.io/is/s9iuKq)

Hmm... not a fan of that solution

Still feel marking extern (C) stuff as @trusted is better.

Introducing a new feature for such a fundamental, yet obvious thing seems overkill, IMHO. Forcing not @safe stuff to be annotated seems better to me.

Sure, if you don't use extern(C) much and you don't care about everyone that uses it extensively in their code moving to other languages, go ahead and do that. It's not realistic to break that much code written in a language that was designed from the start to be closely tied to C. It wouldn't be hard to add a -safe compilation flag, but apparently nobody's into that, they'd prefer to break code others have written.

December 02, 2021
On Thursday, 2 December 2021 at 17:36:18 UTC, H. S. Teoh wrote:
> The solution is very simple: extern(C), extern(C++), and extern anything
> except extern(D) really, should be @system, and extern(D) (the default)
> should be @safe.
>
> The real issue is convincing Walter to accept this.

Yes - this seems like the most reasonable solution to me.  I don't see why it would be particularly controversial.