So, I am writing a Fortran code and I wanted to get the full path of the input file and I wrote the following code with c interface
C code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void realpathFunc(char * argc, char * output, int * n){
realpath(argc, output);
*n = strlen(output);
}
Fortran code:
module sysUtils
!! Routines for different system utilities
use iso_c_binding, only : c_char, c_int
use iso_fortran_env, only: error_unit
private :: error_unit
interface
subroutine getFullPath_s(rPath, aPath, n) bind(c, name='realpathFunc')
!! Resolve absolute path from a relative path
import :: c_char, c_int
character(kind=c_char) :: rPath(*), aPath(*)
integer(kind=c_int) :: n
end subroutine
end interface
contains
character(len=200) function getFullPath(relativePath) result(actualPath)
!! Resolve absolute path from a relative path
character(len=*) :: relativePath
integer :: n
call getFullPath_s(relativePath, actualPath, n)
actualPath(n+1:) = ''
end
end module
Main code:
program main
use sysUtils
print *, getFullPath('myFile')
end
When I execute this with gfortran
( gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)
) with flags -g -Wall -Wextra -Wconversion -g -fbacktrace -fbounds-check -ffpe-trap=zero,overflow,underflow -fsanitize=address,undefined
, the following error is thrown
#0 0x1553e213e86c (/lib/x86_64-linux-gnu/libasan.so.5+0x8086c)
#1 0x55ef197ac2e3 in realpathFunc (/folder/a.out+0x22e3)
#2 0x55ef197ac3b8 in __sysutils_MOD_getfullpath /folder/utils.f90:23
#3 0x55ef197ac840 in MAIN__ /folder/main.f90:5
#4 0x55ef197ac99a in main /folder/main.f90:2
#5 0x1553e12be0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
#6 0x55ef197ac1fd in _start (/folder/a.out+0x21fd)
0x55ef197ad2a2 is located 0 bytes to the right of global variable '*.LC2' defined in 'main.f90' (0x55ef197ad2a0) of size 2
0x55ef197ad2a2 is located 62 bytes to the left of global variable 'options.2' defined in 'main.f90:2' (0x55ef197ad2e0) of size 28
SUMMARY: AddressSanitizer: global-buffer-overflow (/lib/x86_64-linux-gnu/libasan.so.5+0x8086c)
Shadow bytes around the buggy address:
0x0abe632eda00: 00 00 00 00 00 02 f9 f9 f9 f9 f9 f9 00 00 00 00
0x0abe632eda10: 00 00 00 00 00 00 00 00 00 04 f9 f9 f9 f9 f9 f9
0x0abe632eda20: 00 00 00 05 f9 f9 f9 f9 f9 f9 f9 f9 00 00 00 00
0x0abe632eda30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0abe632eda40: 00 00 00 00 00 00 00 00 00 00 00 00 00 01 f9 f9
=>0x0abe632eda50: f9 f9 f9 f9[02]f9 f9 f9 f9 f9 f9 f9 00 00 00 04
0x0abe632eda60: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
0x0abe632eda70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0abe632eda80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0abe632eda90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0abe632edaa0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==918480==ABORTING
Can anyone tell me what am I doing wrong here and how do I solve this error.