summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDana Robinson <43805+derobins@users.noreply.github.com>2022-08-08 18:00:13 (GMT)
committerGitHub <noreply@github.com>2022-08-08 18:00:13 (GMT)
commit57926d549ef75c532badc7ad3318170f7130295b (patch)
treef1997e6e5727144972956b31ff40d62baa2455d8
parent956282fa6f5f5b7df92be6f14c12681f7ecabbc9 (diff)
downloadhdf5-57926d549ef75c532badc7ad3318170f7130295b.zip
hdf5-57926d549ef75c532badc7ad3318170f7130295b.tar.gz
hdf5-57926d549ef75c532badc7ad3318170f7130295b.tar.bz2
Fixes some badness in the onion VFD revision comment code (#1975)
* Fixes some badness in the revision comment code * Committing clang-format changes Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
-rw-r--r--src/H5FDonion.c25
-rw-r--r--src/H5FDonion.h8
2 files changed, 20 insertions, 13 deletions
diff --git a/src/H5FDonion.c b/src/H5FDonion.c
index 23ea624..6cdd9a4 100644
--- a/src/H5FDonion.c
+++ b/src/H5FDonion.c
@@ -1182,16 +1182,23 @@ H5FD__onion_open(const char *filename, unsigned flags, hid_t fapl_id, haddr_t ma
/* Copy comment from FAPL info, if one is given */
if ((H5F_ACC_RDWR | H5F_ACC_CREAT | H5F_ACC_TRUNC) & flags) {
- if (fa->comment) {
- /* Free the old comment */
- file->curr_rev_record.comment = H5MM_xfree(file->curr_rev_record.comment);
-
- /* TODO: Lengths of strings should be size_t */
- file->curr_rev_record.comment_size = (uint32_t)HDstrlen(fa->comment) + 1;
+ /* Free the old comment */
+ file->curr_rev_record.comment = H5MM_xfree(file->curr_rev_record.comment);
+
+ /* The buffer is of size H5FD_ONION_FAPL_INFO_COMMENT_MAX_LEN + 1
+ *
+ * We're getting this buffer from a fixed-size array in a struct, which
+ * will be garbage and not null-terminated if the user isn't careful.
+ * Be careful of this and do strndup first to ensure strdup gets a
+ * null-termianted string (HDF5 doesn't provide a strnlen call if you
+ * don't have one).
+ */
+ if (NULL ==
+ (file->curr_rev_record.comment = H5MM_strndup(fa->comment, H5FD_ONION_FAPL_INFO_COMMENT_MAX_LEN)))
+ HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, NULL, "unable to duplicate comment string")
- if (NULL == (file->curr_rev_record.comment = H5MM_xstrdup(fa->comment)))
- HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, NULL, "unable to allocate comment string")
- }
+ /* TODO: Lengths of strings should be size_t */
+ file->curr_rev_record.comment_size = (uint32_t)HDstrlen(fa->comment) + 1;
}
file->origin_eof = file->header.origin_eof;
file->logical_eof = MAX(file->curr_rev_record.logical_eof, file->logical_eof);
diff --git a/src/H5FDonion.h b/src/H5FDonion.h
index 04fd2ff..cc767c6 100644
--- a/src/H5FDonion.h
+++ b/src/H5FDonion.h
@@ -33,12 +33,12 @@
/* Flag to require page alignment of onion revision data */
#define H5FD_ONION_FAPL_INFO_CREATE_FLAG_ENABLE_PAGE_ALIGNMENT 0x2
-/* Max length of a comment */
+/* Max length of a comment
+ * The buffer is defined to be this size + 1 to handle the NUL
+ */
#define H5FD_ONION_FAPL_INFO_COMMENT_MAX_LEN 255
-/* Indicates that you want the latest revision
- * TODO: Does this work?
- */
+/* Indicates that you want the latest revision */
#define H5FD_ONION_FAPL_INFO_REVISION_ID_LATEST UINT64_MAX
typedef enum H5FD_onion_target_file_constant_t {