Index: dsets.c
===================================================================
--- dsets.c	(revision 27127)
+++ dsets.c	(working copy)
@@ -196,6 +196,15 @@
 #define BYPASS_CHUNK_DIM         500
 #define BYPASS_FILL_VALUE        7
 
+/* Parameters for the vlen down-sizing test */
+#define VLEN_DS_NAME             "test_downsize_vlen_scalar_dataset"
+#define VLEN_DS_DIM              100
+#define VLEN_DS_STRLEN           20
+#define VLEN_DS_STRING           "vlen test string"
+#define VLEN_DS_VALUE            0.12345678901234567890
+#define VLEN_DS_ARRAY_DIM1       3
+#define VLEN_DS_ARRAY_DIM2       5
+
 /* Shared global arrays */
 #define DSET_DIM1       100
 #define DSET_DIM2       200
@@ -9015,6 +9024,144 @@
 
 
 /*-------------------------------------------------------------------------
+ * Function:    test_downsize_vlen_scalar_dataset
+ *
+ * Purpose:     Tests H5Dwrite on a scalar dataset containing a VLEN array
+ *              of { double, C-string }. This causes special code to free
+ *              the nested VLEN (in this case, C-string) allocations.
+ *              
+ *              Note: at 1.8.14 this was identified as a bug and failed.
+ *
+ * Return:      Success: 0
+ *              Failure: -1
+ *
+ * Programmer:  Mark Hodson
+ *              Tuesday 2nd June 2015
+ *
+ *-------------------------------------------------------------------------
+ */
+typedef struct {
+    double value;
+    char * string[VLEN_DS_ARRAY_DIM1][VLEN_DS_ARRAY_DIM2];
+} compound_file_t;
+
+typedef struct {
+    int padding1;
+    double value;
+    int padding2;
+    char * string[VLEN_DS_ARRAY_DIM1][VLEN_DS_ARRAY_DIM2];
+    int padding3;
+} compound_memory_t;
+
+static herr_t
+test_downsize_vlen_scalar_dataset(hid_t file)
+{
+    hid_t       scalar_sid = -1;                /* Scalar dataspace ID */
+    hid_t       string_tid = -1;                /* VARIABLE string datatype ID */
+    hid_t       string_array_tid = -1;          /* VARIABLE string array datatype ID */
+    hid_t       compound_file_tid = -1;         /* COMPOUND datatype ID */
+    hid_t       compound_memory_tid = -1;       /* COMPOUND datatype ID */
+    hid_t       vlen_compound_file_tid = -1;    /* VARIABLE COMPOUND datatype ID */
+    hid_t       vlen_compound_memory_tid = -1;  /* VARIABLE COMPOUND datatype ID */
+    hid_t       scalar_did = -1;                /* SCALAR dataset ID */
+    hvl_t       vlen_compound_data;             /* Top-level VLEN data */
+    compound_memory_t compound_data[VLEN_DS_DIM]; /* Contents of VLEN data */
+    char        common_string[VLEN_DS_STRLEN];  /* Common string contents */
+    hsize_t     array_dims[2] = { VLEN_DS_ARRAY_DIM1, VLEN_DS_ARRAY_DIM2 };
+    int         i, dim1, dim2;                  /* Local index variables */
+
+    TESTING("H5Dwrite() on down-sized VLEN contents");
+
+    /* Create scalar dataspace */
+    if((scalar_sid = H5Screate(H5S_SCALAR)) < 0)
+        TEST_ERROR
+
+    /* Create datatype VLEN{COMPOUND{"value":H5T_NATIVE_DOUBLE, "string":H5T_C_S1|H5T_VARIABLE}} */
+    /* Note: the memory and file structures must be different to invoke the bug @ H5Tconv.c:3323 */
+    if((string_tid = H5Tcopy(H5T_C_S1)) < 0)
+        TEST_ERROR
+    if(H5Tset_size(string_tid, H5T_VARIABLE) < 0)
+        TEST_ERROR
+
+    if((string_array_tid = H5Tarray_create(string_tid, 2, array_dims)) < 0)
+        TEST_ERROR
+
+    if((compound_file_tid = H5Tcreate(H5T_COMPOUND, sizeof(compound_file_t))) < 0)
+        TEST_ERROR
+    if(H5Tinsert(compound_file_tid, "value", HOFFSET(compound_file_t, value), H5T_NATIVE_DOUBLE) < 0)
+        TEST_ERROR
+    if(H5Tinsert(compound_file_tid, "string", HOFFSET(compound_file_t, string), string_array_tid) < 0)
+        TEST_ERROR
+    if((vlen_compound_file_tid = H5Tvlen_create(compound_file_tid)) < 0)
+        TEST_ERROR
+
+    if((compound_memory_tid = H5Tcreate(H5T_COMPOUND, sizeof(compound_memory_t))) < 0)
+        TEST_ERROR
+    if(H5Tinsert(compound_memory_tid, "value", HOFFSET(compound_memory_t, value), H5T_NATIVE_DOUBLE) < 0)
+        TEST_ERROR
+    if(H5Tinsert(compound_memory_tid, "string", HOFFSET(compound_memory_t, string), string_array_tid) < 0)
+        TEST_ERROR
+    if((vlen_compound_memory_tid = H5Tvlen_create(compound_memory_tid)) < 0)
+        TEST_ERROR
+
+    /* Create the scalar dataset of this data type */
+    if((scalar_did = H5Dcreate(file, VLEN_DS_NAME, vlen_compound_file_tid, scalar_sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
+        TEST_ERROR
+
+    /* Setup the variable-length data. Note that if the double "value" field is set to 0.0, the bug will NOT */
+    /* occur because this is the data at offset zero of the element, and it then looks like a NULL VLEN data */
+    strcpy(common_string, VLEN_DS_STRING);
+
+    for(i=0; i<VLEN_DS_DIM; ++i) {
+        compound_data[i].value = VLEN_DS_VALUE;
+        for(dim1=0; dim1<VLEN_DS_ARRAY_DIM1; ++dim1) {
+            for(dim2=0; dim2<VLEN_DS_ARRAY_DIM2; ++dim2) {
+                compound_data[i].string[dim1][dim2] = common_string;
+            }
+        }
+        compound_data[i].padding1 = 0;
+        compound_data[i].padding2 = 0;
+        compound_data[i].padding3 = 0;
+    }
+
+    /* Starting with the maximum size, progressively over-write the content of the dataset with smaller arrays. */
+    /* Note: the bug in v1.8.14 is tripped on the second iteration, when 100 elements are over-written with 99. */
+    for(i=VLEN_DS_DIM; i>0; --i) {
+        vlen_compound_data.len = i;
+        vlen_compound_data.p = compound_data;
+        if (H5Dwrite(scalar_did, vlen_compound_memory_tid, scalar_sid, scalar_sid, H5P_DEFAULT, &vlen_compound_data) < 0)
+            TEST_ERROR
+    }
+
+    /* Close everything */
+    if(H5Sclose(scalar_sid) < 0) TEST_ERROR
+    if(H5Tclose(string_tid) < 0) TEST_ERROR
+    if(H5Tclose(string_array_tid) < 0) TEST_ERROR
+    if(H5Tclose(compound_file_tid) < 0) TEST_ERROR
+    if(H5Tclose(vlen_compound_file_tid) < 0) TEST_ERROR
+    if(H5Tclose(compound_memory_tid) < 0) TEST_ERROR
+    if(H5Tclose(vlen_compound_memory_tid) < 0) TEST_ERROR
+    if(H5Dclose(scalar_did) < 0) TEST_ERROR
+
+    PASSED();
+    return 0;
+
+error:
+    H5E_BEGIN_TRY {
+        H5Sclose(scalar_sid);
+        H5Tclose(string_tid);
+        H5Tclose(string_array_tid);
+        H5Tclose(compound_file_tid);
+        H5Tclose(vlen_compound_file_tid);
+        H5Tclose(compound_memory_tid);
+        H5Tclose(vlen_compound_memory_tid);
+        H5Dclose(scalar_did);
+    } H5E_END_TRY;
+    return -1;
+} /* end test_downsize_vlen_scalar_dataset() */
+
+
+/*-------------------------------------------------------------------------
  * Function:	main
  *
  * Purpose:	Tests the dataset interface (H5D)
@@ -9130,6 +9277,7 @@
         nerrors += (test_filter_delete(file) < 0		? 1 : 0);
         nerrors += (test_filters_endianess() < 0	? 1 : 0);
         nerrors += (test_zero_dims(file) < 0		? 1 : 0);
+        nerrors += (test_downsize_vlen_scalar_dataset(file) < 0		? 1 : 0);
         nerrors += (test_missing_chunk(file) < 0		? 1 : 0);
         nerrors += (test_random_chunks(my_fapl) < 0		? 1 : 0);
 #ifndef H5_NO_DEPRECATED_SYMBOLS
