edefend opened a new pull request, #12: URL: https://github.com/apache/trafficserver-libswoc/pull/12
`Scalar<N>` counts in `int` unless told otherwise, and the scaled value it returns (count * N) comes back as an `int` too. `MemArena` rounds block sizes through `Scalar<4096>` and `Scalar<16>`, so an allocation of 2GB or more turns into a negative `int`, then a huge `size_t`. The result is `std::bad_alloc` or a crash in `make_block()`. Three 1GB allocations from one arena are enough to hit it. This changes the default counter type from `int` to `intmax_t`, which is already the type of the scale `N`. Scalars that name their counter type (`Scalar<N, off_t>` and so on) are unchanged. **Test:** `Scalar default counter` checks that a `Scalar<4096>` and a `Scalar<16>` hold 3GB and convert back to a `size_t`. It fails on master (`-1073741824 == 3221225472`) and passes with the change. I also ran an arena that allocates three 1GB blocks: `bad_alloc` on master, fine with this change. **What this could break** - **Size and ABI.** A `Scalar<N>` with the default counter goes from 4 to 8 bytes. A struct that holds one changes layout, and code built against the old headers has to be recompiled. In this repo only `MemArena` uses default-counter Scalars, and as aliases (`Page`, `QuarterPage`, `Paragraph`), not as members. In Traffic Server I found one use, `HdrHeapMarshalBlocks` in `HdrHeap.h`, which is a `static constexpr` plus temporaries, so no layout change there. Third-party code that stores default `Scalar`s would be affected. - **Type identity.** `Scalar<N>` is now a different type from `Scalar<N, int>`. They still convert to each other, but a function that takes `Scalar<N, int>` will not deduce from a default `Scalar<N>`. I found none in libswoc or Traffic Server. - **Narrowing.** `int x = some_scalar;` now narrows from `intmax_t` to `int` implicitly. That is silent unless the build uses `-Wconversion` or `-Wshorten-64-to-32` (Traffic Server does not). Traffic Server's five usage patterns of `HdrHeapMarshalBlocks` compile clean with `-Wall -Wextra -Werror` on gcc 12 and clang, and give the same values. I did not build all of Traffic Server. - **No more silent wrap.** A count times scale above `INT_MAX` used to wrap and now doesn't. Nothing should depend on the wrap. - **Speed.** Negligible on 64-bit targets. **Testing:** `test_libswoc` with gcc 12: the Scalar and MemArena tests pass. Three other tests (`test_swoc_file`, `ex_TextView` and `test_Errata`, the last one intermittently) fail the same way on unmodified master in my environment, so I left them alone. The same change applies to `lib/swoc` in apache/trafficserver; I'll send that separately. 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
