Mooncake/mooncake-transfer-engine
BP Cheng b92cf05ff7
[TE] Fix signed-char tolower UB in PCI BDF lowercasing loops (follow-up to #2367/#2488) (#2504)
* [TE] Fix signed-char tolower UB in PCI BDF lowercasing loops

Follow-up to #2367/#2488 (requested in #2488): the C-style PCI-BDF
lowercasing loops still pass a (possibly signed) char to tolower, which
sign-extends bytes > 0x7F to a negative int — UB, since the argument
must be representable as unsigned char or equal EOF.

Convert the 7 sites (topology.cpp, tent cuda/rocm probes, tent
memory_prober, benchmark te/tent backends) to
static_cast<char>(std::tolower(static_cast<unsigned char>(*ch))),
matching the idiom from #2367, and add/normalize <cctype> includes.

The strings are driver-generated PCI bus ids (ASCII), so this is a
correctness/consistency cleanup; behavior is identical for ASCII.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* [TE] Address review: use a uniform te_lower() helper instead of per-site casts

Per @alogfans's review on #2504/#2488: replace the repeated
static_cast<char>(std::tolower(static_cast<unsigned char>(*ch))) at each
PCI-BDF lowercasing loop with a single inline te_lower() helper added to
common.h. Same well-defined behavior, no more mass typecasts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* [TE] Move te_lower() to a lightweight char_util.h to fix tent build

The previous commit put te_lower() in common.h, but pulling common.h into
the TENT TUs collides with tent/common/types.h, which defines
LOCAL_SEGMENT_ID as a macro `#define LOCAL_SEGMENT_ID (0ull)` while
common.h declares `const static int LOCAL_SEGMENT_ID = 0;` — the macro
expands inside the declaration and breaks the build (build-flags CI).

Put te_lower() in a new minimal header char_util.h that only pulls in
<cctype>, and include it at all 7 sites instead of common.h. No collision
with the tent macro, still a single uniform helper per @alogfans's review.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* [TE] Name the helper to_lower() to match the std::tolower convention

Rename te_lower() -> to_lower() in char_util.h and the 7 call sites. The
repo has no existing named lowercase helper to reuse, and to_lower reads
naturally alongside std::tolower.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-06-18 20:33:23 +09:00
..
benchmark [TE] Fix signed-char tolower UB in PCI BDF lowercasing loops (follow-up to #2367/#2488) (#2504) 2026-06-18 20:33:23 +09:00
example [TE] add device API support (#2333) 2026-06-05 16:00:07 +08:00
include [TE] Fix signed-char tolower UB in PCI BDF lowercasing loops (follow-up to #2367/#2488) (#2504) 2026-06-18 20:33:23 +09:00
nvlink-allocator [CI/Build] Switch WITH_NVIDIA_PEERMEM to env variable (#2066) 2026-05-20 10:05:21 +08:00
rust fix(transfer-engine): add missing empty checks for batch methods (#2046) 2026-05-07 16:00:54 +08:00
scripts refactor: unify fabric allocator plumbing (#2028) 2026-05-06 10:13:37 +08:00
src [TE] Fix signed-char tolower UB in PCI BDF lowercasing loops (follow-up to #2367/#2488) (#2504) 2026-06-18 20:33:23 +09:00
tent [TE] Fix signed-char tolower UB in PCI BDF lowercasing loops (follow-up to #2367/#2488) (#2504) 2026-06-18 20:33:23 +09:00
tests [TE] Add MC_TE_FILTERS env var for IB device whitelist (#2495) 2026-06-18 10:14:35 +08:00
ubshmem-allocator refactor: unify fabric allocator plumbing (#2028) 2026-05-06 10:13:37 +08:00
CMakeLists.txt [Build] Allow building tebench without USE_TENT (#2322) 2026-06-04 10:00:08 +08:00
fabric_allocator.cmake Build tent (#2089) 2026-05-28 20:30:20 +08:00