From ddfb6d6279ff284861291fc7c7c244ff82bdb8c5 Mon Sep 17 00:00:00 2001 From: Dennis Heimbigner Date: Mon, 8 Aug 2016 21:54:23 -0600 Subject: [PATCH] Make sure that the _NcProperties attr is null terminated and stored as such --- libsrc4/nc4info.c | 14 ++++++++++---- ncdump/ncdump.c | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/libsrc4/nc4info.c b/libsrc4/nc4info.c index 141ea2b87..0f8b8efdb 100644 --- a/libsrc4/nc4info.c +++ b/libsrc4/nc4info.c @@ -126,6 +126,8 @@ NC4_get_propattr(NC_HDF5_FILE_INFO_T* h5) {ncstat = NC_ENOMEM; goto done;} HCHECK((ntype = H5Tget_native_type(atype, H5T_DIR_ASCEND))); HCHECK((H5Aread(attid, ntype, text))); + /* Make sure its null terminated */ + text[size] = '\0'; /* Try to parse text */ ncstat = NC4_properties_parse(&h5->fileinfo->propattr,text); herr = 0; @@ -166,7 +168,7 @@ NC4_put_propattr(NC_HDF5_FILE_INFO_T* h5) /* Create a datatype to refer to. */ HCHECK((atype = H5Tcopy(H5T_C_S1))); HCHECK((H5Tset_cset(atype, H5T_CSET_ASCII))); - HCHECK((H5Tset_size(atype, strlen(text)))); + HCHECK((H5Tset_size(atype, strlen(text)+1))); /*keep nul term */ HCHECK((aspace = H5Screate(H5S_SCALAR))); HCHECK((attid = H5Acreate(grp, NCPROPS, atype, aspace, H5P_DEFAULT))); HCHECK((H5Awrite(attid, atype, text))); @@ -205,6 +207,7 @@ int NC4_buildpropinfo(struct NCPROPINFO* info,char** propdatap) { size_t total; + char* propdata = NULL; if(info == NULL || info->version == 0) return NC_EINVAL; if(propdatap == NULL) @@ -227,13 +230,16 @@ NC4_buildpropinfo(struct NCPROPINFO* info,char** propdatap) total += strlen("="); total += strlen(info->hdf5ver); } - *propdatap = (char*)malloc(total+1); - if(*propdatap == NULL) + propdata = (char*)malloc(total+1); + if(propdata == NULL) return NC_ENOMEM; - snprintf(*propdatap,total+1, + snprintf(propdata,total+1, "%s=%d|%s=%s|%s=%s", NCPVERSION,info->version, NCPNCLIBVERSION,info->netcdfver, NCPHDF5LIBVERSION,info->hdf5ver); + /* Force null termination */ + propdata[total] = '\0'; + if(propdatap) {*propdatap = propdata;} else {free(propdata);} return NC_NOERR; } diff --git a/ncdump/ncdump.c b/ncdump/ncdump.c index be43a9e3a..374a883f5 100644 --- a/ncdump/ncdump.c +++ b/ncdump/ncdump.c @@ -1098,7 +1098,7 @@ pr_att_hidden( if(stat == NC_NOERR) { pr_att_name(ncid, "", NCPROPS); /* make sure its null terminated */ - propdata[len+1] = '\0'; + propdata[len] = '\0'; printf(" = \"%s\" ;\n",propdata); } free(propdata);