-
-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
H5fget_name_f len_trim fix #826
Conversation
… problems if the passed buffer is freshly allocated or contains trailing whitespace).
e28f000
to
0f07e8b
Compare
Please provide testing code that shows the problem before the fix and passes after the fix. |
… whitespace. With the buffer populated with whitespace the test will pass with the current code, but would fail without the fix implemented in commit 0f07e8b.
…ame was returned from h5fget_name_f.
Here's some example code that fails on the current development branch and passes once this PR is applied:
I also added some changes to this PR that modify tH5F.F90 so that the test subroutine |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is the majority of Fortran users expect the trailing spaces to be trimmed if they are creating a character string that is larger than the actual filename, having a filename with trailing white space is not common. This also mimics Fortran standard OPEN, which also does not necessarily create a filename with trailing whitespace.
A better solution would be to add an optional len parameter for the filename. But that is not needed because a user can use C_NULL_CHAR to preserve the trailing white space.
CHARACTER(*), PARAMETER::expected_filename = "test.h5 "//C_NULL_CHAR
C has a different behavior because the null terminator determines the string size.
I see the issue, I had read the issue as trailing white space in the filename. Let me look into it. |
@@ -844,7 +844,7 @@ INTEGER FUNCTION h5fget_name_c(obj_id, size, buf, buflen) & | |||
CHARACTER(KIND=C_CHAR), DIMENSION(*), INTENT(OUT) :: buf | |||
END FUNCTION h5fget_name_c | |||
END INTERFACE | |||
buflen = LEN_TRIM(buf) | |||
buflen = LEN(buf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, change the declaration (line 827):
CHARACTER(LEN=*), INTENT(OUT) :: buf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in new PR.
fortran/test/tH5F.F90
Outdated
write(*,*) "file name obtained from the dataset id is incorrect" | ||
call check("h5fget_name_f",-1,total_error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave in the old test and add a new test:
CALL h5dopen_f(reopen_id, dsetname, dset_id, error)
CALL check("h5dopen_f",error,total_error)
!
!Get file name from the dataset identifier
!
! Use an uninitialized buffer
CALL h5fget_name_f(dset_id, file_name, name_size, error)
CALL check("h5fget_name_f",error,total_error)
IF(name_size .NE. LEN_TRIM(fix_filename))THEN
WRITE(*,*) " file name size obtained from the dataset id is incorrect"
total_error = total_error + 1
ENDIF
IF(file_name(1:name_size) .NE. TRIM(fix_filename)) THEN
WRITE(*,*) " file name obtained from the dataset id is incorrect"
total_error = total_error + 1
END IF
! Use an initialized buffer
file_name(:) = " "
CALL h5fget_name_f(dset_id, file_name, name_size, error)
CALL check("h5fget_name_f",error,total_error)
IF(name_size .NE. LEN_TRIM(fix_filename))THEN
WRITE(*,*) " file name size obtained from the dataset id is incorrect"
total_error = total_error + 1
ENDIF
IF(file_name(1:name_size) .NE. TRIM(fix_filename)) THEN
WRITE(*,*) " file name obtained from the dataset id is incorrect"
total_error = total_error + 1
END IF
!
!Read the dataset.
!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The size should always be correct, just the filename would be wrong since the buffer size given was zero.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave in the old test and add a new test
I pushed some changes reverting my edits to reopentest and adding a new test focusing on h5fget_name_f.
…needed for checking filenames returned from h5fget_name_f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jhaiduce, should I make the changes and issue a new PR?
! | ||
!Create dataset "/dset" inside the file . | ||
! | ||
CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, dataspace, & |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest simplifying this by just opening the root group "/" and using the group id instead of going through the trouble of creating a dataset.
call h5gopen_f(file_id,"/",g_id,ierr)
Sounds good to me, thanks! |
Reworked PR, new PR #972 |
* H5Fget_name_f fixed to handle correctly trailing whitespaces and newly allocated buffers. Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Scot Breitenfeld <brtnfld@hdfgroup.org> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
* H5Fget_name_f fixed to handle correctly trailing whitespaces and newly allocated buffers. Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Scot Breitenfeld <brtnfld@hdfgroup.org> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
* H5Fget_name_f fixed to handle correctly trailing whitespaces and newly allocated buffers. Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Scot Breitenfeld <brtnfld@hdfgroup.org> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
h5fget_name_f
was calling the intrinsiclen_trim
to determine the length of the passed buffer. This can cause problems if the passed buffer contains trailing whitespace (in which case the h5fget_name_f will limit itself to the length of the non-whitespace characters currently in the passed buffer, even if the buffer's capacity is larger), or if the passed buffer was freshly allocated (in which case the resulting buffer length is unpredictable).The pull request replaces the call to
len_trim
with a call tolen
, which returns the total buffer length regardless of the passed contents of the buffer, and is safe to call on a freshly allocated (i.e. unassigned) buffer.This fixes #825.