H5Iget_name() inconsistency

Is there an error in the documentation for H5Iget_name(), regarding the
terminal null ? (or the function itself)
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5I.html#Identify-GetName

I am trying to allocate a buffer dynamically by first calling
ssize_t sz = H5Iget_name(obj_id, NULL, 0);

Then I allocate the buffer, then I call
H5Iget_name(obj_id, NULL, sz);

and it chops off the last character. When I look at h5gname.c I see
this: (the variable "len" is later returned as the return value) -- it
looks like there's an inconsistency as the returned length does not
include the terminal null, but when using the input parameter "size", it
does include the terminal null.

/* get object location */
    if(H5G_loc(id, &loc) >= 0) {
        ssize_t len = 0;

        /* If the user path is available and it's not "hidden", use it
*/
        if(loc.path->user_path_r != NULL && loc.path->obj_hidden == 0) {
            len = H5RS_len(loc.path->user_path_r);

            if(name) {
                HDstrncpy(name, H5RS_get_str(loc.path->user_path_r),
MIN((size_t)(len + 1), size));
                if((size_t)len >= size)
                    name[size - 1] = '\0';
            } /* end if */
        } /* end if */

···

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe@hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.

hello, Jason

Is there an error in the documentation for H5Iget_name(), regarding the
terminal null ? (or the function itself)
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5I.html#Identify-GetName

I am trying to allocate a buffer dynamically by first calling
ssize_t sz = H5Iget_name(obj_id, NULL, 0);

Then I allocate the buffer, then I call
H5Iget_name(obj_id, NULL, sz);

and it chops off the last character. When I look at h5gname.c I see
this: (the variable "len" is later returned as the return value) -- it
looks like there's an inconsistency as the returned length does not
include the terminal null, but when using the input parameter "size", it
does include the terminal null.

/* get object location */
   if(H5G_loc(id, &loc) >= 0) {
       ssize_t len = 0;

       /* If the user path is available and it's not "hidden", use it
*/
       if(loc.path->user_path_r != NULL && loc.path->obj_hidden == 0) {
           len = H5RS_len(loc.path->user_path_r);

           if(name) {
               HDstrncpy(name, H5RS_get_str(loc.path->user_path_r),
MIN((size_t)(len + 1), size));
               if((size_t)len >= size)
                   name[size - 1] = '\0';
           } /* end if */
       } /* end if */

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe@hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.

when H5Iget_name is called with zero on the SIZE parameter

ssize_t sz = H5Iget_name(obj_id, NULL, 0);

it returns the value of

size_t strlen( const char *string );

the number of characters in string, excluding the terminal NULL (this string is stored internally by HDF5)

the second call of H5Iget_name

H5Iget_name(obj_id, NULL, sz);

calls

char *strncpy( char *strDest, const char *strSource, size_t count );

from the man pages:

Copy characters of one string to another.

The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.

so, the string has to be "manually" NULL terminated in our code

To solve this inconsistency you can make the second call with SIZE + 1, SIZE being the value returned on the first call

does you (or anybody else in this list) would prefer this handled a different way ? For example, the function to return SIZE + 1 ?

Anyway, you are right that this behavior is not very clear in the reference manual, that we will update

http://www.hdfgroup.org/HDF5/doc/RM/RM_H5I.html#Identify-GetName

thanks for catching the issue

···

At 09:31 AM 4/10/2008, Jason Sachs wrote:

--------------------------------------------------------------
Pedro Vicente (T) 217.265-0311
pvn@hdfgroup.org
The HDF Group. 1901 S. First. Champaign, IL 61820

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe@hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.