public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Harris Snyder <hsnyder@structura.bio>
To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [PATCH, Fortran] Bug fix in ISO_Fortran_binding - unsigned char arrays
Date: Tue, 12 Jan 2021 14:20:19 -0500	[thread overview]
Message-ID: <CAFWzjBLmfKNx-g_Yrn8MJzu4P-aM_5+8oemFJCj2_CC_vW0azw@mail.gmail.com> (raw)
In-Reply-To: <CAFWzjB+e3pfVXFRUUPboWHRd9jOPFCHkn2+TTkU-NtEXLVg+VQ@mail.gmail.com>

Hi everyone,

In a previous email thread (subject: Possible bug re:
ISO_Fortran_binding.h) it was determined that there was a bug in
ISO_Fortran_binding.c. When attempting to use the ISO Fortran binding
from C to create an array of type uint8_t or signed char, a crash
would result unless the element size was manually specified as 1 when
calling CFI_establish. The F2018 standard indicates that signed char /
uint8_t should be an integer type and therefore explicitly specifying
the element size should not be required. The attached patch fixes the
issue (test case included).

OK for master? I don't have write access so I will need someone else
to commit this for me if possible.

Thanks,
Harris Snyder


Fixes a bug in ISO_Fortran_binding.c whereby signed char or uint8_t
arrays would cause crashes unless an element size is specified.

libgfortran/ChangeLog:

    * runtime/ISO_Fortran_binding.c (CFI_establish): fixed signed char arrays.

gcc/testsuite/ChangeLog:

    * gfortran.dg/iso_fortran_binding_uint8_array.f90: New test.
    * gfortran.dg/iso_fortran_binding_uint8_array_driver.c: New test.


diff --git a/gcc/testsuite/gfortran.dg/iso_fortran_binding_uint8_array.f90
b/gcc/testsuite/gfortran.dg/iso_fortran_binding_uint8_array.f90
new file mode 100644
index 00000000000..bf85bf52949
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/iso_fortran_binding_uint8_array.f90
@@ -0,0 +1,11 @@
+! { dg-do run }
+! { dg-additional-sources iso_fortran_binding_uint8_array_driver.c }
+
+module m
+   use iso_c_binding
+contains
+   subroutine fsub( x ) bind(C, name="fsub")
+      integer(c_int8_t), intent(inout) :: x(:)
+      x = x+1
+   end subroutine
+end module
diff --git a/gcc/testsuite/gfortran.dg/iso_fortran_binding_uint8_array_driver.c
b/gcc/testsuite/gfortran.dg/iso_fortran_binding_uint8_array_driver.c
new file mode 100644
index 00000000000..84ed127d525
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/iso_fortran_binding_uint8_array_driver.c
@@ -0,0 +1,25 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include "ISO_Fortran_binding.h"
+
+extern void fsub(CFI_cdesc_t *);
+
+int main(void)
+{
+   int8_t x[] = {1,2,3,4};
+   int N = sizeof(x)/sizeof(x[0]);
+
+   CFI_CDESC_T(1) dat;
+   CFI_index_t ext[1];
+   ext[0] = (CFI_index_t)N;
+   int rc = CFI_establish((CFI_cdesc_t *)&dat, &x, CFI_attribute_other,
+                      CFI_type_int8_t, 0, (CFI_rank_t)1, ext);
+   printf("CFI_establish call returned: %d\n", rc);
+
+   fsub((CFI_cdesc_t *)&dat );
+
+   for (int i=0; i<N; i++)
+       printf("%"PRId8"\n", x[i]);
+   return 0;
+}
diff --git a/libgfortran/runtime/ISO_Fortran_binding.c
b/libgfortran/runtime/ISO_Fortran_binding.c
index 86ff25afb23..3746ec1c681 100644
--- a/libgfortran/runtime/ISO_Fortran_binding.c
+++ b/libgfortran/runtime/ISO_Fortran_binding.c
@@ -345,8 +345,7 @@ int CFI_establish (CFI_cdesc_t *dv, void
*base_addr, CFI_attribute_t attribute,
   dv->base_addr = base_addr;

   if (type == CFI_type_char || type == CFI_type_ucs4_char ||
-      type == CFI_type_signed_char || type == CFI_type_struct ||
-      type == CFI_type_other)
+      type == CFI_type_struct || type == CFI_type_other)
     dv->elem_len = elem_len;
   else
     {

  parent reply	other threads:[~2021-01-12 19:20 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-10 22:17 Possible bug re: ISO_Fortran_binding.h Harris Snyder
2021-01-11 11:24 ` Tobias Burnus
2021-01-11 15:42   ` Harris Snyder
2021-01-11 16:21     ` Tobias Burnus
2021-01-11 18:18       ` Harris Snyder
2021-01-12 14:30         ` Tobias Burnus
2021-01-12 19:20         ` Harris Snyder [this message]
2021-01-13  6:34           ` Bug fix in ISO_Fortran_binding - unsigned char arrays Harris Snyder
2021-01-13 16:02             ` [PATCH, Fortran] PR fortran/93524 - ISO_Fortran_binding signed " Harris Snyder
2021-01-14  3:42               ` Jerry DeLisle

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=CAFWzjBLmfKNx-g_Yrn8MJzu4P-aM_5+8oemFJCj2_CC_vW0azw@mail.gmail.com \
    --to=hsnyder@structura.bio \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@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).