#!/usr/bin/env bash # CI check: clawhdf5-format has no truncating `u64 as usize` cast on a 32-bit # target. HDF5 addresses and lengths are 64-bit; on wasm32 (or any 32-bit # target) such a cast silently wraps an address past 4 GiB onto another part # of the file. File values go through `addr::to_usize` (a clean error) and # in-memory counts through `addr::saturating_usize`. # # Lints with clippy's cast_possible_truncation and fails on any u64 -> usize # finding (other truncations are not checked here), once per feature set # below. Together the sets compile every feature-gated line of the crate that # ci-test.sh builds: features only add code, except `not(feature = ...)` # paths for std/checksum/fast-checksum/szip, which the no-default-features # and default sets cover. szip is left out (it needs libaec), as in # ci-test.sh. # # The sets are linted for wasm32 where they build there. zstd links a C # library that does not build for wasm32, so the set with it is linted for # the host: the lint reports u64 -> usize casts whatever the target's # pointer width, and the crate has no pointer-width-dependent code. # # Usage: # ./scripts/check-32bit-casts.sh # # Prerequisites: # rustup target add wasm32-unknown-unknown set -euo pipefail WASM="wasm32-unknown-unknown" ALL_BUT_ZSTD="parallel,lz4,pcodec,fast-checksum,blake3_hash,plugin-filters,lookup-stats" # target|cargo feature arguments SETS=( "$WASM|--no-default-features" "$WASM|--no-default-features --features std,checksum" "$WASM|" "$WASM|--features $ALL_BUT_ZSTD" "host|--features $ALL_BUT_ZSTD,zstd" ) status=0 for set in "${SETS[@]}"; do target=${set%%|*} args=${set#*|} target_args=() if [ "$target" != host ]; then target_args=(--target "$target") fi echo "==> Checking for truncating u64 -> usize casts in clawhdf5-format ($target: ${args:-default features})" # shellcheck disable=SC2086 # $args is a list of arguments out=$(cargo clippy -p clawhdf5-format "${target_args[@]}" $args \ --message-format short \ -- -A clippy::all -W clippy::cast_possible_truncation 2>&1) || { echo "$out" echo "==> clippy failed" >&2 exit 1 } found=$(grep -F 'casting `u64` to `usize`' <<<"$out" || true) if [ -n "$found" ]; then echo "$found" status=1 fi done if [ "$status" -ne 0 ]; then echo "==> use addr::to_usize (file values) or addr::saturating_usize (in-memory counts)" >&2 exit 1 fi echo "==> no truncating u64 -> usize casts"