Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a few issues noted by LGTM #1421

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/H5Dlayout.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ H5D__layout_meta_size(const H5F_t *f, const H5O_layout_t *layout, hbool_t includ
ret_value++;

/* Dimension sizes */
ret_value += layout->u.chunk.ndims * layout->u.chunk.enc_bytes_per_dim;
ret_value += layout->u.chunk.ndims * (size_t)layout->u.chunk.enc_bytes_per_dim;

/* Type of chunk index */
ret_value++;
Expand Down
29 changes: 15 additions & 14 deletions src/H5Znbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ H5Z__filter_nbit(unsigned flags, size_t cd_nelmts, const unsigned cd_values[], s

/* input; decompress */
if (flags & H5Z_FLAG_REVERSE) {
size_out = d_nelmts * cd_values[4]; /* cd_values[4] stores datatype size */
size_out = d_nelmts * (size_t)cd_values[4]; /* cd_values[4] stores datatype size */

/* allocate memory space for decompressed buffer */
if (NULL == (outbuf = (unsigned char *)H5MM_malloc(size_out)))
Expand Down Expand Up @@ -1170,16 +1170,17 @@ H5Z__nbit_decompress_one_array(unsigned char *data, size_t data_offset, unsigned

n = total_size / p.size;
for (i = 0; i < n; i++)
H5Z__nbit_decompress_one_atomic(data, data_offset + i * p.size, buffer, j, buf_len, &p);
H5Z__nbit_decompress_one_atomic(data, data_offset + i * (size_t)p.size, buffer, j, buf_len,
&p);
break;

case H5Z_NBIT_ARRAY:
base_size = parms[*parms_index]; /* read in advance */
n = total_size / base_size; /* number of base_type elements inside the array datatype */
begin_index = *parms_index;
for (i = 0; i < n; i++) {
if (H5Z__nbit_decompress_one_array(data, data_offset + i * base_size, buffer, j, buf_len,
parms, parms_index) < 0)
if (H5Z__nbit_decompress_one_array(data, data_offset + i * (size_t)base_size, buffer, j,
buf_len, parms, parms_index) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_CANTFILTER, FAIL, "can't decompress array")
*parms_index = begin_index;
}
Expand All @@ -1190,8 +1191,8 @@ H5Z__nbit_decompress_one_array(unsigned char *data, size_t data_offset, unsigned
n = total_size / base_size; /* number of base_type elements inside the array datatype */
begin_index = *parms_index;
for (i = 0; i < n; i++) {
if (H5Z__nbit_decompress_one_compound(data, data_offset + i * base_size, buffer, j, buf_len,
parms, parms_index) < 0)
if (H5Z__nbit_decompress_one_compound(data, data_offset + i * (size_t)base_size, buffer, j,
buf_len, parms, parms_index) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_CANTFILTER, FAIL, "can't decompress compound")
*parms_index = begin_index;
}
Expand Down Expand Up @@ -1291,7 +1292,7 @@ H5Z__nbit_decompress(unsigned char *data, unsigned d_nelmts, unsigned char *buff
FUNC_ENTER_STATIC

/* may not have to initialize to zeros */
HDmemset(data, 0, d_nelmts * parms[4]);
HDmemset(data, 0, d_nelmts * (size_t)parms[4]);

/* initialization before the loop */
j = 0;
Expand All @@ -1309,7 +1310,7 @@ H5Z__nbit_decompress(unsigned char *data, unsigned d_nelmts, unsigned char *buff
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "invalid datatype precision/offset")

for (i = 0; i < d_nelmts; i++)
H5Z__nbit_decompress_one_atomic(data, i * p.size, buffer, &j, &buf_len, &p);
H5Z__nbit_decompress_one_atomic(data, i * (size_t)p.size, buffer, &j, &buf_len, &p);
break;

case H5Z_NBIT_ARRAY:
Expand Down Expand Up @@ -1468,16 +1469,16 @@ H5Z__nbit_compress_one_array(unsigned char *data, size_t data_offset, unsigned c
p.offset = parms[(*parms_index)++];
n = total_size / p.size;
for (i = 0; i < n; i++)
H5Z__nbit_compress_one_atomic(data, data_offset + i * p.size, buffer, j, buf_len, &p);
H5Z__nbit_compress_one_atomic(data, data_offset + i * (size_t)p.size, buffer, j, buf_len, &p);
break;

case H5Z_NBIT_ARRAY:
base_size = parms[*parms_index]; /* read in advance */
n = total_size / base_size; /* number of base_type elements inside the array datatype */
begin_index = *parms_index;
for (i = 0; i < n; i++) {
H5Z__nbit_compress_one_array(data, data_offset + i * base_size, buffer, j, buf_len, parms,
parms_index);
H5Z__nbit_compress_one_array(data, data_offset + i * (size_t)base_size, buffer, j, buf_len,
parms, parms_index);
*parms_index = begin_index;
}
break;
Expand All @@ -1487,8 +1488,8 @@ H5Z__nbit_compress_one_array(unsigned char *data, size_t data_offset, unsigned c
n = total_size / base_size; /* number of base_type elements inside the array datatype */
begin_index = *parms_index;
for (i = 0; i < n; i++) {
H5Z__nbit_compress_one_compound(data, data_offset + i * base_size, buffer, j, buf_len, parms,
parms_index);
H5Z__nbit_compress_one_compound(data, data_offset + i * (size_t)base_size, buffer, j, buf_len,
parms, parms_index);
*parms_index = begin_index;
}
break;
Expand Down Expand Up @@ -1574,7 +1575,7 @@ H5Z__nbit_compress(unsigned char *data, unsigned d_nelmts, unsigned char *buffer
p.offset = parms[7];

for (i = 0; i < d_nelmts; i++)
H5Z__nbit_compress_one_atomic(data, i * p.size, buffer, &new_size, &buf_len, &p);
H5Z__nbit_compress_one_atomic(data, i * (size_t)p.size, buffer, &new_size, &buf_len, &p);
break;

case H5Z_NBIT_ARRAY:
Expand Down
6 changes: 3 additions & 3 deletions src/H5Zscaleoffset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ H5Z__filter_scaleoffset(unsigned flags, size_t cd_nelmts, const unsigned cd_valu
p.minbits = minbits;

/* calculate size of output buffer after decompression */
size_out = d_nelmts * p.size;
size_out = d_nelmts * (size_t)p.size;

/* allocate memory space for decompressed buffer */
if (NULL == (outbuf = (unsigned char *)H5MM_malloc(size_out)))
Expand Down Expand Up @@ -1403,7 +1403,7 @@ H5Z__scaleoffset_convert(void *buf, unsigned d_nelmts, unsigned dtype_size)
unsigned char *buffer, temp;

buffer = (unsigned char *)buf;
for (i = 0; i < d_nelmts * dtype_size; i += dtype_size)
for (i = 0; i < d_nelmts * (size_t)dtype_size; i += dtype_size)
for (j = 0; j < dtype_size / 2; j++) {
/* swap pair of bytes */
temp = buffer[i + j];
Expand Down Expand Up @@ -1681,7 +1681,7 @@ H5Z__scaleoffset_decompress(unsigned char *data, unsigned d_nelmts, unsigned cha
unsigned buf_len;

/* must initialize to zeros */
for (i = 0; i < d_nelmts * p.size; i++)
for (i = 0; i < d_nelmts * (size_t)p.size; i++)
data[i] = 0;

/* initialization before the loop */
Expand Down
6 changes: 3 additions & 3 deletions tools/lib/h5diff_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,7 @@ diff_ldouble_element(unsigned char *mem1, unsigned char *mem2, hsize_t elem_idx,
opts->print_percentage = 0;
print_pos(opts, elem_idx, 0);
if (print_data(opts)) {
parallel_print(F_FORMAT, temp1_double, temp2_double, ABS(temp1_double - temp2_double));
parallel_print(LD_FORMAT, temp1_double, temp2_double, ABS(temp1_double - temp2_double));
}
nfound++;
}
Expand Down Expand Up @@ -2110,7 +2110,7 @@ diff_ldouble_element(unsigned char *mem1, unsigned char *mem2, hsize_t elem_idx,
opts->print_percentage = 0;
print_pos(opts, elem_idx, 0);
if (print_data(opts)) {
parallel_print(F_FORMAT, temp1_double, temp2_double, ABS(temp1_double - temp2_double));
parallel_print(LD_FORMAT, temp1_double, temp2_double, ABS(temp1_double - temp2_double));
}
nfound++;
}
Expand Down Expand Up @@ -2157,7 +2157,7 @@ diff_ldouble_element(unsigned char *mem1, unsigned char *mem2, hsize_t elem_idx,
opts->print_percentage = 0;
print_pos(opts, elem_idx, 0);
if (print_data(opts)) {
parallel_print(F_FORMAT, temp1_double, temp2_double, ABS(temp1_double - temp2_double));
parallel_print(LD_FORMAT, temp1_double, temp2_double, ABS(temp1_double - temp2_double));
}
nfound++;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/lib/h5diff_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ build_match_list_attrs(hid_t loc1_id, hid_t loc2_id, table_attrs_t **table_out,
}

if (opts->mode_verbose_level >= 1)
parallel_print("Attributes status: %d common, %d only in obj1, %d only in obj2\n",
parallel_print("Attributes status: %zu common, %zu only in obj1, %zu only in obj2\n",
table_lp->nattrs - table_lp->nattrs_only1 - table_lp->nattrs_only2,
table_lp->nattrs_only1, table_lp->nattrs_only2);

Expand Down
2 changes: 1 addition & 1 deletion tools/lib/h5tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ h5tools_fopen(const char *fname, unsigned flags, hid_t fapl_id, hbool_t use_spec
* as TRUE, we should return failure now since the file couldn't be opened with
* the VFL driver/VOL connector that was set on the FAPL by the caller.
*/
if (fid < 0 && use_specific_driver)
if (use_specific_driver)
H5TOOLS_GOTO_ERROR(H5I_INVALID_HID, "failed to open file using specified FAPL");

/*
Expand Down
4 changes: 2 additions & 2 deletions tools/lib/h5tools_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -2486,7 +2486,7 @@ h5tools_print_datatype(FILE *stream, h5tools_str_t *buffer, const h5tool_format_
ctx->need_prefix = TRUE;

h5tools_str_reset(buffer);
h5tools_str_append(buffer, "OPAQUE_SIZE \"%s\";", size);
h5tools_str_append(buffer, "OPAQUE_SIZE \"%zu\";", size);
h5tools_render_element(stream, info, ctx, buffer, &curr_pos, (size_t)ncols, (hsize_t)0,
(hsize_t)0);
}
Expand Down Expand Up @@ -2996,7 +2996,7 @@ h5tools_dump_oid(FILE *stream, const h5tool_format_t *info, h5tools_context_t *c
ctx->need_prefix = TRUE;

h5tools_str_reset(&buffer);
h5tools_str_append(&buffer, "%s %s %d %s", OBJID, BEGIN, oid, END);
h5tools_str_append(&buffer, "%s %s %" PRId64 " %s", OBJID, BEGIN, oid, END);
h5tools_render_element(stream, info, ctx, &buffer, &curr_pos, (size_t)ncols, (hsize_t)0, (hsize_t)0);

h5tools_str_close(&buffer);
Expand Down
4 changes: 2 additions & 2 deletions tools/src/h5dump/h5dump_ddl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ attr_search(hid_t oid, const char *attr_name, const H5A_info_t H5_ATTR_UNUSED *a
j = (int)HDstrlen(op_name) - 1;
/* find the last / */
while (j >= 0) {
if (op_name[j] == '/' && (j == 0 || (j > 0 && op_name[j - 1] != '\\')))
if (op_name[j] == '/' && (j == 0 || (op_name[j - 1] != '\\')))
Copy link
Collaborator Author

@jhendersonHDF jhendersonHDF Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM noticed the previous checks of while (j >= 0) and if (... (j == 0 ...) make the j > 0 check redundant.

break;
j--;
}
Expand Down Expand Up @@ -1541,7 +1541,7 @@ handle_attributes(hid_t fid, const char *attr, void H5_ATTR_UNUSED *data, int H5

/* find the last / */
while (j >= 0) {
if (attr[j] == '/' && (j == 0 || (j > 0 && attr[j - 1] != '\\')))
if (attr[j] == '/' && (j == 0 || (attr[j - 1] != '\\')))
break;
j--;
}
Expand Down
13 changes: 9 additions & 4 deletions tools/src/h5dump/h5dump_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ xml_name_to_XID(hid_t loc_id, const char *str, char *outstr, int outlen, int gen
if (outlen < 22)
return 1;

H5_CHECK_OVERFLOW(outlen, int, size_t);

lookup_ret = ref_path_table_lookup(str, &obj_token);
if (lookup_ret < 0) {
if (HDstrlen(str) == 0) {
Expand All @@ -600,7 +602,7 @@ xml_name_to_XID(hid_t loc_id, const char *str, char *outstr, int outlen, int gen
ref_path_table_gen_fake(str, &obj_token);

H5Otoken_to_str(loc_id, &obj_token, &obj_tok_str);
HDsnprintf(outstr, outlen, "xid_%s", obj_tok_str);
HDsnprintf(outstr, (size_t)outlen, "xid_%s", obj_tok_str);
H5free_memory(obj_tok_str);

return 0;
Expand All @@ -615,7 +617,7 @@ xml_name_to_XID(hid_t loc_id, const char *str, char *outstr, int outlen, int gen
ref_path_table_gen_fake(str, &obj_token);

H5Otoken_to_str(loc_id, &obj_token, &obj_tok_str);
HDsnprintf(outstr, outlen, "xid_%s", obj_tok_str);
HDsnprintf(outstr, (size_t)outlen, "xid_%s", obj_tok_str);
H5free_memory(obj_tok_str);

return 0;
Expand All @@ -627,7 +629,7 @@ xml_name_to_XID(hid_t loc_id, const char *str, char *outstr, int outlen, int gen
}

H5Otoken_to_str(loc_id, &obj_token, &obj_tok_str);
HDsnprintf(outstr, outlen, "xid_%s", obj_tok_str);
HDsnprintf(outstr, (size_t)outlen, "xid_%s", obj_tok_str);
H5free_memory(obj_tok_str);

return 0;
Expand Down Expand Up @@ -3598,7 +3600,10 @@ xml_dump_fill_value(hid_t dcpl, hid_t type)
h5tools_str_reset(&buffer);
h5tools_str_append(&buffer, "\"");
for (i = 0; i < sz; i++) {
h5tools_str_append(&buffer, "%x ", *(unsigned int *)buf + (i * sizeof(unsigned int)));
unsigned long val = *(unsigned int *)buf + (i * sizeof(unsigned int));

H5_CHECK_OVERFLOW(val, unsigned long, unsigned);
h5tools_str_append(&buffer, "%x ", (unsigned)val);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%x specifier takes an unsigned int, but the arithmetic results in an unsigned long. Convert to unsigned after overflow check.

}
h5tools_str_append(&buffer, "\"");
h5tools_render_element(rawoutstream, outputformat, &ctx, &buffer, &curr_pos,
Expand Down
2 changes: 1 addition & 1 deletion tools/src/h5perf/sio_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ run_test_loop(struct options *opts)
}

/* print size information */
output_report("Transfer Buffer Size (bytes): %d\n", buf_bytes);
output_report("Transfer Buffer Size (bytes): %zu\n", buf_bytes);
output_report("File Size(MB): %.2f\n", ((double)parms.num_bytes) / ONE_MB);

print_indent(0);
Expand Down