From: "Sławomir Lach" <slawek@lach.art.pl>
To: gcc@gcc.gnu.org
Subject: can-be-null can-not-be-null break-instruction for better handling pointers.
Date: Thu, 27 Apr 2023 17:54:52 +0200 [thread overview]
Message-ID: <13261922.uLZWGnKmhe@localhost.localdomain> (raw)
I am not C expert, so be polity. I do not see something similar in C world,
but similar techniques in other languages, such like Vala.
I suggest to create two new pointer type:
1. can-be-null
2. cannot-be-null
(You must find other words to describe it behavior).
First enforces to compiler checking it is not null, when it was used (by ->
operator) or used, when cannot-be-null is excepted. Of course, user can cast
to cannot-be-null to avoid checking. So:
1. Usage with -> operator, or * must be placed inside conditional block, with
contains null check in condition
2. Passing as cannot-be-null (except explicit conversion) will requires to put
in block as above
The same restrictions apply to put normal pointer in case, when cannot-be-null
excepted.
Additionally, break-instruction word will be reserved for functions, so exit
will be traded as end of control-flow and further code will be traded as
checked.
Imagine assert will be traded as break-ins
This code:
int get_vector_size(struct vector cannot-be-null *vec)
{
assert(vec != NULL)
return vec->size;
}
Will be correct, because assert macro will (possible) be extended to:
if (!(vec != NULL)) {
puts("assertion error: vec != NULL");
exit(1);
}
And exit will be break-instruction.
We could use cannot-be-null in some cases, such like:
int year;
scanf("How old are you:%d", &year);
But user could pass NULL as second parameter.
When pointers in scanf parameters are cannot-be-null, the compiler will
disallow pass null by us.
Of course - inside scanf must check if each parameter are not NULL.
Another code:
void error(const char *message)
{
perror(message);
}
And perror will have cannot-be-null word describe one's parameter.
This will cause compile-time-error, because message was passed to function,
which parameter is cannot-be-null.
Programmer should do:
void error(const char *message)
{
if (NULL != message)
perror(message);
}
What do you think. This will solve a lot of problems,
It could be also great to introduce function checking if any of it parameter
are null, but I do not known how to make it good-quality (simple in usage).
Programer should propably pass number of passed parameters, or type of
parameter before each.
next reply other threads:[~2023-04-27 15:54 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-27 15:54 Sławomir Lach [this message]
2023-04-28 21:34 ` Christopher Bazley
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=13261922.uLZWGnKmhe@localhost.localdomain \
--to=slawek@lach.art.pl \
--cc=gcc@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).