Skip to content

Commit

Permalink
Be a bit safer with signed arithmetic, thus quieting some signed-over…
Browse files Browse the repository at this point in the history
…flow warnings from GCC (HDFGroup#1706)

* Avoid a signed overflow: check the range of `entry_ptr->age` before
increasing it instead of increasing it and then checking the range.
This quiets a GCC warning.

* Avoid the potential for signed overflow by rewriting expressions
`MAX(0, fwidth - n)` as `MAX(n, fwidth) - n` for various `n`.
This change quiets some GCC warnings.

* Change some local variables that cannot take sensible negative values
from signed to unsigned.  This quiets GCC warnings about potential
signed overflow.

* In a handful of instances, check the range of a signed integer before
increasing/decreasing it, just in case the increase/decrease overflows.
This quiets a handful of GCC signed-overflow warnings.

* Committing clang-format changes

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and jhendersonHDF committed May 8, 2022
1 parent e74e0d4 commit 39b118f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/H5Bdbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ H5B_debug(H5F_t *f, haddr_t addr, FILE *stream, int indent, int fwidth, const H5
*/
for (u = 0; u < bt->nchildren; u++) {
HDfprintf(stream, "%*sChild %d...\n", indent, "", u);
HDfprintf(stream, "%*s%-*s %" PRIuHADDR "\n", indent + 3, "", MAX(0, fwidth - 3),
HDfprintf(stream, "%*s%-*s %" PRIuHADDR "\n", indent + 3, "", MAX(3, fwidth) - 3,
"Address:", bt->child[u]);

/* If there is a key debugging routine, use it to display the left & right keys */
if (type->debug_key) {
/* Decode the 'left' key & print it */
HDfprintf(stream, "%*s%-*s\n", indent + 3, "", MAX(0, fwidth - 3), "Left Key:");
HDfprintf(stream, "%*s%-*s\n", indent + 3, "", MAX(3, fwidth) - 3, "Left Key:");
HDassert(H5B_NKEY(bt, shared, u));
(void)(type->debug_key)(stream, indent + 6, MAX(0, fwidth - 6), H5B_NKEY(bt, shared, u), udata);
(void)(type->debug_key)(stream, indent + 6, MAX(6, fwidth) - 6, H5B_NKEY(bt, shared, u), udata);

/* Decode the 'right' key & print it */
HDfprintf(stream, "%*s%-*s\n", indent + 3, "", MAX(0, fwidth - 3), "Right Key:");
HDfprintf(stream, "%*s%-*s\n", indent + 3, "", MAX(3, fwidth) - 3, "Right Key:");
HDassert(H5B_NKEY(bt, shared, u + 1));
(void)(type->debug_key)(stream, indent + 6, MAX(0, fwidth - 6), H5B_NKEY(bt, shared, u + 1),
(void)(type->debug_key)(stream, indent + 6, MAX(6, fwidth) - 6, H5B_NKEY(bt, shared, u + 1),
udata);
} /* end if */
} /* end for */
Expand Down
5 changes: 3 additions & 2 deletions src/H5Cimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -2737,10 +2737,11 @@ H5C__prep_for_file_close__setup_image_entries_array(H5C_t *cache_ptr)
*/
if (entry_ptr->type->id == H5AC_PREFETCHED_ENTRY_ID) {
image_entries[u].type_id = entry_ptr->prefetch_type_id;
image_entries[u].age = entry_ptr->age + 1;

if (image_entries[u].age > H5AC__CACHE_IMAGE__ENTRY_AGEOUT__MAX)
if (entry_ptr->age >= H5AC__CACHE_IMAGE__ENTRY_AGEOUT__MAX)
image_entries[u].age = H5AC__CACHE_IMAGE__ENTRY_AGEOUT__MAX;
else
image_entries[u].age = entry_ptr->age + 1;
} /* end if */
else {
image_entries[u].type_id = entry_ptr->type->id;
Expand Down

0 comments on commit 39b118f

Please sign in to comment.