Mooncake/mooncake-transfer-engine/benchmark
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
..
CMakeLists.txt [TE][Sunrise][Feat] Enable Sunrise VRAM support in tebench for the TENT backend (#2452) 2026-06-15 23:15:35 +08:00
bench_runner.h [TE] Add TENT codebase to main (Phase 1: structural import) (#1213) 2025-12-22 19:22:13 +08:00
main.cpp [TE][Sunrise][Feat] Enable Sunrise support in the classic transfer engine path (#2290) 2026-06-12 17:22:51 +08:00
te_backend.cpp [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
te_backend.h [TE][Sunrise][Feat] Enable Sunrise support in the classic transfer engine path (#2290) 2026-06-12 17:22:51 +08:00
tent_backend.cpp [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_backend.h [TENT] feat: add per-request transport_hint for fine-grained transport selection in tent (#2339) 2026-06-08 21:15:05 +08:00
utils.cpp [TE][Sunrise][Feat] Enable Sunrise VRAM support in tebench for the TENT backend (#2452) 2026-06-15 23:15:35 +08:00
utils.h [TE][Sunrise][Feat] Enable Sunrise VRAM support in tebench for the TENT backend (#2452) 2026-06-15 23:15:35 +08:00