Skip to content

Commit

Permalink
Fix remaining calloc param reversals (#5018)
Browse files Browse the repository at this point in the history
gcc 14 identifies likely places where the sizeof() call is used
for the first parameter to calloc(). This PR fixes the remaining
places in the library where this occurs and reworks a few other
allocations to be more uniform.
  • Loading branch information
derobins authored Oct 28, 2024
1 parent a38d87a commit 786b033
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 127 deletions.
2 changes: 1 addition & 1 deletion hl/src/H5TB.c
Original file line number Diff line number Diff line change
Expand Up @@ -3185,7 +3185,7 @@ H5TB_create_type(hid_t loc_id, const char *dset_name, size_t type_size, const si
if (H5TBget_table_info(loc_id, dset_name, &nfields, NULL) < 0)
goto out;

if (NULL == (fnames = (char **)calloc(sizeof(char *), (size_t)nfields)))
if (NULL == (fnames = (char **)calloc((size_t)nfields, sizeof(char *))))
goto out;

for (i = 0; i < nfields; i++)
Expand Down
2 changes: 1 addition & 1 deletion test/hyperslab.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ test_array_fill(size_t lo, size_t hi)
TESTING(s);

/* Initialize */
if (NULL == (dst = (int *)calloc(sizeof(int), ARRAY_FILL_SIZE * hi)))
if (NULL == (dst = (int *)calloc((ARRAY_FILL_SIZE * hi), sizeof(int))))
TEST_ERROR;

/* Setup */
Expand Down
40 changes: 20 additions & 20 deletions test/ntypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ test_compound_dtype2(hid_t file)
TESTING("nested compound datatype");

/* Allocate space for the points & check arrays */
if (NULL == (points = malloc(sizeof(s1) * DIM0 * DIM1)))
if (NULL == (points = (s1 *)malloc((DIM0 * DIM1) * sizeof(s1))))
TEST_ERROR;
if (NULL == (check = calloc(DIM0 * DIM1, sizeof(s1))))
if (NULL == (check = (s1 *)calloc((DIM0 * DIM1), sizeof(s1))))
TEST_ERROR;

/* Initialize the dataset */
Expand Down Expand Up @@ -533,9 +533,9 @@ test_compound_dtype2(hid_t file)

/* Read the dataset back. Temporary buffer is for special platforms like
* Cray */
if (NULL == (tmp = malloc(DIM0 * DIM1 * H5Tget_size(native_type))))
if (NULL == (tmp = malloc((DIM0 * DIM1) * H5Tget_size(native_type))))
TEST_ERROR;
if (NULL == (bkg = calloc(DIM0 * DIM1, sizeof(s1))))
if (NULL == (bkg = calloc((DIM0 * DIM1), sizeof(s1))))
TEST_ERROR;

if (H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
Expand Down Expand Up @@ -648,9 +648,9 @@ test_compound_dtype(hid_t file)
TESTING("compound datatype");

/* Allocate space for the points & check arrays */
if (NULL == (points = malloc(sizeof(s1) * DIM0 * DIM1)))
if (NULL == (points = (s1 *)malloc((DIM0 * DIM1) * sizeof(s1))))
TEST_ERROR;
if (NULL == (check = calloc(DIM0 * DIM1, sizeof(s1))))
if (NULL == (check = (s1 *)calloc((DIM0 * DIM1), sizeof(s1))))
TEST_ERROR;

/* Initialize the dataset */
Expand Down Expand Up @@ -752,9 +752,9 @@ test_compound_dtype(hid_t file)

/* Read the dataset back. Temporary buffer is for special platforms like
* Cray */
if (NULL == (tmp = malloc(DIM0 * DIM1 * H5Tget_size(native_type))))
if (NULL == (tmp = malloc((DIM0 * DIM1) * H5Tget_size(native_type))))
TEST_ERROR;
if (NULL == (bkg = calloc(DIM0 * DIM1, sizeof(s1))))
if (NULL == (bkg = calloc((DIM0 * DIM1), sizeof(s1))))
TEST_ERROR;

if (H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
Expand Down Expand Up @@ -856,9 +856,9 @@ test_compound_dtype3(hid_t file)
TESTING("compound datatype with array as field");

/* Allocate space for the points & check arrays */
if (NULL == (points = malloc(sizeof(s1) * DIM0 * DIM1)))
if (NULL == (points = (s1 *)malloc((DIM0 * DIM1) * sizeof(s1))))
TEST_ERROR;
if (NULL == (check = calloc(DIM0 * DIM1, sizeof(s1))))
if (NULL == (check = (s1 *)calloc((DIM0 * DIM1), sizeof(s1))))
TEST_ERROR;

/* Initialize the dataset */
Expand Down Expand Up @@ -980,9 +980,9 @@ test_compound_dtype3(hid_t file)

/* Read the dataset back. Temporary buffer is for special platforms like
* Cray */
if (NULL == (tmp = malloc(DIM0 * DIM1 * H5Tget_size(native_type))))
if (NULL == (tmp = malloc((DIM0 * DIM1) * H5Tget_size(native_type))))
TEST_ERROR;
if (NULL == (bkg = calloc(DIM0 * DIM1, sizeof(s1))))
if (NULL == (bkg = calloc((DIM0 * DIM1), sizeof(s1))))
TEST_ERROR;

if (H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
Expand Down Expand Up @@ -1091,9 +1091,9 @@ test_compound_opaque(hid_t file)
TESTING("compound datatype with opaque field");

/* Allocate space for the points & check arrays */
if (NULL == (points = malloc(sizeof(s1) * DIM0 * DIM1)))
if (NULL == (points = (s1 *)malloc((DIM0 * DIM1) * sizeof(s1))))
TEST_ERROR;
if (NULL == (check = calloc(DIM0 * DIM1, sizeof(s1))))
if (NULL == (check = (s1 *)calloc((DIM0 * DIM1), sizeof(s1))))
TEST_ERROR;

/* Initialize the dataset */
Expand Down Expand Up @@ -1206,9 +1206,9 @@ test_compound_opaque(hid_t file)

/* Read the dataset back. Temporary buffer is for special platforms like
* Cray */
if (NULL == (tmp = malloc(DIM0 * DIM1 * H5Tget_size(native_type))))
if (NULL == (tmp = malloc((DIM0 * DIM1) * H5Tget_size(native_type))))
TEST_ERROR;
if (NULL == (bkg = calloc(DIM0 * DIM1, sizeof(s1))))
if (NULL == (bkg = calloc((DIM0 * DIM1), sizeof(s1))))
TEST_ERROR;

if (H5Dread(dataset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
Expand Down Expand Up @@ -1469,9 +1469,9 @@ test_array_dtype(hid_t file)
TESTING("array of compound datatype");

/* Allocate space for the points & check arrays */
if (NULL == (points = malloc(sizeof(s1) * DIM0 * DIM1 * 5)))
if (NULL == (points = (s1 *)malloc((DIM0 * DIM1 * 5) * sizeof(s1))))
TEST_ERROR;
if (NULL == (check = calloc(DIM0 * DIM1 * 5, sizeof(s1))))
if (NULL == (check = (s1 *)calloc((DIM0 * DIM1 * 5), sizeof(s1))))
TEST_ERROR;

/* Initialize the dataset */
Expand Down Expand Up @@ -2428,9 +2428,9 @@ test_refer_dtype2(hid_t file)
TESTING("dataset region reference");

/* Allocate write & read buffers */
if (NULL == (dwbuf = malloc(sizeof(uint8_t) * SPACE2_DIM1 * SPACE2_DIM2)))
if (NULL == (dwbuf = (uint8_t *)malloc((SPACE2_DIM1 * SPACE2_DIM2) * sizeof(uint8_t))))
TEST_ERROR;
if (NULL == (drbuf = calloc(SPACE2_DIM1 * SPACE2_DIM2, sizeof(uint8_t))))
if (NULL == (drbuf = (uint8_t *)calloc((SPACE2_DIM1 * SPACE2_DIM2), sizeof(uint8_t))))
TEST_ERROR;

/* Create dataspace for datasets */
Expand Down
68 changes: 34 additions & 34 deletions test/trefer.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ test_reference_params(void)
wbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
tbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
obuf = calloc(SPACE1_DIM1, sizeof(unsigned));
obuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));

for (i = 0; i < SPACE1_DIM1; i++)
obuf[i] = i * 3;
Expand Down Expand Up @@ -468,10 +468,10 @@ test_reference_obj(void)
MESSAGE(5, ("Testing Object Reference Functions\n"));

/* Allocate write & read buffers */
wbuf = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
ibuf = calloc(SPACE1_DIM1, sizeof(unsigned));
obuf = calloc(SPACE1_DIM1, sizeof(unsigned));
wbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
ibuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));
obuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));

for (i = 0; i < SPACE1_DIM1; i++)
obuf[i] = i * 3;
Expand Down Expand Up @@ -571,7 +571,7 @@ test_reference_obj(void)
VERIFY(obj_type, H5O_TYPE_NAMED_DATATYPE, "H5Rget_obj_type3");

/* Check copying a reference */
wbuf_cp = calloc(1, sizeof(H5R_ref_t));
wbuf_cp = (H5R_ref_t *)calloc(1, sizeof(H5R_ref_t));
ret = H5Rcopy(&wbuf[0], &wbuf_cp[0]);
CHECK(ret, FAIL, "H5Rcopy");

Expand Down Expand Up @@ -838,9 +838,9 @@ test_reference_vlen_obj(void)
MESSAGE(5, ("Testing Object Reference Functions within VLEN type\n"));

/* Allocate write & read buffers */
wbuf = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
ibuf = calloc(SPACE1_DIM1, sizeof(unsigned));
obuf = calloc(SPACE1_DIM1, sizeof(unsigned));
wbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
ibuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));
obuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));

for (i = 0; i < SPACE1_DIM1; i++)
obuf[i] = i * 3;
Expand Down Expand Up @@ -1102,8 +1102,8 @@ test_reference_cmpnd_obj(void)
MESSAGE(5, ("Testing Object Reference Functions within compound type\n"));

/* Allocate write & read buffers */
ibuf = calloc(SPACE1_DIM1, sizeof(unsigned));
obuf = calloc(SPACE1_DIM1, sizeof(unsigned));
ibuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));
obuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));

for (i = 0; i < SPACE1_DIM1; i++)
obuf[i] = i * 3;
Expand Down Expand Up @@ -1411,10 +1411,10 @@ test_reference_region(H5F_libver_t libver_low, H5F_libver_t libver_high)
MESSAGE(5, ("Testing Dataset Region Reference Functions\n"));

/* Allocate write & read buffers */
wbuf = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
dwbuf = (uint8_t *)calloc((size_t)(SPACE2_DIM1 * SPACE2_DIM2), sizeof(uint8_t));
drbuf = (uint8_t *)calloc((size_t)(SPACE2_DIM1 * SPACE2_DIM2), sizeof(uint8_t));
wbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
dwbuf = (uint8_t *)calloc((SPACE2_DIM1 * SPACE2_DIM2), sizeof(uint8_t));
drbuf = (uint8_t *)calloc((SPACE2_DIM1 * SPACE2_DIM2), sizeof(uint8_t));

for (tu8 = dwbuf, i = 0; i < (SPACE2_DIM1 * SPACE2_DIM2); i++)
*tu8++ = (uint8_t)(i * 3);
Expand Down Expand Up @@ -1867,10 +1867,10 @@ test_reference_region_1D(H5F_libver_t libver_low, H5F_libver_t libver_high)
MESSAGE(5, ("Testing 1-D Dataset Region Reference Functions\n"));

/* Allocate write & read buffers */
wbuf = calloc((size_t)SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf = calloc((size_t)SPACE1_DIM1, sizeof(H5R_ref_t));
dwbuf = (uint8_t *)calloc((size_t)SPACE3_DIM1, sizeof(uint8_t));
drbuf = (uint8_t *)calloc((size_t)SPACE3_DIM1, sizeof(uint8_t));
wbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
dwbuf = (uint8_t *)calloc(SPACE3_DIM1, sizeof(uint8_t));
drbuf = (uint8_t *)calloc(SPACE3_DIM1, sizeof(uint8_t));

for (tu8 = dwbuf, i = 0; i < SPACE3_DIM1; i++)
*tu8++ = (uint8_t)(i * 3);
Expand Down Expand Up @@ -3125,10 +3125,10 @@ test_reference_compat_conv(void)
}

/* Allocate write & read buffers */
wbuf_obj = calloc(SPACE1_DIM1, sizeof(hobj_ref_t));
rbuf_obj = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
wbuf_reg = calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf_reg = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
wbuf_obj = (hobj_ref_t *)calloc(SPACE1_DIM1, sizeof(hobj_ref_t));
rbuf_obj = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
wbuf_reg = (hdset_reg_ref_t *)calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf_reg = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));

/* Create dataspace for datasets */
sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL);
Expand Down Expand Up @@ -3434,17 +3434,17 @@ test_reference_perf(void)
MESSAGE(5, ("Testing Object Reference Performance\n"));

/* Allocate write & read buffers */
wbuf = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
obuf = calloc(SPACE1_DIM1, sizeof(unsigned));
ibuf = calloc(SPACE1_DIM1, sizeof(unsigned));
wbuf_deprec = calloc(SPACE1_DIM1, sizeof(hobj_ref_t));
rbuf = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf_deprec = calloc(SPACE1_DIM1, sizeof(hobj_ref_t));
tbuf = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
wbuf_reg = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf_reg = calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
wbuf_reg_deprec = calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf_reg_deprec = calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
wbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
obuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));
ibuf = (unsigned *)calloc(SPACE1_DIM1, sizeof(unsigned));
wbuf_deprec = (hobj_ref_t *)calloc(SPACE1_DIM1, sizeof(hobj_ref_t));
rbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf_deprec = (hobj_ref_t *)calloc(SPACE1_DIM1, sizeof(hobj_ref_t));
tbuf = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
wbuf_reg = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
rbuf_reg = (H5R_ref_t *)calloc(SPACE1_DIM1, sizeof(H5R_ref_t));
wbuf_reg_deprec = (hdset_reg_ref_t *)calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf_reg_deprec = (hdset_reg_ref_t *)calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));

for (i = 0; i < SPACE1_DIM1; i++)
obuf[i] = i * 3;
Expand Down
24 changes: 12 additions & 12 deletions test/trefer_deprec.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,10 @@ test_reference_region(H5F_libver_t libver_low, H5F_libver_t libver_high)
MESSAGE(5, ("Testing Dataset Region Reference Functions\n"));

/* Allocate write & read buffers */
wbuf = calloc((size_t)SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf = malloc(sizeof(hdset_reg_ref_t) * SPACE1_DIM1);
dwbuf = malloc(sizeof(uint8_t) * SPACE2_DIM1 * SPACE2_DIM2);
drbuf = calloc((size_t)(SPACE2_DIM1 * SPACE2_DIM2), sizeof(uint8_t));
wbuf = (hdset_reg_ref_t *)calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf = (hdset_reg_ref_t *)malloc(SPACE1_DIM1 * sizeof(hdset_reg_ref_t));
dwbuf = (uint8_t *)malloc((SPACE2_DIM1 * SPACE2_DIM2) * sizeof(uint8_t));
drbuf = (uint8_t *)calloc((SPACE2_DIM1 * SPACE2_DIM2), sizeof(uint8_t));

/* Create file access property list */
fapl = H5Pcreate(H5P_FILE_ACCESS);
Expand Down Expand Up @@ -1064,10 +1064,10 @@ test_reference_region_1D(H5F_libver_t libver_low, H5F_libver_t libver_high)
MESSAGE(5, ("Testing 1-D Dataset Region Reference Functions\n"));

/* Allocate write & read buffers */
wbuf = calloc((size_t)SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf = malloc(sizeof(hdset_reg_ref_t) * SPACE1_DIM1);
dwbuf = malloc(sizeof(uint8_t) * SPACE3_DIM1);
drbuf = calloc((size_t)SPACE3_DIM1, sizeof(uint8_t));
wbuf = (hdset_reg_ref_t *)calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf = (hdset_reg_ref_t *)malloc(SPACE1_DIM1 * sizeof(hdset_reg_ref_t));
dwbuf = (uint8_t *)malloc(SPACE3_DIM1 * sizeof(uint8_t));
drbuf = (uint8_t *)calloc(SPACE3_DIM1, sizeof(uint8_t));

/* Create the file access property list */
fapl = H5Pcreate(H5P_FILE_ACCESS);
Expand Down Expand Up @@ -1636,10 +1636,10 @@ test_reference_compat(void)
MESSAGE(5, ("Testing Deprecated Object Reference Functions\n"));

/* Allocate write & read buffers */
wbuf_obj = calloc(SPACE1_DIM1, sizeof(hobj_ref_t));
rbuf_obj = malloc(sizeof(hobj_ref_t) * SPACE1_DIM1);
wbuf_reg = calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf_reg = malloc(sizeof(hdset_reg_ref_t) * SPACE1_DIM1);
wbuf_obj = (hobj_ref_t *)calloc(SPACE1_DIM1, sizeof(hobj_ref_t));
rbuf_obj = (hobj_ref_t *)malloc(SPACE1_DIM1 * sizeof(hobj_ref_t));
wbuf_reg = (hdset_reg_ref_t *)calloc(SPACE1_DIM1, sizeof(hdset_reg_ref_t));
rbuf_reg = (hdset_reg_ref_t *)malloc(SPACE1_DIM1 * sizeof(hdset_reg_ref_t));

/* Create file */
fid1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
Expand Down
Loading

0 comments on commit 786b033

Please sign in to comment.