Skip to content

fix remaining clang-tidy findings in the C sources (#353)#362

Open
ericsson-kuma wants to merge 1 commit into
GrapheneOS:mainfrom
ericsson-kuma:fix-tidy-findings
Open

fix remaining clang-tidy findings in the C sources (#353)#362
ericsson-kuma wants to merge 1 commit into
GrapheneOS:mainfrom
ericsson-kuma:fix-tidy-findings

Conversation

@ericsson-kuma

Copy link
Copy Markdown

Fixes the two findings that were left in #353.

h_malloc.c:432 clang-analyzer-security.ArrayBound

False positive. slots is limited to MAX_SLAB_SLOT_COUNT (256) via size_class_slots, so (slots - 1) / U64_WIDTH <= 3 and i always stays within the 4 bitmap words; random_index is bounded by slots as well. The analyzer can't see either bound across translation units (slots is loaded from a u16 field and get_random_u16_uniform is defined in random.c), and I couldn't find a behavior-preserving restructure that expresses them locally, so this uses a targeted NOLINT with a comment rather than disabling the check for the whole project.

util.h:51 bugprone-narrowing-conversions

Made the conversion explicit with a cast as suggested. int64_t rather than long long because casting to the higher-rank long long trips bugprone-misplaced-widening-cast (u64 is unsigned long on LP64). The same bits reach __builtin_ffsll as before.

Verification (clang-tidy 21.1.8):

  • before: the C sources line of make tidy fails with the h_malloc.c:432 ArrayBound error, and clang-tidy -header-filter='.*' h_malloc.c util.c additionally reports the util.h:51 narrowing error
  • after: the C sources line of make tidy passes and the -header-filter='.*' run is clean
  • make && make test passes before and after; the rebuilt libhardened_malloc.so is byte-identical to the pre-change build (comments and an explicit cast only)

Note: with the C sources passing, make tidy now reaches its second clang-tidy invocation and surfaces 8 pre-existing findings in new.cc (misc-use-anonymous-namespace x4, misc-const-correctness x4 - the latter's pointer analysis is new in clang-tidy 21) that were previously hidden by the earlier failure. Those look like a style/config decision so I left them out of scope here.

Refs #353

Fix the two findings left in issue GrapheneOS#353:

h_malloc.c:432 clang-analyzer-security.ArrayBound: false positive.
slots is limited to MAX_SLAB_SLOT_COUNT (256) via size_class_slots, so
(slots - 1) / U64_WIDTH <= 3 and i always stays within the 4 bitmap
words; random_index is bounded by slots as well. The analyzer can't see
either bound across translation units (slots is loaded from a u16 field
and get_random_u16_uniform is defined in random.c), so suppress the
report with a targeted NOLINT instead of disabling the check.

util.h:51 bugprone-narrowing-conversions: make the conversion explicit
with a cast. int64_t rather than long long since casting to the higher
rank long long trips bugprone-misplaced-widening-cast. The same bits
reach __builtin_ffsll as before and the built library is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant