This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Bug with ISO_C_BINDING on Mac OS?


Hi all,

attached are two small test cases (Fortran & C), which try to open a
bz2 file for reading (they require a, possibly empty, file 'test.bz2'
in the same dir). The Fortran version uses ISO_C_BINDING interfaces
for the calls to 'fopen' and 'BZ2_bzReadOpen', and both work as
expected on Linux (x86_64):

$ gcc-4.9 testBz2.c -lbz2
$ ./a.out
#1
#2
#3

$ gfortran-4.9 testBz2.f90 -lbz2
$ ./a.out
 #1
 #2
 #3

(Also with other compilers btw. The Fortran code has been reduced from
a larger code which successfully reads bz2 files in this way.)

On Mac OS (x86_64-apple-darwin11.4.2), the C version still works,
while the Fortran version produces a segfault in the call to
BZ2_bzReadOpen:

$ ./a.out
 #1
 #2

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.


Valgrind shows:

 #1
 #2
==1773== Invalid read of size 2
==1773==    at 0x100354455: ferror (in /usr/lib/system/libsystem_c.dylib)
==1773==    by 0x100026774: BZ2_bzReadOpen (in /usr/lib/libbz2.1.0.dylib)
==1773==    by 0x100000DD7: MAIN__ (testBz2.f90:38)
==1773==    by 0x100000E7A: main (testBz2.f90:42)
==1773==  Address 0x3fdef0 is not stack'd, malloc'd or (recently) free'd
==1773==
==1773==
==1773== Process terminating with default action of signal 11 (SIGSEGV)
==1773==  General Protection Fault
==1773==    at 0x1002D46CD:
misaligned_stack_error_entering_dyld_stub_binder (in
/usr/lib/system/libdyld.dylib)
==1773==    by 0x100148027: ??? (in
/usr/local/lib/gcc/x86_64-apple-darwin11.4.2/4.9.0/libgfortran.3.dylib)
==1773==    by 0x100026774: BZ2_bzReadOpen (in /usr/lib/libbz2.1.0.dylib)
==1773==    by 0x100000DD7: MAIN__ (testBz2.f90:38)
==1773==    by 0x100000E7A: main (testBz2.f90:42)


Can anyone give me a hint what is going on, or how I can debug it? Is
this a gfortran bug with ISO_C_BINDING on Mac OS or some other
problem? Are my ISO_C_BINDING interfaces correct (as mentioned, they
work fine on Linux)? Can anyone reproduce the segfault on Mac OS?

Cheers,
Janus
#include <stdio.h>
#include <stdlib.h>

#include <bzlib.h>

int main(void)
{
  FILE * fp;
  BZFILE *bzFp;
  int bzError;

  printf("#1\n");

  fp = fopen ("test.bz2", "r");

  if (fp <= 0)
    {
      printf("Error opening file!\n");
      return 1;
    }

  printf("#2\n");

  bzFp = BZ2_bzReadOpen (&bzError, fp, 0, 0, NULL, 0);

  printf("#3\n");

  return 0;
}

Attachment: testBz2.f90
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]