public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: Sandra Loosemore <sandra@codesourcery.com>,
	"fortran@gcc.gnu.org" <fortran@gcc.gnu.org>,
	"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH, Fortran] [PR libfortran/101317] Bind(c): Improve error checking in CFI_* functions
Date: Thu, 22 Jul 2021 09:54:15 +0200	[thread overview]
Message-ID: <cfaac873-5ba9-d8ec-bcbb-55ad4bebc55e@codesourcery.com> (raw)
In-Reply-To: <1fd11074-9c32-37a6-a957-3fe3e329eb9e@codesourcery.com>

Hi Sandra,

On 21.07.21 20:01, Sandra Loosemore wrote:
> Hmmm. CFI_establish explicitly says that the elem_len has to be
> greater than zero.  It seems somewhat confusing that it's inconsistent
> with the other functions that take an elem_len argument.

Congratulation – we have found a bug in the spec, which is also
present in the current draft (21-007). I have now written to J3:
https://mailman.j3-fortran.org/pipermail/j3/2021-July/013189.html

> Ha! I noticed the same thing and already posted a separate patch for
> that.  :-P
> https://gcc.gnu.org/pipermail/fortran/2021-July/056243.html
:-)
>> How about PRIiPTR + ptrdiff_t instead of %d + (int) cast? At least as
>> positive value, extent may exceed INT_MAX.
> Hmmm, there are similar problems in existing code in other functions
> in this file (e.g., CFI_section).

I think that you could fix as well. At least for size(array), it is not
uncommon that this exceeds MAX_INT.

On the other hand, I think it is unlikely to occur for a single
dimension (→ extent). In particular, the most likely way to get a
negative value is doing 'int' calculations with an overflow – and then
assigning the result "array->dim[i].extent". But in that case, that
(possibly negative) value fits into an int by construction.

>>> +      if (source->attribute == CFI_attribute_other
>>> +          && source->rank > 0
>>> +          && source->dim[source->rank - 1].extent == -1)
>>> +        {
>>> +          fprintf (stderr, "CFI_setpointer: The source is a "
>>> +               "nonallocatable nonpointer object that is an "
>>> +               "assumed-size array.\n");
>>
>> I think you could just check for assumed rank – without
>> CFI_attribute_other in the 'if' and 'nonallocatable nonpointer' in
>> the error message. Only nonallocatable nonpointer variables can be of
>> assumed size (in Fortran); I think that makes the message simpler
>> (focusing on the issue) and if the C user passes an
>> allocatable/pointer, which is assumed rank, it is also a bug.
>
> The wording of the message reflects the language of the standard:
> "source shall be a null pointer or the address of a C descriptor for
> an allocated allocatable object, a data pointer object, or a
> nonallocatable nonpointer data object that is not an assumed-size array.

I know – but the wording is such that it permits all 'nonallocatable
nonpointer data object' – with one exception.

This does not mean that 'assumed-size array' is only invalid for
'nonallocatable nonpointer' – it is also invalid for
allocatables/pointers. The latter cannot occur for Fortran code as only
deferred-shape arrays are permitted in that case, but from the C side,
you can easily set it to the wrong value.

Thus, by simplifying the wording, the error message is clearer (directly
pointing to the issue) and it additionally catches another wrong use of
the array descriptor, which can be (only) triggered from C.

Tobias

-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

  reply	other threads:[~2021-07-22  7:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-17  0:49 Sandra Loosemore
2021-07-21 17:26 ` Tobias Burnus
2021-07-21 18:01   ` Sandra Loosemore
2021-07-22  7:54     ` Tobias Burnus [this message]
2021-07-25  4:11       ` [PATCH v2, " Sandra Loosemore
2021-07-26  7:35         ` Tobias Burnus

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=cfaac873-5ba9-d8ec-bcbb-55ad4bebc55e@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=sandra@codesourcery.com \
    /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).