diff --git a/src/serial_io.c b/src/serial_io.c
index 1bf43a34328739c9a3b77e04c7caefc99d1ca629..b189df53c0fae8d7ebe23714217ffa2201d70ee2 100644
--- a/src/serial_io.c
+++ b/src/serial_io.c
@@ -201,6 +201,9 @@ void prepareArray(hid_t grp, char* fileName, FILE* xmfFile, char* name,
     chunk_shape[1] = 0;
   }
 
+  /* Make sure the chunks are not larger than the dataset */
+  if (chunk_shape[0] > N) chunk_shape[0] = N;
+
   /* Change shape of data space */
   h_err = H5Sset_extent_simple(h_space, rank, shape, NULL);
   if (h_err < 0) {
diff --git a/src/single_io.c b/src/single_io.c
index 6547b417c873252e23cb69f384fc68a8a32ff9ff..f3225ecf487d223c3bea560cbae626a03da36928 100644
--- a/src/single_io.c
+++ b/src/single_io.c
@@ -199,6 +199,9 @@ void writeArrayBackEnd(hid_t grp, char* fileName, FILE* xmfFile, char* name,
     chunk_shape[1] = 0;
   }
 
+  /* Make sure the chunks are not larger than the dataset */
+  if (chunk_shape[0] > N) chunk_shape[0] = N;
+
   /* Change shape of data space */
   h_err = H5Sset_extent_simple(h_space, rank, shape, NULL);
   if (h_err < 0) {