Skip to content

Commit e5f526b

Browse files
authored
Fix use of uninit memory in h5dumpgentest (#5947)
Fix uninitialized memory usage in h5dumpgentest.c by initializing arrays and structures, using calloc, and adding memory deallocation.
1 parent cd2414b commit e5f526b

File tree

1 file changed

+85
-43
lines changed

1 file changed

+85
-43
lines changed

tools/test/h5dump/h5dumpgentest.c

Lines changed: 85 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,7 @@ gent_nestcomp(void)
25672567
/*
25682568
* Initialize the data
25692569
*/
2570+
memset(s1, 0, sizeof(s1));
25702571
for (i = 0; i < 10; i++) {
25712572
s1[i].a = i;
25722573
s1[i].b = (float)(i * i);
@@ -4115,29 +4116,41 @@ write_attr_in(hid_t loc_id, const char *dset_name, /* for saving reference to da
41154116

41164117
/* create 1D attributes with dimension [2], 2 elements */
41174118
hsize_t dims[1] = {2};
4118-
char buf1[2][2] = {"ab", "de"}; /* string, NO NUL fixed length */
4119-
char buf2[2] = {1, 2}; /* bitfield, opaque */
4120-
s_t buf3[2] = {{1, 2}, {3, 4}}; /* compound */
4121-
hobj_ref_t buf4[2]; /* reference */
4122-
hvl_t buf5[2]; /* vlen */
4123-
hsize_t dimarray[1] = {3}; /* array dimension */
4124-
int buf6[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* array */
4125-
int buf7[2] = {1, 2}; /* integer */
4126-
float buf8[2] = {1, 2}; /* float */
4127-
float buf9[4] = {1, 2, 3, 4}; /* complex */
4119+
char buf1[2][2] = {"ab", "de"}; /* string, NO NUL fixed length */
4120+
char buf2[2] = {1, 2}; /* bitfield, opaque */
4121+
s_t buf3[2]; /* compound */
4122+
hobj_ref_t buf4[2]; /* reference */
4123+
hvl_t buf5[2]; /* vlen */
4124+
4125+
memset(buf3, 0, sizeof(buf3));
4126+
buf3[0].a = 1;
4127+
buf3[0].b = 2;
4128+
buf3[1].a = 3;
4129+
buf3[1].b = 4;
4130+
hsize_t dimarray[1] = {3}; /* array dimension */
4131+
int buf6[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* array */
4132+
int buf7[2] = {1, 2}; /* integer */
4133+
float buf8[2] = {1, 2}; /* float */
4134+
float buf9[4] = {1, 2, 3, 4}; /* complex */
41284135

41294136
/* create 2D attributes with dimension [3][2], 6 elements */
41304137
hsize_t dims2[2] = {3, 2};
41314138
char buf12[6][2] = {"ab", "cd", "ef", "gh", "ij", "kl"}; /* string, NO NUL fixed length */
41324139
char buf22[3][2] = {{1, 2}, {3, 4}, {5, 6}}; /* bitfield, opaque */
4133-
s_t buf32[6] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}}; /* compound */
4134-
hobj_ref_t buf42[3][2]; /* reference */
4135-
hvl_t buf52[3][2]; /* vlen */
4140+
s_t buf32[6]; /* compound */
4141+
hobj_ref_t buf42[3][2]; /* reference */
4142+
hvl_t buf52[3][2]; /* vlen */
41364143
int buf62[6][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {13, 14, 15}, {16, 17, 18}}; /* array */
41374144
int buf72[3][2] = {{1, 2}, {3, 4}, {5, 6}}; /* integer */
41384145
float buf82[3][2] = {{1, 2}, {3, 4}, {5, 6}}; /* float */
41394146
float buf92[6][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}}; /* complex */
41404147

4148+
memset(buf32, 0, sizeof(buf32));
4149+
for (i = 0; i < 6; i++) {
4150+
buf32[i].a = (2 * i) + 1;
4151+
buf32[i].b = (2 * i) + 2;
4152+
}
4153+
41414154
/* create 3D attributes with dimension [4][3][2], 24 elements */
41424155
hsize_t dims3[3] = {4, 3, 2};
41434156
char buf13[24][2] = {
@@ -4152,6 +4165,7 @@ write_attr_in(hid_t loc_id, const char *dset_name, /* for saving reference to da
41524165
float buf83[4][3][2]; /* float */
41534166
float buf93[24][2]; /* complex */
41544167

4168+
memset(buf33, 0, sizeof(buf33));
41554169
/*-------------------------------------------------------------------------
41564170
* 1D attributes
41574171
*-------------------------------------------------------------------------
@@ -4573,29 +4587,41 @@ write_dset_in(hid_t loc_id, const char *dset_name, /* for saving reference to da
45734587

45744588
/* create 1D attributes with dimension [2], 2 elements */
45754589
hsize_t dims[1] = {2};
4576-
char buf1[2][2] = {"ab", "de"}; /* string, NO NUL fixed length */
4577-
char buf2[2] = {1, 2}; /* bitfield, opaque */
4578-
s_t buf3[2] = {{1, 2}, {3, 4}}; /* compound */
4579-
hobj_ref_t buf4[2]; /* reference */
4580-
hvl_t buf5[2]; /* vlen */
4581-
hsize_t dimarray[1] = {3}; /* array dimension */
4582-
int buf6[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* array */
4583-
int buf7[2] = {1, 2}; /* integer */
4584-
float buf8[2] = {1, 2}; /* float */
4585-
float buf9[4] = {1, 2, 3, 4}; /* complex */
4590+
char buf1[2][2] = {"ab", "de"}; /* string, NO NUL fixed length */
4591+
char buf2[2] = {1, 2}; /* bitfield, opaque */
4592+
s_t buf3[2]; /* compound */
4593+
hobj_ref_t buf4[2]; /* reference */
4594+
hvl_t buf5[2]; /* vlen */
4595+
4596+
memset(buf3, 0, sizeof(buf3));
4597+
buf3[0].a = 1;
4598+
buf3[0].b = 2;
4599+
buf3[1].a = 3;
4600+
buf3[1].b = 4;
4601+
hsize_t dimarray[1] = {3}; /* array dimension */
4602+
int buf6[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* array */
4603+
int buf7[2] = {1, 2}; /* integer */
4604+
float buf8[2] = {1, 2}; /* float */
4605+
float buf9[4] = {1, 2, 3, 4}; /* complex */
45864606

45874607
/* create 2D attributes with dimension [3][2], 6 elements */
45884608
hsize_t dims2[2] = {3, 2};
45894609
char buf12[6][2] = {"ab", "cd", "ef", "gh", "ij", "kl"}; /* string, NO NUL fixed length */
45904610
char buf22[3][2] = {{1, 2}, {3, 4}, {5, 6}}; /* bitfield, opaque */
4591-
s_t buf32[6] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}}; /* compound */
4592-
hobj_ref_t buf42[3][2]; /* reference */
4593-
hvl_t buf52[3][2]; /* vlen */
4611+
s_t buf32[6]; /* compound */
4612+
hobj_ref_t buf42[3][2]; /* reference */
4613+
hvl_t buf52[3][2]; /* vlen */
45944614
int buf62[6][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {13, 14, 15}, {16, 17, 18}}; /* array */
45954615
int buf72[3][2] = {{1, 2}, {3, 4}, {5, 6}}; /* integer */
45964616
float buf82[3][2] = {{1, 2}, {3, 4}, {5, 6}}; /* float */
45974617
float buf92[6][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}}; /* complex */
45984618

4619+
memset(buf32, 0, sizeof(buf32));
4620+
for (int i = 0; i < 6; i++) {
4621+
buf32[i].a = (2 * i) + 1;
4622+
buf32[i].b = (2 * i) + 2;
4623+
}
4624+
45994625
/* create 3D attributes with dimension [4][3][2], 24 elements */
46004626
hsize_t dims3[3] = {4, 3, 2};
46014627
char buf13[24][2] = {
@@ -4610,6 +4636,7 @@ write_dset_in(hid_t loc_id, const char *dset_name, /* for saving reference to da
46104636
float buf83[4][3][2]; /* float */
46114637
float buf93[24][2]; /* complex */
46124638

4639+
memset(buf33, 0, sizeof(buf33));
46134640
/*-------------------------------------------------------------------------
46144641
* 1D
46154642
*-------------------------------------------------------------------------
@@ -6080,20 +6107,29 @@ gent_fvalues(void)
60806107
double b;
60816108
} c_t;
60826109

6083-
hid_t fid; /* file id */
6084-
hid_t dcpl; /* dataset creation property list */
6085-
hid_t sid; /* dataspace ID */
6086-
hid_t tid; /* datatype ID */
6087-
hid_t did; /* datasetID */
6088-
hsize_t dims[1] = {2};
6089-
int buf[2] = {1, 2}; /* integer */
6090-
int fillval1 = -99; /* integer fill value */
6091-
c_t buf2[2] = {{1, 2}, {3, 4}}; /* compound */
6092-
c_t fillval2[1] = {{1, 2}}; /* compound fill value */
6093-
hvl_t buf3[2]; /* vlen */
6094-
hvl_t fillval3; /* vlen fill value */
6095-
hsize_t dimarray[1] = {3}; /* array dimension */
6096-
int buf4[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* array */
6110+
hid_t fid; /* file id */
6111+
hid_t dcpl; /* dataset creation property list */
6112+
hid_t sid; /* dataspace ID */
6113+
hid_t tid; /* datatype ID */
6114+
hid_t did; /* datasetID */
6115+
hsize_t dims[1] = {2};
6116+
int buf[2] = {1, 2}; /* integer */
6117+
int fillval1 = -99; /* integer fill value */
6118+
c_t buf2[2]; /* compound */
6119+
c_t fillval2[1]; /* compound fill value */
6120+
hvl_t buf3[2]; /* vlen */
6121+
hvl_t fillval3; /* vlen fill value */
6122+
hsize_t dimarray[1] = {3}; /* array dimension */
6123+
6124+
memset(buf2, 0, sizeof(buf2));
6125+
buf2[0].a = 1;
6126+
buf2[0].b = 2;
6127+
buf2[1].a = 3;
6128+
buf2[1].b = 4;
6129+
memset(fillval2, 0, sizeof(fillval2));
6130+
fillval2[0].a = 1;
6131+
fillval2[0].b = 2;
6132+
int buf4[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* array */
60976133
int H5_ATTR_NDEBUG_UNUSED ret;
60986134

60996135
/* create a file */
@@ -10195,9 +10231,9 @@ gent_compound_ints(void)
1019510231
int m; /* Array init loop vars */
1019610232

1019710233
/* Allocate buffers */
10198-
Cmpd1 = (Cmpd1Struct *)malloc(sizeof(Cmpd1Struct) * F77_LENGTH);
10234+
Cmpd1 = (Cmpd1Struct *)calloc(F77_LENGTH, sizeof(Cmpd1Struct));
1019910235
assert(Cmpd1);
10200-
Cmpd2 = (Cmpd2Struct *)malloc(sizeof(Cmpd2Struct) * F77_LENGTH);
10236+
Cmpd2 = (Cmpd2Struct *)calloc(F77_LENGTH, sizeof(Cmpd2Struct));
1020110237
assert(Cmpd2);
1020210238

1020310239
/* Initialize the data in the arrays/datastructure */
@@ -11217,6 +11253,8 @@ gent_bitnopaquefields(void)
1121711253
uint64_t buf4[F80_DIM32]; /* bitfield, opaque */
1121811254
s_t buf5[F80_DIM32]; /* compound */
1121911255

11256+
memset(buf5, 0, sizeof(buf5));
11257+
1122011258
file_id = H5Fcreate(FILE80, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
1122111259

1122211260
if ((grp = H5Gcreate2(file_id, "bittypetests", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) >= 0) {
@@ -11468,7 +11506,7 @@ gent_compound_complex2(void)
1146811506
hsize_t nelmts = F82_DIM32;
1146911507

1147011508
/* Allocate buffer */
11471-
buf = (compound *)malloc(sizeof(compound) * F82_DIM32);
11509+
buf = (compound *)calloc(F82_DIM32, sizeof(compound));
1147211510
assert(buf);
1147311511

1147411512
file = H5Fcreate(FILE82, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
@@ -14289,6 +14327,10 @@ gent_test_reference_external(void)
1428914327
if (H5Dwrite(dataset, H5T_STD_REF, H5S_ALL, H5S_ALL, H5P_DEFAULT, ref_wbuf) < 0)
1429014328
return 1;
1429114329

14330+
/* Destroy references to free allocated memory */
14331+
for (i = 0; i < SPACE1_DIM1; i++)
14332+
H5Rdestroy(&ref_wbuf[i]);
14333+
1429214334
/* Close disk dataspace */
1429314335
if (H5Sclose(sid) < 0)
1429414336
return 1;

0 commit comments

Comments
 (0)