Skip to content
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

Segmentation fault when reading an array of fixed length strings (inc in ncdump) #2159

Closed
IanHolthamGMV opened this issue Dec 2, 2021 · 3 comments · Fixed by #2466
Closed
Assignees
Milestone

Comments

@IanHolthamGMV
Copy link

I have been writing a file handler in C++ for HDF5 files with v4.8.1 on Linux (Centos7).

Everything has gone well until I tried to read an array of fixed length strings.

I created a test file with a single Group and then a single Dataset of strings of fixed length 20 in a 1 dimensional array of 5 strings. If I then call ncdump on that file I get a segmentation fault (output from ncdump at the end of this post). Stepping through my own code with the same file reveals a segmentation fault when I call nc_free_string on this dataset.

I've stepped into the code and looking at line 1875 of hdf5var.c, it seems to allocate memory for just the first string in the array (fstring_len is 20):

if (!(*(char **)data = malloc(1 + fstring_len)))

Is there something I've misunderstood, or is this a bug in the library?

Thanks

Ian

StringTest.txt

@DennisHeimbigner
Copy link
Collaborator

I think I see the problem. Netcdf-4 does not support HDF5 style fixed length strings; it only
supports variable length strings (more or less so when you do this:

dimensions: phony_dim_0 = 5 ;
variables: string StringArray(phony_dim_0) ;

You are in effect doing the equivalent of allocated a 5 element array each of wholes elements
is a variable length string. More or less equivalent to "char* StringArray[5]"
Unfortunately, F90 does not handle this case well. You might be better off doing something
like this:

dimensions: strlen = 20; nstrings=5;
variable: char StringArray(nstrings,strlen)

@jswhit
Copy link

jswhit commented Jun 28, 2022

4.9.0 segfaults when trying to read an hdf5 file with fixed length string arrays (test file available at Unidata/netcdf4-python#1172).

@DennisHeimbigner
Copy link
Collaborator

I have a fix that I am testing now. Your identification of

if (!(*(char **)data = malloc(1 + fstring_len)))

as the source of the problem appears to be correct, although the fix
is a bit complicated.

@WardF WardF added this to the 4.9.1 milestone Jun 28, 2022
DennisHeimbigner added a commit to DennisHeimbigner/netcdf-c that referenced this issue Jul 29, 2022
re: Unidata#2159

There was error in libhdf5 that only allowed reading a single
value HDF5 fixed string. Fix to allow reading an array of such strings.
Also make sure it still works for scalars and for attributes.
Add a testcase: nc_test4/tst_fixedstring.sh.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants