Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The Wright Bothers weren't the first to fly. They were just the first not to crash.


devel / comp.lang.c / Is C ready to become a safer language?

SubjectAuthor
* Is C ready to become a safer language?Thiago Adams
+* Re: Is C ready to become a safer language?Lawrence D'Oliveiro
|+- Re: Is C ready to become a safer language?Kaz Kylheku
|`* Re: Is C ready to become a safer language?Thiago Adams
| +* Re: Is C ready to become a safer language?bart
| |`* Re: Is C ready to become a safer language?Thiago Adams
| | +- Re: Is C ready to become a safer language?Thiago Adams
| | `- Re: Is C ready to become a safer language?Keith Thompson
| `* Re: Is C ready to become a safer language?Keith Thompson
|  `- Re: Is C ready to become a safer language?Thiago Adams
+* Re: Is C ready to become a safer language?Kaz Kylheku
|`- Re: Is C ready to become a safer language?Keith Thompson
+* Re: Is C ready to become a safer language?Keith Thompson
|+* Re: Is C ready to become a safer language?David Brown
||+* Re: Is C ready to become a safer language?Thiago Adams
|||+- Re: Is C ready to become a safer language?Malcolm McLean
|||`* Re: Is C ready to become a safer language?David Brown
||| `* Re: Is C ready to become a safer language?Thiago Adams
|||  `- Re: Is C ready to become a safer language?David Brown
||`* Re: Is C ready to become a safer language?Keith Thompson
|| +* Re: Is C ready to become a safer language?Malcolm McLean
|| |+- Re: Is C ready to become a safer language?Keith Thompson
|| |`* Re: Is C ready to become a safer language?David Brown
|| | `* Re: Is C ready to become a safer language?Malcolm McLean
|| |  +* Re: Is C ready to become a safer language?David Brown
|| |  |+* Re: Is C ready to become a safer language?Malcolm McLean
|| |  ||`- Re: Is C ready to become a safer language?David Brown
|| |  |`* Re: Is C ready to become a safer language?Ben Bacarisse
|| |  | +* Re: Is C ready to become a safer language?Malcolm McLean
|| |  | |+* Re: Is C ready to become a safer language?Ben Bacarisse
|| |  | ||`- Re: Is C ready to become a safer language?Malcolm McLean
|| |  | |`* Re: Is C ready to become a safer language?David Brown
|| |  | | `* ustent on that.Malcolm McLean
|| |  | |  `* Re: ustent on that.David Brown
|| |  | |   `- Re: ustent on that.Malcolm McLean
|| |  | `* Re: Is C ready to become a safer language?bart
|| |  |  `* Re: Is C ready to become a safer language?Ben Bacarisse
|| |  |   `- Re: Is C ready to become a safer language?David Brown
|| |  `- Re: Is C ready to become a safer language?Keith Thompson
|| `* Re: Is C ready to become a safer language?David Brown
||  `* Re: Is C ready to become a safer language?Keith Thompson
||   `- Re: Is C ready to become a safer language?David Brown
|`* Re: Is C ready to become a safer language?Tim Rentsch
| `* Re: Is C ready to become a safer language?Keith Thompson
|  `* Re: Is C ready to become a safer language?Tim Rentsch
|   `* Re: Is C ready to become a safer language?Keith Thompson
|    `* Re: Is C ready to become a safer language?Tim Rentsch
|     `* Re: Is C ready to become a safer language?Keith Thompson
|      `- Re: Is C ready to become a safer language?Tim Rentsch
+* Re: Is C ready to become a safer language?Richard Kettlewell
|`- Re: Is C ready to become a safer language?Thiago Adams
`* Re: Is C ready to become a safer language?bart
 `* Re: Is C ready to become a safer language?Tim Rentsch
  `* Re: Is C ready to become a safer language?bart
   +- Re: Is C ready to become a safer language?Richard Harnden
   +* Re: Is C ready to become a safer language?Kaz Kylheku
   |+* Re: Is C ready to become a safer language?Thiago Adams
   ||`* Re: Is C ready to become a safer language?David Brown
   || `* Re: Is C ready to become a safer language?Thiago Adams
   ||  +- Re: Is C ready to become a safer language?Keith Thompson
   ||  `- Re: Is C ready to become a safer language?David Brown
   |`* Re: Is C ready to become a safer language?bart
   | +- Re: Is C ready to become a safer language?Keith Thompson
   | +* Re: Is C ready to become a safer language?Kaz Kylheku
   | |`* Re: Is C ready to become a safer language?bart
   | | +- Re: Is C ready to become a safer language?David Brown
   | | `- Re: Is C ready to become a safer language?Kaz Kylheku
   | `- Re: Is C ready to become a safer language?David Brown
   +* Re: Is C ready to become a safer language?Keith Thompson
   |`* Re: Is C ready to become a safer language?Thiago Adams
   | `- Re: Is C ready to become a safer language?Keith Thompson
   +* Re: Is C ready to become a safer language?vallor
   |+- Re: Is C ready to become a safer language?bart
   |+* Re: Is C ready to become a safer language?Keith Thompson
   ||`- Re: Is C ready to become a safer language?David Brown
   |+* Re: Is C ready to become a safer language?Tim Rentsch
   ||`- Re: Is C ready to become a safer language?vallor
   |`* Re: Is C ready to become a safer language?Malcolm McLean
   | `* Re: Is C ready to become a safer language?Richard Harnden
   |  `* Re: Is C ready to become a safer language?bart
   |   `* Re: Is C ready to become a safer language?Richard Harnden
   |    `- Re: Is C ready to become a safer language?bart
   `- Re: Is C ready to become a safer language?Tim Rentsch

Pages:1234
Is C ready to become a safer language?

<uq1jnk$1qebo$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32832&group=comp.lang.c#32832

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 01:01:56 -0300
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <uq1jnk$1qebo$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 8 Feb 2024 04:01:56 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ab8463c8bce658e2d4a0c767e684ebc";
logging-data="1915256"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rLoqECgZasWJhwQMhlOX7iqm0NKn+uTc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/1VbY3jivCIH3KfuAPBfiCsAb+Y=
Content-Language: en-GB
 by: Thiago Adams - Thu, 8 Feb 2024 04:01 UTC

Let's say C compilers can detect all sorts of bugs at compile time.

How would C compilers report that? As an error or a warning?

Let's use this sample:

int main() {
int a = 1;
a = a / 0;
}

GCC says:

warning: division by zero is undefined [-Wdivision-by-zero]
5 | a = a / 0;
| ^ ~

In case GCC or any other compiler reports this as an error, then C
programmers would likely complain. Am I right?

So, even if we had compile-time checks for bugs, C compilers and the
standard are not prepared to handle the implications to make C a safer
language.

From my point of view, we need an error, not a warning. But we also
need a way to ignore the error in case the programmer wants to see what
happens, with a division by zero, for instance. (Please note that this
topic IS NOT about this specific warning; it is just a sample.)

Warnings work more or less like this. The problem with warnings is that
they are not standardized - the "name/number" and the way to
disable/enable them.

So this is the first problem we need to solve in C to make the language
safer. We need a mechanism.

I also believe we can have a standard profile for warnings that will
change the compiler behaviour; for instance, making undefined behaviour
an error.

The C standard is complacent with the lack of error messages. Sometimes
it says "message is encouraged...". This shifts the responsibility from
the language. But when someone says "C is dangerous," the language as a
whole is blamed, regardless of whether you are using MISRA, for instance.

Thus, not only are the mechanics of the language is unprepared, but the
standard is also not prepared to assume the responsibility of being a
source of guidance and safety.

Re: Is C ready to become a safer language?

<uq1lvp$1qvlk$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32833&group=comp.lang.c#32833

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 04:40:25 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <uq1lvp$1qvlk$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 04:40:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5894b09826b61230d924acc4bad71243";
logging-data="1932980"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18udypINUBpoCiQN5bYt2bv"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:UCdeC0k1VwwCNMvDj65WxMzX2RA=
 by: Lawrence D'Oliv - Thu, 8 Feb 2024 04:40 UTC

On Thu, 8 Feb 2024 01:01:56 -0300, Thiago Adams wrote:

> So, even if we had compile-time checks for bugs, C compilers and the
> standard are not prepared to handle the implications to make C a safer
> language.

Do you want C to turn into Java? In Java, rules about reachability are
built into the language. And these rules are simplistic ones, based on
the state of the art back in the 1990s, not taking account of
improvements in compiler technology since then. For example, in the
following code, the uninitialized declaration of “Result” is a
compile-time error, even though a human looking at the code can figure
out that there is no way it will be left uninitialized at the point of
reference:

char Result; /* not allowed! */
boolean LastWasBackslash = false;
for (;;)
{
++ColNr;
final int ich = Input.read();
if (ich < 0)
{
--ColNr;
EOF = true;
Result = '\n';
break;
}
else if (LastWasCR && (char)ich == '\012')
{
/* skip LF following CR */
--ColNr;
LastWasCR = false;
LastWasBackslash = false;
}
else if (!LastWasBackslash && (char)ich == '\\')
{
LastWasBackslash = true;
LastWasCR = false;
}
else if (!LastWasBackslash || !IsEOL((char)ich))
{
Result = (char)ich;
break;
}
else
{
++LineNr;
ColNr = 0;
LastWasCR = (char)ich == '\015';
LastWasBackslash = false;
} /*if*/
} /*for*/
EOL = EOF || IsEOL(Result);
LastWasCR = Result == '\015';

Re: Is C ready to become a safer language?

<20240207201520.547@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32835&group=comp.lang.c#32835

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 04:58:27 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 141
Message-ID: <20240207201520.547@kylheku.com>
References: <uq1jnk$1qebo$2@dont-email.me>
Injection-Date: Thu, 8 Feb 2024 04:58:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="163263dc92baed57fd9aed84cf1a8c15";
logging-data="1938453"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/c8JkOYi5F4OH6MIKw5jjjNV2s3Lm2u1A="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Y/yrFCtDMpcpwqjQkly0x/GiJFo=
 by: Kaz Kylheku - Thu, 8 Feb 2024 04:58 UTC

On 2024-02-08, Thiago Adams <thiago.adams@gmail.com> wrote:
> Let's say C compilers can detect all sorts of bugs at compile time.
>
> How would C compilers report that? As an error or a warning?

ISO C doesn't distinguish "error" from "warning"; it speaks only about
diagnostics. It requires diagnostics for certain situations.

In compiler (not ISO C) parlance, we might understand "error" to be a
situation when a diagnostic is issued, and the implementation terminates
the translation, so that a translated program or translation unit is not
produced.

A "warning" is any other situation when a diagnostic is issued and
translation continues.

(These concepts extend into run-time. There could be a run-time diagnostic
which doesn't terminate the program, and one which does.)

With these definitions ...

> Let's use this sample:
>
> int main() {
> int a = 1;
> a = a / 0;
> }
>
> GCC says:
>
> warning: division by zero is undefined [-Wdivision-by-zero]
> 5 | a = a / 0;
> | ^ ~
> In case GCC or any other compiler reports this as an error, then C
> programmers would likely complain. Am I right?

They might.

A programmer taking a standard-based view might remark that
the program is not required to be diagnosed by ISO C.

It does invoke undefined behavior if executed, though.

Based on the deduction that the program has unconditional undefined
behavior, it is legitimate to terminate translating the program with a
diagnostic, since that is a possible consequence of undefined behavior.

> So, even if we had compile-time checks for bugs, C compilers and the
> standard are not prepared to handle the implications to make C a safer
> language.
>
> From my point of view, we need an error, not a warning.

Implementations are free to implement arbitrary diagnostics, and also
nonconforming modes of translation, which stop translating a program
that does not violate any ISO C syntax or constraint rule.

Thus compilers can provide tools to help programmers enforce
rules that don't exist in the language as such.

> But we also
> need a way to ignore the error in case the programmer wants to see what
> happens, with a division by zero, for instance. (Please note that this
> topic IS NOT about this specific warning; it is just a sample.)
>
> Warnings work more or less like this. The problem with warnings is that
> they are not standardized - the "name/number" and the way to
> disable/enable them.
>
> So this is the first problem we need to solve in C to make the language
> safer. We need a mechanism.
>
> I also believe we can have a standard profile for warnings that will
> change the compiler behaviour; for instance, making undefined behaviour
> an error.
>
> The C standard is complacent with the lack of error messages. Sometimes
> it says "message is encouraged...". This shifts the responsibility from

I don't believe so. The C standard absolutely requires diagnostics
for certain situations. Everywhere else, it doesn't.

I don't remember seeing any text "encouraging" a diagnostic.
That's ambiguous: is it really required or not?

> the language. But when someone says "C is dangerous," the language as a
> whole is blamed, regardless of whether you are using MISRA, for instance.
>
> Thus, not only are the mechanics of the language is unprepared, but the
> standard is also not prepared to assume the responsibility of being a
> source of guidance and safety.

The standard specifies which constructs are required to do what under
what conditions, and those situations are safe.

Sometimes "do what" is unspecified (the implementation choices from a
range of safe behaviors) or implementation-defined (similar, but
implementation also documents the choice).

The standard also specifies that some situations must be diagnosed,
like syntax and type errors and other constraint rule violations.

Everything else is undefined behavior (some of it potentially
defined by the implementation as a "documented extension").

Avoidance of undefined behavior is left to the careful design
of the program, and whatever tools the implementation and third parties
provide for detecting undefined behavior.

Some languages, like Common Lisp, provide a notion of safety level.
Over individual expressions in Lisp, we can declare optimization
parameters: safety (0-3) and speed (0-3). We can also declare facts to
the Common Lisp compiler like types of operands and results. When we
lie to the compiler, the behavior becomes undefined, but the situation
is nuanced. Safe code diagnoses errors. If we tell the Lisp compiler
that some X is a cons cell, and then access (car X), but at run time, X
turns out to be a string, an error will be signaled. However, if we
compile the code with safety 0, all bets are off: the machine code may
blindly access the string object as if it were a cons cell, with
disastrous consequences.

C could benefit from an approach along these lines. The big problem is
that in C it's hard to impossible to make many undefined behaviors
safe (as in detect them and abort the program).

For isntance, there is no way to tell whether a pointer is valid
or not, or how large an array it points to.

Lisp is a safe, dynamic language first, and an aggressively optimized
language second. It's easy to tell that (car X) is accessing a string
and not a cons cell thanks to the run-time information in the objects.

It's easier to strip away safety from a safe language, and generate
unsafe code that works with lower level machine types, than to introduce
safety into a machine-oriented language, because the data
representations don't accomodate the needed run-time bits.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Is C ready to become a safer language?

<87jznfh7p6.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32836&group=comp.lang.c#32836

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!news.szaf.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Wed, 07 Feb 2024 20:59:49 -0800
Organization: None to speak of
Lines: 54
Message-ID: <87jznfh7p6.fsf@nosuchdomain.example.com>
References: <uq1jnk$1qebo$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="93a22936c47851e9e74d3b604d07c7bb";
logging-data="1936751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HsBL8rv+nCnVUFJSwfiYM"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:mbEBEsfjxUV9QYmIV0BHrcLoGsk=
sha1:5QZzAxEb5oL17hLsKSAVdYNfYpA=
 by: Keith Thompson - Thu, 8 Feb 2024 04:59 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
> Let's say C compilers can detect all sorts of bugs at compile time.
>
> How would C compilers report that? As an error or a warning?
>
> Let's use this sample:
>
> int main() {
> int a = 1;
> a = a / 0;
> }
>
> GCC says:
>
> warning: division by zero is undefined [-Wdivision-by-zero]
> 5 | a = a / 0;
> | ^ ~
>
> In case GCC or any other compiler reports this as an error, then C
> programmers would likely complain. Am I right?

Someone will always complain, but a conforming compiler can report this
as a fatal error.

Division by zero has undefine behavior. Under the standard's definition
of undefined behavior, it says:

NOTE

Possible undefined behavior ranges from ignoring the situation
completely with unpredictable results, to behaving during
translation or program execution in a documented manner
characteristic of the environment (with or without the issuance
of a diagnostic message), to terminating a translation or
execution (with the issuance of a diagnostic message).

Though it's not quite that simple. Rejecting the program if the
compiler can't prove that the division will be executed would IMHO
be non-conforming. Code that's never executed has no behavior, so it
doesn't have undefined behavior.

But of course any compiler can reject anything it likes in
non-conforming mode. See for example "gcc -Werror".

But even ignoring that, a culture of paying very close attention to
non-fatal warnings could go a long way towards making C safer (assuming
compilers are clever enough to issue good warnings).

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Is C ready to become a safer language?

<20240207205903.650@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32837&group=comp.lang.c#32837

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!nntp.comgw.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 05:00:04 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <20240207205903.650@kylheku.com>
References: <uq1jnk$1qebo$2@dont-email.me> <uq1lvp$1qvlk$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 05:00:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="163263dc92baed57fd9aed84cf1a8c15";
logging-data="1938453"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SpFZOWtKLrHtmYGzRv0b+D81Q6sEIOxQ="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:VhP/T2OmCToh82ZMROUtJUPxqW4=
 by: Kaz Kylheku - Thu, 8 Feb 2024 05:00 UTC

On 2024-02-08, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
> On Thu, 8 Feb 2024 01:01:56 -0300, Thiago Adams wrote:
>
>> So, even if we had compile-time checks for bugs, C compilers and the
>> standard are not prepared to handle the implications to make C a safer
>> language.
>
> Do you want C to turn into Java? In Java, rules about reachability are
> built into the language. And these rules are simplistic ones, based on
> the state of the art back in the 1990s, not taking account of
> improvements in compiler technology since then. For example, in the
> following code, the uninitialized declaration of “Result” is a
> compile-time error, even though a human looking at the code can figure
> out that there is no way it will be left uninitialized at the point of
> reference:

Because C doesn't mandate such a warning, GCC was able to rid itself
of naive, badly implemented diagnostics in this area.

I have some old installations of GCC which still warn about some of my
code (that some variable might be used uninitialized). Newer compilers
are silent on that code, due to doing better analysis.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Is C ready to become a safer language?

<87fry3h65t.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32838&group=comp.lang.c#32838

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Wed, 07 Feb 2024 21:33:02 -0800
Organization: None to speak of
Lines: 27
Message-ID: <87fry3h65t.fsf@nosuchdomain.example.com>
References: <uq1jnk$1qebo$2@dont-email.me> <20240207201520.547@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="93a22936c47851e9e74d3b604d07c7bb";
logging-data="1947219"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18C1U5rosyLHCsyY3bNoSzE"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:krVGn5gOloNE4g1hOby5NrTWAP8=
sha1:9miCg3gReWY1R6F/xRo6MPGRuhs=
 by: Keith Thompson - Thu, 8 Feb 2024 05:33 UTC

Kaz Kylheku <433-929-6894@kylheku.com> writes:
> On 2024-02-08, Thiago Adams <thiago.adams@gmail.com> wrote:
[...]
>> The C standard is complacent with the lack of error messages. Sometimes
>> it says "message is encouraged...". This shifts the responsibility from
>
> I don't believe so. The C standard absolutely requires diagnostics
> for certain situations. Everywhere else, it doesn't.
>
> I don't remember seeing any text "encouraging" a diagnostic.
> That's ambiguous: is it really required or not?

Search for "Recommended practice" in the standard. For example,
N1570 6.7.4p9:

Recommended practice

The implementation should produce a diagnostic message for a
function declared with a _Noreturn function specifier that appears
to be capable of returning to its caller.

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Is C ready to become a safer language?

<uq22na$1sppb$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32841&group=comp.lang.c#32841

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 09:17:45 +0100
Organization: A noiseless patient Spider
Lines: 102
Message-ID: <uq22na$1sppb$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 8 Feb 2024 08:17:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a878e37942be30dcab60787af34a441b";
logging-data="1992491"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18DlWcr/rDi/hudiy9nHtBr/4Ytg/7ys/s="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:zPPMSoq8oKRKkld+QLijkOGJ1kw=
In-Reply-To: <87jznfh7p6.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: David Brown - Thu, 8 Feb 2024 08:17 UTC

On 08/02/2024 05:59, Keith Thompson wrote:
> Thiago Adams <thiago.adams@gmail.com> writes:
>> Let's say C compilers can detect all sorts of bugs at compile time.
>>
>> How would C compilers report that? As an error or a warning?
>>
>> Let's use this sample:
>>
>> int main() {
>> int a = 1;
>> a = a / 0;
>> }
>>
>> GCC says:
>>
>> warning: division by zero is undefined [-Wdivision-by-zero]
>> 5 | a = a / 0;
>> | ^ ~
>>
>> In case GCC or any other compiler reports this as an error, then C
>> programmers would likely complain. Am I right?
>
> Someone will always complain, but a conforming compiler can report this
> as a fatal error.
>

I'm not /entirely/ convinced. Such code is only undefined behaviour at
run-time, I believe. A compiler could reject the code (give a fatal
error) if it is sure that this will be reached when the code is run.
But can it be sure of that, even if it is in "main()" ? Freestanding
implementations don't need to run "main()" (not all my programs have had
a "main()" function), and the freestanding/hosted implementation choice
is a matter of the implementation, not just the compiler.

> Division by zero has undefine behavior. Under the standard's definition
> of undefined behavior, it says:
>
> NOTE
>
> Possible undefined behavior ranges from ignoring the situation
> completely with unpredictable results, to behaving during
> translation or program execution in a documented manner
> characteristic of the environment (with or without the issuance
> of a diagnostic message), to terminating a translation or
> execution (with the issuance of a diagnostic message).
>
> Though it's not quite that simple. Rejecting the program if the
> compiler can't prove that the division will be executed would IMHO
> be non-conforming. Code that's never executed has no behavior, so it
> doesn't have undefined behavior.
>

Indeed.

In particular, someone could write :

static inline void undefined_behaviour(void) {
1 / 0;
}

and use that for a "can never happen" indicator for compiler optimisation :

int foo(int x) {
if (x < 0) undefined_behaviour();
// From here on, the compiler can optimise using the
// assumption that x >= 0
...
}

gcc and clang have __builtin_unreachable() for this purpose - "calling"
that function that is always run-time undefined behaviour.

> But of course any compiler can reject anything it likes in
> non-conforming mode. See for example "gcc -Werror".
>

Yes. Once I have a project roughly in shape, I always enable that so
that I don't miss any warnings.

> But even ignoring that, a culture of paying very close attention to
> non-fatal warnings could go a long way towards making C safer (assuming
> compilers are clever enough to issue good warnings).
>

Yes.

I think it would be better if compilers were stricter in their default
modes, even if their stricter compliant modes have to reduce the
severity of some warnings. People should be allowed to write "weird"
code that looks very much like an error - but perhaps it is those people
who should need extra flags or other effort, not those that write
"normal" code and want to catch their bugs. (I know this can't be done
for tools like gcc, because it could cause problems with existing code.)

Still, I am happy to see that the latest gcc trunk has made some pre-C99
features into fatal errors instead of warnings - implicit function
declarations are now fatal errors.

Re: Is C ready to become a safer language?

<uq2gun$1uos6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32855&group=comp.lang.c#32855

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 09:20:39 -0300
Organization: A noiseless patient Spider
Lines: 108
Message-ID: <uq2gun$1uos6$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me> <uq1lvp$1qvlk$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 8 Feb 2024 12:20:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ab8463c8bce658e2d4a0c767e684ebc";
logging-data="2057094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zq2qjZmmOJK5DSU/ugPj1AYNqLWjpBe4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lphH2kYnE8CZWT1udSQpDyHMs1o=
In-Reply-To: <uq1lvp$1qvlk$1@dont-email.me>
Content-Language: en-GB
 by: Thiago Adams - Thu, 8 Feb 2024 12:20 UTC

Em 2/8/2024 1:40 AM, Lawrence D'Oliveiro escreveu:
> On Thu, 8 Feb 2024 01:01:56 -0300, Thiago Adams wrote:
>
>> So, even if we had compile-time checks for bugs, C compilers and the
>> standard are not prepared to handle the implications to make C a safer
>> language.
>
> Do you want C to turn into Java?

No. In this topic I am assuming that the compilers can catch all sorts
of bugs in compile time.
With this assumption in mind, my point is that the standard is not
prepared to handle the implications.

Because:

1 - There is no universal mechanism for enable/disable warnings. Each
compiler does in a different way using pragma, and there is no guidance
in the standard with a rule number for instance.

2 - Because C avoids breaking code, it also needs something like a
warning profile.

3 - Maybe the standard is not prepared to assume the task of central
point of guidance about safety.

Another sample:

int main()
{ int a[2];
a[3] = 1;
}

GCC says

warning: array index 3 is past the end of the array (that has type
'int[2]') [-Warray-bounds]
5 | a[3] = 1;
| ^ ~

One more:

struct X {double d;};
struct Y {int i;};
int main()
{ struct X x;
struct Y *p;
p = &x;
}

GCC says:

warning: incompatible pointer types assigning to 'struct Y *' from
'struct X *' [-Wincompatible-pointer-types]
7 | p = &x;
| ^ ~~

For this one a C++ compiler gives:

<source>:7:6: error: cannot convert 'X*' to 'Y*' in assignment
7 | p = &x;

So, this may be also related with the "spirit of C". I like the idea of
the programmer can do whatever they want.
BUT, I think my default the compiler must complain.

Then I think we could have in the standard a better guidance about
safety defaults, and to avoid breaking code, the standard could define
safety profiles.

I also would like to note that some languages that have one main
implementation like rust does not have this problem..because the
specification is more or less the implementation of one compiler.

This will never be the case of C so I am wondering how the standard
could be written for at same time.

- keep compiling existing code
- give guidance and safety guarantees
- make code safe by default

Without this the C language is abstract about safety.

Consider the question

"Is C language safe?"

The answer will be , well, the language itself is very vague, depends
of the compiler you use.

The other problem is that we may need annotations and maybe other
changes on the type system. C23 now has attributes.

My point is also that safety , cannot be on the compiler side only,
because each compiler can have a different annotations.

Re: Is C ready to become a safer language?

<uq2ijf$1vahb$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32859&group=comp.lang.c#32859

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 09:48:47 -0300
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <uq2ijf$1vahb$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com> <uq22na$1sppb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 12:48:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ab8463c8bce658e2d4a0c767e684ebc";
logging-data="2075179"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vNgDvfgdM7z6UejrYzF+a+gZnbWoRgqs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kjz1n0nTnb4waCUCxjo0rXMytrA=
Content-Language: en-GB
In-Reply-To: <uq22na$1sppb$1@dont-email.me>
 by: Thiago Adams - Thu, 8 Feb 2024 12:48 UTC

Em 2/8/2024 5:17 AM, David Brown escreveu:
....
> I think it would be better if compilers were stricter in their default
> modes, even if their stricter compliant modes have to reduce the
> severity of some warnings.  People should be allowed to write "weird"
> code that looks very much like an error - but perhaps it is those people
> who should need extra flags or other effort, not those that write
> "normal" code and want to catch their bugs.  (I know this can't be done
> for tools like gcc, because it could cause problems with existing code.)

Yes this is my point.
But I believe we need a standard mechanism, and this is the first step
towards safety in C.

Consider this code.

int main(void)
{ int a = 1, b = 2;

#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4706 )
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wparentheses"
#endif

if (a = b){}

#ifdef _MSC_VER
#pragma warning( pop )
#else
#pragma GCC diagnostic pop
#endif
}

This code wants to use a = b inside the if condition.
The code shows how to disable the warning in GCC and MSCV.

If we had in the standard a number for the warning, and a mechanism for
disabling then we could have something like

int main(void)
{ int a = 1, b = 2;

if (a = b) [[disable:4706]]
{
}
}

Maybe

int main(void)
{ int a = 1, b = 2;

if (a = b) _ignore("I want to assign here...")
{
}
}

That is applied to any warning on that specific line.
The advantage is the warning ID is not necessary

Re: Is C ready to become a safer language?

<uq2p8e$20iub$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32865&group=comp.lang.c#32865

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 14:42:22 +0000
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <uq2p8e$20iub$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com> <uq22na$1sppb$1@dont-email.me>
<uq2ijf$1vahb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 14:42:22 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6f5fed1333f3a87d8fe9a7695eb9a9cf";
logging-data="2116555"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19O376RUl7j2wvJm7SanPTTEarzlyZNvGE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:wtkOVJqsJLGBH5vFZV08vkRbTXU=
In-Reply-To: <uq2ijf$1vahb$1@dont-email.me>
Content-Language: en-GB
 by: Malcolm McLean - Thu, 8 Feb 2024 14:42 UTC

On 08/02/2024 12:48, Thiago Adams wrote:
> Em 2/8/2024 5:17 AM, David Brown escreveu:
> ...
>> I think it would be better if compilers were stricter in their default
>> modes, even if their stricter compliant modes have to reduce the
>> severity of some warnings.  People should be allowed to write "weird"
>> code that looks very much like an error - but perhaps it is those
>> people who should need extra flags or other effort, not those that
>> write "normal" code and want to catch their bugs.  (I know this can't
>> be done for tools like gcc, because it could cause problems with
>> existing code.)
>
>
> Yes this is my point.
> But I believe we need a standard mechanism, and this is the first step
> towards safety in C.
>
> Consider this code.
>
> int main(void)
> {
>     int a = 1, b = 2;
>
>     #ifdef _MSC_VER
>       #pragma warning( push )
>       #pragma warning( disable : 4706 )
>     #else
>       #pragma GCC diagnostic push
>       #pragma GCC diagnostic ignored "-Wparentheses"
>     #endif
>
>     if (a = b){}
>
>    #ifdef _MSC_VER
>       #pragma warning( pop )
>     #else
>       #pragma GCC diagnostic pop
>     #endif
> }
>
> This code wants to use a = b inside the if condition.
> The code shows how to disable the warning in GCC and MSCV.
>
> If we had in the standard a number for the warning, and a mechanism for
> disabling then we could have something like
>
> int main(void)
> {
>     int a = 1, b = 2;
>
>     if (a = b) [[disable:4706]]
>     {
>     }
> }
>
> Maybe
>
> int main(void)
> {
>     int a = 1, b = 2;
>
>     if (a = b) _ignore("I want to assign here...")
>     {
>     }
> }
>
> That is applied to any warning on that specific line.
> The advantage is the warning ID is not necessary
>
>
>
Which is a huge advantage. The first proposal creates something which is
a complete mystery to anyone who doesn't intimately know what has been
done. The second anyone can easily understand.
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: Is C ready to become a safer language?

<uq2rps$213ok$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32866&group=comp.lang.c#32866

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 16:25:48 +0100
Organization: A noiseless patient Spider
Lines: 88
Message-ID: <uq2rps$213ok$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com> <uq22na$1sppb$1@dont-email.me>
<uq2ijf$1vahb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 15:25:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a878e37942be30dcab60787af34a441b";
logging-data="2133780"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+R/twRm13y2ajwcN040Y7ZhvcoLR4gdmk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:GY8qoLN8J8uxCx/ECoFd9X6cA9s=
In-Reply-To: <uq2ijf$1vahb$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Thu, 8 Feb 2024 15:25 UTC

On 08/02/2024 13:48, Thiago Adams wrote:
> Em 2/8/2024 5:17 AM, David Brown escreveu:
> ...
>> I think it would be better if compilers were stricter in their default
>> modes, even if their stricter compliant modes have to reduce the
>> severity of some warnings.  People should be allowed to write "weird"
>> code that looks very much like an error - but perhaps it is those
>> people who should need extra flags or other effort, not those that
>> write "normal" code and want to catch their bugs.  (I know this can't
>> be done for tools like gcc, because it could cause problems with
>> existing code.)
>
>
> Yes this is my point.
> But I believe we need a standard mechanism, and this is the first step
> towards safety in C.
>
> Consider this code.
>
> int main(void)
> {
>     int a = 1, b = 2;
>
>     #ifdef _MSC_VER
>       #pragma warning( push )
>       #pragma warning( disable : 4706 )
>     #else
>       #pragma GCC diagnostic push
>       #pragma GCC diagnostic ignored "-Wparentheses"
>     #endif
>
>     if (a = b){}
>
>    #ifdef _MSC_VER
>       #pragma warning( pop )
>     #else
>       #pragma GCC diagnostic pop
>     #endif
> }
>
> This code wants to use a = b inside the if condition.
> The code shows how to disable the warning in GCC and MSCV.
>
> If we had in the standard a number for the warning, and a mechanism for
> disabling then we could have something like

Standard numbers would be a /really/ bad idea - standard names would be
vastly better. There are already a couple of attributes that
standardise manipulation of warnings - [[nodiscard]] and
[[maybe_unused]]. But that syntax is not scalable. Perhaps :

[[ignored(parentheses)]]
[[warn(parentheses)]]
[[error(parentheses)]]

>
> int main(void)
> {
>     int a = 1, b = 2;
>
>     if (a = b) [[disable:4706]]
>     {
>     }
> }
>
> Maybe
>
> int main(void)
> {
>     int a = 1, b = 2;
>
>     if (a = b) _ignore("I want to assign here...")
>     {
>     }
> }
>
> That is applied to any warning on that specific line.
> The advantage is the warning ID is not necessary
>

The disadvantage then is that it affects all warnings, not the ones you
know are safe to ignore. And anything that applies to a specific line
is a non-starter for C - you would have to attach it to a statement or
other syntactic unit.

Re: Is C ready to become a safer language?

<wwv7cjfotl5.fsf@LkoBDZeT.terraraq.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32868&group=comp.lang.c#32868

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.furie.org.uk!nntp.terraraq.uk!.POSTED.tunnel.sfere.anjou.terraraq.org.uk!not-for-mail
From: inva...@invalid.invalid (Richard Kettlewell)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 08 Feb 2024 15:37:26 +0000
Organization: terraraq NNTP server
Message-ID: <wwv7cjfotl5.fsf@LkoBDZeT.terraraq.uk>
References: <uq1jnk$1qebo$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: innmantic.terraraq.uk; posting-host="tunnel.sfere.anjou.terraraq.org.uk:172.17.207.6";
logging-data="7068"; mail-complaints-to="usenet@innmantic.terraraq.uk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:nDSucoGUqbGSOK/1sJHyX9du1Cc=
X-Face: h[Hh-7npe<<b4/eW[]sat,I3O`t8A`(ej.H!F4\8|;ih)`7{@:A~/j1}gTt4e7-n*F?.Rl^
F<\{jehn7.KrO{!7=:(@J~]<.[{>v9!1<qZY,{EJxg6?Er4Y7Ng2\Ft>Z&W?r\c.!4DXH5PWpga"ha
+r0NzP?vnz:e/knOY)PI-
X-Boydie: NO
 by: Richard Kettlewell - Thu, 8 Feb 2024 15:37 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
> From my point of view, we need an error, not a warning. But we also
> need a way to ignore the error in case the programmer wants to see
> what happens, with a division by zero, for instance. (Please note that
> this topic IS NOT about this specific warning; it is just a sample.)

All this already exists. Compilers warn about many possible or actual
problems (such as your example) and the warnings can be selectively or
globally turned into errors. For example see:
https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Warning-Options.html#index-Werror

> Warnings work more or less like this. The problem with warnings is
> that they are not standardized - the "name/number" and the way to
> disable/enable them.

Having to configure warnings and errors separately for each compiler in
a multi-platform environment is a tiny cost compared to the cost of
developing and maintaining cross-platform source code, multiple build
platforms, multiple test platforms, etc. So I don’t think there’s much
benefit to be hand from standardization here. Historically the addition
of useful warning options to compilers has outpaced the development of
the C standard in any case.

> So this is the first problem we need to solve in C to make the
> language safer. We need a mechanism.
>
> I also believe we can have a standard profile for warnings that will
> change the compiler behaviour; for instance, making undefined
> behaviour an error.

This can’t be done completely at compile time, at least not without an
unacceptably high level of false positives.

Tools do exist to detect undefined behavior at runtime and terminate the
program, though. A couple of examples are:

https://clang.llvm.org/docs/MemorySanitizer.html
https://clang.llvm.org/docs/AddressSanitizer.html

However there are severe limitations:
* Coverage is only partial.
* Performance is impacted.
* Some of the tools are unsuited to production environments, see e.g.
https://www.openwall.com/lists/oss-security/2016/02/17/9

You could also look at (partial) hardware-based responses to the
undefined behavior problem such as
https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/

--
https://www.greenend.org.uk/rjk/

Re: Is C ready to become a safer language?

<87bk8rgcy0.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32870&group=comp.lang.c#32870

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 08 Feb 2024 08:04:07 -0800
Organization: None to speak of
Lines: 47
Message-ID: <87bk8rgcy0.fsf@nosuchdomain.example.com>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com>
<uq22na$1sppb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="93a22936c47851e9e74d3b604d07c7bb";
logging-data="2144023"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+YiznOImAONWDxeFS1ZjdX"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:uCOt4froIbVwDLel6wuJntb1rts=
sha1:fMNAlpOzSnsSJmUbXrwAWQlSXD0=
 by: Keith Thompson - Thu, 8 Feb 2024 16:04 UTC

David Brown <david.brown@hesbynett.no> writes:
> On 08/02/2024 05:59, Keith Thompson wrote:
>> Thiago Adams <thiago.adams@gmail.com> writes:
>>> Let's say C compilers can detect all sorts of bugs at compile time.
>>>
>>> How would C compilers report that? As an error or a warning?
>>>
>>> Let's use this sample:
>>>
>>> int main() {
>>> int a = 1;
>>> a = a / 0;
>>> }
>>>
>>> GCC says:
>>>
>>> warning: division by zero is undefined [-Wdivision-by-zero]
>>> 5 | a = a / 0;
>>> | ^ ~
>>>
>>> In case GCC or any other compiler reports this as an error, then C
>>> programmers would likely complain. Am I right?
>> Someone will always complain, but a conforming compiler can report
>> this as a fatal error.
>
> I'm not /entirely/ convinced. Such code is only undefined behaviour
> at run-time, I believe. A compiler could reject the code (give a
> fatal error) if it is sure that this will be reached when the code is
> run. But can it be sure of that, even if it is in "main()" ?
> Freestanding implementations don't need to run "main()" (not all my
> programs have had a "main()" function), and the freestanding/hosted
> implementation choice is a matter of the implementation, not just the
> compiler.

In a freestanding implementation, main() *might* be just another
function. In that case a compiler can't prove that the code will be
invoked.

I was assuming a hosted implementation -- and the compiler knows whether
its implementation is hosted or freestanding.

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Is C ready to become a safer language?

<uq2um0$21dgh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32872&group=comp.lang.c#32872

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 16:14:56 +0000
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <uq2um0$21dgh$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com> <uq22na$1sppb$1@dont-email.me>
<87bk8rgcy0.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 8 Feb 2024 16:14:56 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6f5fed1333f3a87d8fe9a7695eb9a9cf";
logging-data="2143761"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1E0tHVTFSr8krND98i4jOTC5jVUXjmsA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:FC9pJR9C6dHFw9uIoPeITrZn0K4=
In-Reply-To: <87bk8rgcy0.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: Malcolm McLean - Thu, 8 Feb 2024 16:14 UTC

On 08/02/2024 16:04, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 08/02/2024 05:59, Keith Thompson wrote:
>>> Thiago Adams <thiago.adams@gmail.com> writes:
>>>> Let's say C compilers can detect all sorts of bugs at compile time.
>>>>
>>>> How would C compilers report that? As an error or a warning?
>>>>
>>>> Let's use this sample:
>>>>
>>>> int main() {
>>>> int a = 1;
>>>> a = a / 0;
>>>> }
>>>>
>>>> GCC says:
>>>>
>>>> warning: division by zero is undefined [-Wdivision-by-zero]
>>>> 5 | a = a / 0;
>>>> | ^ ~
>>>>
>>>> In case GCC or any other compiler reports this as an error, then C
>>>> programmers would likely complain. Am I right?
>>> Someone will always complain, but a conforming compiler can report
>>> this as a fatal error.
>>
>> I'm not /entirely/ convinced. Such code is only undefined behaviour
>> at run-time, I believe. A compiler could reject the code (give a
>> fatal error) if it is sure that this will be reached when the code is
>> run. But can it be sure of that, even if it is in "main()" ?
>> Freestanding implementations don't need to run "main()" (not all my
>> programs have had a "main()" function), and the freestanding/hosted
>> implementation choice is a matter of the implementation, not just the
>> compiler.
>
> In a freestanding implementation, main() *might* be just another
> function. In that case a compiler can't prove that the code will be
> invoked.
>
Well sometimes it can. The boot routine or program entry point is by
defintion always invoked, and you can generally prove that at least some
code is always reached from that. However it is the halting problem, and
you can never prove for all cases, even if the code must be reached or
not reached regardless of runtime inputs.

--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: Is C ready to become a safer language?

<uq2ume$21hts$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32873&group=comp.lang.c#32873

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 13:15:10 -0300
Organization: A noiseless patient Spider
Lines: 105
Message-ID: <uq2ume$21hts$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com> <uq22na$1sppb$1@dont-email.me>
<uq2ijf$1vahb$1@dont-email.me> <uq2rps$213ok$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 16:15:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ab8463c8bce658e2d4a0c767e684ebc";
logging-data="2148284"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lMsgm9DY5evDaNLZwTaYaCvzZl68Iks8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:uUYqAeSLL8BclV6nFXAE/fy7TVo=
Content-Language: en-GB
In-Reply-To: <uq2rps$213ok$1@dont-email.me>
 by: Thiago Adams - Thu, 8 Feb 2024 16:15 UTC

Em 2/8/2024 12:25 PM, David Brown escreveu:
> On 08/02/2024 13:48, Thiago Adams wrote:
>> Em 2/8/2024 5:17 AM, David Brown escreveu:
>> ...
>>> I think it would be better if compilers were stricter in their
>>> default modes, even if their stricter compliant modes have to reduce
>>> the severity of some warnings.  People should be allowed to write
>>> "weird" code that looks very much like an error - but perhaps it is
>>> those people who should need extra flags or other effort, not those
>>> that write "normal" code and want to catch their bugs.  (I know this
>>> can't be done for tools like gcc, because it could cause problems
>>> with existing code.)
>>
>>
>> Yes this is my point.
>> But I believe we need a standard mechanism, and this is the first step
>> towards safety in C.
>>
>> Consider this code.
>>
>> int main(void)
>> {
>>      int a = 1, b = 2;
>>
>>      #ifdef _MSC_VER
>>        #pragma warning( push )
>>        #pragma warning( disable : 4706 )
>>      #else
>>        #pragma GCC diagnostic push
>>        #pragma GCC diagnostic ignored "-Wparentheses"
>>      #endif
>>
>>      if (a = b){}
>>
>>     #ifdef _MSC_VER
>>        #pragma warning( pop )
>>      #else
>>        #pragma GCC diagnostic pop
>>      #endif
>> }
>>
>> This code wants to use a = b inside the if condition.
>> The code shows how to disable the warning in GCC and MSCV.
>>
>> If we had in the standard a number for the warning, and a mechanism
>> for disabling then we could have something like
>
> Standard numbers would be a /really/ bad idea - standard names would be
> vastly better.  There are already a couple of attributes that
> standardise manipulation of warnings - [[nodiscard]] and
> [[maybe_unused]].  But that syntax is not scalable.  Perhaps :
>
>     [[ignored(parentheses)]]
>     [[warn(parentheses)]]
>     [[error(parentheses)]]
>
>
>>
>> int main(void)
>> {
>>      int a = 1, b = 2;
>>
>>      if (a = b) [[disable:4706]]
>>      {
>>      }
>> }
>>
>> Maybe
>>
>> int main(void)
>> {
>>      int a = 1, b = 2;
>>
>>      if (a = b) _ignore("I want to assign here...")
>>      {
>>      }
>> }
>>
>> That is applied to any warning on that specific line.
>> The advantage is the warning ID is not necessary
>>
>
> The disadvantage then is that it affects all warnings, not the ones you
> know are safe to ignore.  And anything that applies to a specific line
> is a non-starter for C - you would have to attach it to a statement or
> other syntactic unit.
>
>
>

I was having a look at C# specification. It uses pragma.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#nullable-context

Sample
#pragma warning disable 414, CS3021
#pragma warning restore CS3021

It also have warning numbers, I am not sure if other C# compilers uses
the same warnings "id".

Re: Is C ready to become a safer language?

<uq2v9i$21hts$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32874&group=comp.lang.c#32874

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 13:25:22 -0300
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <uq2v9i$21hts$2@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
<wwv7cjfotl5.fsf@LkoBDZeT.terraraq.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 16:25:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ab8463c8bce658e2d4a0c767e684ebc";
logging-data="2148284"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/ZOwpgL1h38e9fbw+mzUtkxBIwhEuCI4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:JovGb1xBPxXGZqX+XfI47DsPqvw=
In-Reply-To: <wwv7cjfotl5.fsf@LkoBDZeT.terraraq.uk>
Content-Language: pt-BR, en-GB
 by: Thiago Adams - Thu, 8 Feb 2024 16:25 UTC

Em 2/8/2024 12:37 PM, Richard Kettlewell escreveu:
> Thiago Adams <thiago.adams@gmail.com> writes:
>> From my point of view, we need an error, not a warning. But we also
>> need a way to ignore the error in case the programmer wants to see
>> what happens, with a division by zero, for instance. (Please note that
>> this topic IS NOT about this specific warning; it is just a sample.)
>
> All this already exists. Compilers warn about many possible or actual
> problems (such as your example) and the warnings can be selectively or
> globally turned into errors. For example see:
> https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Warning-Options.html#index-Werror

It is not standard making safety not portable.

>> Warnings work more or less like this. The problem with warnings is
>> that they are not standardized - the "name/number" and the way to
>> disable/enable them.
>
> Having to configure warnings and errors separately for each compiler in
> a multi-platform environment is a tiny cost compared to the cost of
> developing and maintaining cross-platform source code, multiple build
> platforms, multiple test platforms, etc. So I don’t think there’s much
> benefit to be hand from standardization here. Historically the addition
> of useful warning options to compilers has outpaced the development of
> the C standard in any case.

See my sample comparing GCC and MSVC pragma to disable warnings.
I believe that code can be improved.
>> So this is the first problem we need to solve in C to make the
>> language safer. We need a mechanism.
>>
>> I also believe we can have a standard profile for warnings that will
>> change the compiler behaviour; for instance, making undefined
>> behaviour an error.
>
> This can’t be done completely at compile time, at least not without an
> unacceptably high level of false positives.

For now I am just assuming it can, them we can focus on the mechanism of
control.
But since you pointed the problems.. going further, and comparing with
Rust for instance, the static analysis will require flow analysis.
Imagine to have to specify how flow analysis should behave in all
compilers? This is a problem that Rust does not have.
Basically it is an specification for static analysis not only the
language. It could be an separated document.
What are the benefits? I believe C language as a whole would pass a new
and important message of safety.

Re: Is C ready to become a safer language?

<uq2vjp$21qev$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32876&group=comp.lang.c#32876

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 16:30:49 +0000
Organization: A noiseless patient Spider
Lines: 107
Message-ID: <uq2vjp$21qev$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 16:30:49 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="467e1f63ec7a511011c681490b409044";
logging-data="2157023"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gIctgISYk/QTFqgZUdk9pj4qcaqjc0Oo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:QfWtqd13n1DkLdVXjoXr00ahCdk=
In-Reply-To: <uq1jnk$1qebo$2@dont-email.me>
Content-Language: en-GB
 by: bart - Thu, 8 Feb 2024 16:30 UTC

On 08/02/2024 04:01, Thiago Adams wrote:
>
> Let's say C compilers can detect all sorts of bugs at compile time.
>
> How would C compilers report that? As an error or a warning?
>
> Let's use this sample:
>
> int main() {
>     int a = 1;
>     a = a / 0;
> }
>
> GCC says:
>
> warning: division by zero is undefined [-Wdivision-by-zero]
>     5 |  a = a / 0;
>       |        ^ ~
>
> In case GCC or any other compiler reports this as an error, then C
> programmers would likely complain. Am I right?
>
> So, even if we had compile-time checks for bugs, C compilers and the
> standard are not prepared to handle the implications to make C a safer
> language.
>
> From my point of view, we need an error, not a warning. But we also
> need a way to ignore the error in case the programmer wants to see what
> happens, with a division by zero, for instance.

Replace the 0 with a variable containing zero at runtime.

(Please note that this
> topic IS NOT about this specific warning; it is just a sample.)
>
> Warnings work more or less like this. The problem with warnings is that
> they are not standardized - the "name/number" and the way to
> disable/enable them.
>
> So this is the first problem we need to solve in C to make the language
> safer. We need a mechanism.
>
> I also believe we can have a standard profile for warnings that will
> change the compiler behaviour; for instance, making undefined behaviour
> an error.
>
> The C standard is complacent with the lack of error messages. Sometimes
> it says "message is encouraged...". This shifts the responsibility from
> the language. But when someone says "C is dangerous," the language as a
> whole is blamed, regardless of whether you are using MISRA, for instance.
>
> Thus, not only are the mechanics of the language is unprepared, but the
> standard is also not prepared to assume the responsibility of being a
> source of guidance and safety.

This is something which has long been of fascination to me: how exactly
do you get a C compiler to actually fail a program with a hard error
when there is obviously something wrong, while not also failing on
completely harmless matters.

So, taking gcc as an example, the same program may:

* Pass, and generate a runnable binary

* Warn, and generate a runnable binary still

* Fail with a hard error.

But the latter is unusual; you might get it with a syntax error for example.

The compiler-behaviour yet get depends on the options that are supplied,
which in turn depends on the programmer: THEY get to choose whether a
program passes or not, something you that you might expect to be the
compiler's job!

The compiler power-users here will have their own set-ups that define
which classes of programs will pass or fail. But I think compilers
should do that out of the box.

--------------------------------------------------

c:\c>type c.c
int main(void) {
int a, b;
a=b/0;
}

c:\c>tcc c.c

c:\c>gcc c.c
c.c: In function 'main':
c.c:3:5: warning: division by zero [-Wdiv-by-zero]
3 | a=b/0;
| ^

c:\c>mcc c
Compiling c.c to c.exe
Proc: main
MCL Error: Divide by zero Line:3 in:c.c

Re: Is C ready to become a safer language?

<877cjehq6n.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32878&group=comp.lang.c#32878

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 08 Feb 2024 08:32:48 -0800
Organization: None to speak of
Lines: 26
Message-ID: <877cjehq6n.fsf@nosuchdomain.example.com>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com>
<uq22na$1sppb$1@dont-email.me>
<87bk8rgcy0.fsf@nosuchdomain.example.com>
<uq2um0$21dgh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="93a22936c47851e9e74d3b604d07c7bb";
logging-data="2144023"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/dZWdES4atVtJiA3R2iWi"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:8PrdCct4uyttNpe3UlU22EesYrI=
sha1:mhCX734TH6l1IS88ZonM7EdPKGI=
 by: Keith Thompson - Thu, 8 Feb 2024 16:32 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> On 08/02/2024 16:04, Keith Thompson wrote:
[...]
>> In a freestanding implementation, main() *might* be just another
>> function. In that case a compiler can't prove that the code will be
>> invoked.
>>
> Well sometimes it can. The boot routine or program entry point is by
> defintion always invoked, and you can generally prove that at least
> some code is always reached from that.

That's not the case in the example being discussed, which was an
unconditional division by zero in main().

> However it is the halting
> problem, and you can never prove for all cases, even if the code must
> be reached or not reached regardless of runtime inputs.

The halting problem is hardly relevant. A compiler *may* reject a
program if it can prove that undefined behavior will always occur.
It's under no obligation to do so, or even to diagnose it.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Is C ready to become a safer language?

<uq31fe$22580$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32880&group=comp.lang.c#32880

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 17:02:39 +0000
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <uq31fe$22580$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me> <uq1lvp$1qvlk$1@dont-email.me>
<uq2gun$1uos6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 17:02:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="467e1f63ec7a511011c681490b409044";
logging-data="2168064"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+IrK/VsCfBwrcALi8xRw60HmrKko4tZ2Y="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:mawjdiYYiwuFwJ+AAvLVgF4xULM=
In-Reply-To: <uq2gun$1uos6$1@dont-email.me>
Content-Language: en-GB
 by: bart - Thu, 8 Feb 2024 17:02 UTC

On 08/02/2024 12:20, Thiago Adams wrote:

> One more:
>
> struct X {double d;};
> struct Y {int i;};
> int main()
> {
>  struct X x;
>  struct Y *p;
>  p = &x;
> }
>
> GCC says:
>
> warning: incompatible pointer types assigning to 'struct Y *' from
> 'struct X *' [-Wincompatible-pointer-types]
>     7 |  p = &x;
>       |    ^ ~~
>
> For this one a C++ compiler gives:
>
> <source>:7:6: error: cannot convert 'X*' to 'Y*' in assignment
>     7 |  p = &x;
>
>
> So, this may be also related with the "spirit of C". I like the idea of
> the programmer can do whatever they want.

They can, they just have to write it like this:

p = (struct Y *)&x;

or, with a common extension:

p = (typeof(p))&x;

(or, in my language, it can be just 'p := cast(&x)'. Without a cast, it

> Consider the question
>
> "Is C language safe?"
>
> The answer  will be , well, the language itself is very vague, depends
> of the compiler you use.

And the options you provide that define the dialect of C and its strictness.

But even if compilers were strict by default, too many things in C are
unsafe, but still legal. That's due to the language design which is not
going to change.

Re: Is C ready to become a safer language?

<uq32c5$222ao$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32886&group=comp.lang.c#32886

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 14:17:57 -0300
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <uq32c5$222ao$1@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me> <uq1lvp$1qvlk$1@dont-email.me>
<uq2gun$1uos6$1@dont-email.me> <uq31fe$22580$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 17:17:57 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ab8463c8bce658e2d4a0c767e684ebc";
logging-data="2165080"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188fPGVPAAzgNwLYrJdc5UH/O/wM0cFTy4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:eH4JCEcy6WozVswJwSXDfS9Y2lE=
In-Reply-To: <uq31fe$22580$1@dont-email.me>
Content-Language: en-GB
 by: Thiago Adams - Thu, 8 Feb 2024 17:17 UTC

Em 2/8/2024 2:02 PM, bart escreveu:
> On 08/02/2024 12:20, Thiago Adams wrote:
>
>> One more:
>>
>> struct X {double d;};
>> struct Y {int i;};
>> int main()
>> {
>>   struct X x;
>>   struct Y *p;
>>   p = &x;
>> }
>>
>> GCC says:
>>
>> warning: incompatible pointer types assigning to 'struct Y *' from
>> 'struct X *' [-Wincompatible-pointer-types]
>>      7 |  p = &x;
>>        |    ^ ~~
>>
>> For this one a C++ compiler gives:
>>
>> <source>:7:6: error: cannot convert 'X*' to 'Y*' in assignment
>>      7 |  p = &x;
>>
>>
>> So, this may be also related with the "spirit of C". I like the idea
>> of the programmer can do whatever they want.
>
> They can, they just have to write it like this:
>
>    p = (struct Y *)&x;
>
> or, with a common extension:
>
>    p = (typeof(p))&x;
>
> (or, in my language, it can be just 'p := cast(&x)'. Without a cast, it

Here casts are a very good solution. Another sample is when mixing enuns
types.

>> Consider the question
>>
>> "Is C language safe?"
>>
>> The answer  will be , well, the language itself is very vague, depends
>> of the compiler you use.
>
>
> And the options you provide that define the dialect of C and its
> strictness.
>
> But even if compilers were strict by default, too many things in C are
> unsafe, but still legal. That's due to the language design which is not
> going to change.
>

This depends on how strict you want to be.

Another sample

void f(int a[]){
a[2] = 1;
}

I wish this code would not compile if in strict mode. Because it
requires inter-procedural analysis.

But with this change:

void f(int a[3]){
a[2] = 1;
} it should compile.
Here is similar of cast, the language already have the mechanism to talk
with the static analyser.

With the progress of static analyses we need more. If we just leave the
problem for static analysers to solve then safety will not be portable.
Each one will have it own warning, annotations etc.

What is is the point of C standard?
To make the code portable? But not the code guarantees portable?

Re: Is C ready to become a safer language?

<uq32fl$222ao$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32887&group=comp.lang.c#32887

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 14:19:49 -0300
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <uq32fl$222ao$2@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me> <uq1lvp$1qvlk$1@dont-email.me>
<uq2gun$1uos6$1@dont-email.me> <uq31fe$22580$1@dont-email.me>
<uq32c5$222ao$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 8 Feb 2024 17:19:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ab8463c8bce658e2d4a0c767e684ebc";
logging-data="2165080"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188Y+gYNOqRu1f7DeZpiVtzcpC9B6z0+kM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:t+a5XgmN59NdY443DVSTdL4YEWs=
In-Reply-To: <uq32c5$222ao$1@dont-email.me>
Content-Language: en-GB
 by: Thiago Adams - Thu, 8 Feb 2024 17:19 UTC

Em 2/8/2024 2:17 PM, Thiago Adams escreveu:
....
> Another sample
>
> void f(int a[]){
>   a[2] = 1;
> }
>
> I wish this code would not compile if in strict mode. Because it
> requires inter-procedural analysis.

The other reason is because the implementation may not be available.
But the interface of the function allow check at each caller.

Re: Is C ready to become a safer language?

<87v86yg88i.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32890&group=comp.lang.c#32890

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 08 Feb 2024 09:45:49 -0800
Organization: None to speak of
Lines: 43
Message-ID: <87v86yg88i.fsf@nosuchdomain.example.com>
References: <uq1jnk$1qebo$2@dont-email.me> <uq1lvp$1qvlk$1@dont-email.me>
<uq2gun$1uos6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="93a22936c47851e9e74d3b604d07c7bb";
logging-data="2180864"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18uakjiqwXjuW0kLTjXKgsn"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:mEUk+E9M+7c7yZRwLng7M1ydaqs=
sha1:b6fFtaUNOzQz9iAqgU45x/BUIJc=
 by: Keith Thompson - Thu, 8 Feb 2024 17:45 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
[...]
> One more:
>
> struct X {double d;};
> struct Y {int i;};
> int main()
> {
> struct X x;
> struct Y *p;
> p = &x;
> }
>
> GCC says:
>
> warning: incompatible pointer types assigning to 'struct Y *' from
> 'struct X *' [-Wincompatible-pointer-types]
> 7 | p = &x;
> | ^ ~~
>
> For this one a C++ compiler gives:
>
> <source>:7:6: error: cannot convert 'X*' to 'Y*' in assignment
> 7 | p = &x;
>
>
> So, this may be also related with the "spirit of C". I like the idea
> of the programmer can do whatever they want.
> BUT, I think my default the compiler must complain.

That's simply a constraint violation; there is no implicit conversion
from struct X* to struct Y*. A conforming compiler must diagnose it
(and *may* treat it as a fatal error). (I believe the C++ rules are
equivalent. g++ just happens to be more strict than gcc in this case.)

gcc rejects it with "-pedantic-errors".

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Is C ready to become a safer language?

<uq34l4$222ao$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32892&group=comp.lang.c#32892

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 14:56:52 -0300
Organization: A noiseless patient Spider
Lines: 107
Message-ID: <uq34l4$222ao$3@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me> <uq1lvp$1qvlk$1@dont-email.me>
<uq2gun$1uos6$1@dont-email.me> <87v86yg88i.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 8 Feb 2024 17:56:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9ab8463c8bce658e2d4a0c767e684ebc";
logging-data="2165080"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2w3REkI2uPl2zClSy//l9DKvbzbwLqY4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:URDtA+w8bhr5gG79dPUd5t2i1hE=
Content-Language: en-GB
In-Reply-To: <87v86yg88i.fsf@nosuchdomain.example.com>
 by: Thiago Adams - Thu, 8 Feb 2024 17:56 UTC

Em 2/8/2024 2:45 PM, Keith Thompson escreveu:
> Thiago Adams <thiago.adams@gmail.com> writes:
> [...]
>> One more:
>>
>> struct X {double d;};
>> struct Y {int i;};
>> int main()
>> {
>> struct X x;
>> struct Y *p;
>> p = &x;
>> }
>>
>> GCC says:
>>
>> warning: incompatible pointer types assigning to 'struct Y *' from
>> 'struct X *' [-Wincompatible-pointer-types]
>> 7 | p = &x;
>> | ^ ~~
>>
>> For this one a C++ compiler gives:
>>
>> <source>:7:6: error: cannot convert 'X*' to 'Y*' in assignment
>> 7 | p = &x;
>>
>>
>> So, this may be also related with the "spirit of C". I like the idea
>> of the programmer can do whatever they want.
>> BUT, I think my default the compiler must complain.
>
> That's simply a constraint violation; there is no implicit conversion
> from struct X* to struct Y*. A conforming compiler must diagnose it
> (and *may* treat it as a fatal error). (I believe the C++ rules are
> equivalent. g++ just happens to be more strict than gcc in this case.)
>
> gcc rejects it with "-pedantic-errors".

This sample shows a different mind set between C and C++ compilers.
I am suggesting a a "safe mindset" for C, that is basically a profile
for warnings.

Another one.

int * f() {
int i = 0;
return &i;
}

int main(){
int* p = f();
}

<source>:3:12: warning: function returns address of local variable
[-Wreturn-local-addr]
3 | return &i;
| ^~

The mindset is "well.. this may be an error..."
I wish a new mindset

"this code seems wrong and I will not compile this until you explicitly
annotate why you need this"

The idea that C programmers can do whatever they want still valid, but
not by default in a safer profile.

Another one

enum E1{A};
enum E2{B};
int main(){
if (A == B){}
}

<source>:4:9: warning: comparison between 'enum E1' and 'enum E2'
[-Wenum-compare]
4 | if (A == B){}

In C++ it is an warning as well.

enum E1{A};
enum E2{B};
int main(){
if (A == B){}
E1 a = B; //error in C++
}

enum E1{A};
enum E2{B};
int main(){
if (A == B){}
enum E1 a = B; //OK in C
}

(I know in C enumerators are int,
but static analysis can handle that internally)

Re: Is C ready to become a safer language?

<87r0hmg7p7.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32893&group=comp.lang.c#32893

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 08 Feb 2024 09:57:24 -0800
Organization: None to speak of
Lines: 55
Message-ID: <87r0hmg7p7.fsf@nosuchdomain.example.com>
References: <uq1jnk$1qebo$2@dont-email.me> <uq1lvp$1qvlk$1@dont-email.me>
<uq2gun$1uos6$1@dont-email.me> <uq31fe$22580$1@dont-email.me>
<uq32c5$222ao$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="93a22936c47851e9e74d3b604d07c7bb";
logging-data="2180864"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+pYdsjNFP4kVJfhq1UN6f"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:Sm+7XOFgBqz8dpTEr/XUP2z7WlU=
sha1:DZvJ+Hj/+Xfm0nbC4vOisBtRyow=
 by: Keith Thompson - Thu, 8 Feb 2024 17:57 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
[...]
> Here casts are a very good solution. Another sample is when mixing
> enuns types.

No casts are required for assignments between different enum types, or
between enums and integers. Stricter enums can be a nice feature, but C
doesn't have them. (C++ does.)

[...]

> This depends on how strict you want to be.
>
> Another sample
>
> void f(int a[]){
> a[2] = 1;
> }
>
> I wish this code would not compile if in strict mode. Because it
> requires inter-procedural analysis.

I think you mean either that you wish C allowed that sample to be
rejected or that your C compiler had the ability to reject it. In fact
it's valid C code (which *might* have undefined behavior depending on
what's passed by the caller).

> But with this change:
>
> void f(int a[3]){
> a[2] = 1;
> }
> it should compile.

Certainly. But all three of these:
void f(int a[])
void f(int a[3])
void f(int *a)

are exactly equivalent according to the C standard. (The fact that the
3 is ignored in the second example is IMHO unfortunate.)

A compiler can take notice of the [3] for the purpose of enabling or
disabling warnings, but there's no such requirement. And since
void f(int a[static 3])
can be used to assert that a points to the initial element of an array
with at least 3 elements, special handling for
void f(int a[3])
seems unlikely.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Is C ready to become a safer language?

<uq3eci$247ke$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32898&group=comp.lang.c#32898

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Is C ready to become a safer language?
Date: Thu, 8 Feb 2024 21:42:58 +0100
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <uq3eci$247ke$3@dont-email.me>
References: <uq1jnk$1qebo$2@dont-email.me>
<87jznfh7p6.fsf@nosuchdomain.example.com> <uq22na$1sppb$1@dont-email.me>
<uq2ijf$1vahb$1@dont-email.me> <uq2rps$213ok$1@dont-email.me>
<uq2ume$21hts$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 8 Feb 2024 20:42:58 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4d017ee92fb0812e2bbc769b70d08051";
logging-data="2236046"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nBD5XdVdOI6WnKlFw03b8DVhr4lR513Q="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:rbkLqX1BOKdnjwUHqpIwLqIdsRA=
In-Reply-To: <uq2ume$21hts$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Thu, 8 Feb 2024 20:42 UTC

On 08/02/2024 17:15, Thiago Adams wrote:
> Em 2/8/2024 12:25 PM, David Brown escreveu:
>
> I was having a look at C# specification. It uses pragma.
>
> https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#nullable-context
>
> Sample
> #pragma warning disable 414, CS3021
> #pragma warning restore CS3021
>

gcc and clang both accept #pragmas for controlling warnings. These
could be standardised as an alternative to (or in addition to) attributes.

> It also have warning numbers, I am not sure if other C# compilers uses
> the same warnings "id".
>

No. Warnings are determined by compilers. Well-designed tools use
names, and sometimes there can be compatibility (such as clang
originally copying gcc, then each copying the other for warning flag
names depending on who implements it first). Numbers are not IME at all
consistent - they are a left-over from the days when adding proper error
messages or flag names to a compiler would take too much code space. It
is amateurish that MS has yet to fix this.

Pages:1234
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor