Qualify C math functions - #2051
Conversation
Use <cmath> and std::isfinite/std::signbit for floating point checks in JSON and metrics code. Some C++ standard libraries do not expose these functions unqualified through the global namespace.
There was a problem hiding this comment.
Pull request overview
This PR improves portability of floating-point checks by switching from C <math.h>/unqualified functions to C++ <cmath> with std::isfinite/std::signbit, aligning the JSON and metrics code with standard C++ usage and fixing build issues on some BSD toolchains.
Changes:
- Replaced
<math.h>with<cmath>in metrics and JSON implementation code. - Qualified floating-point checks with
std::isfinitein metrics submission and JSON number construction. - Updated JSON float parsing tests to use
std::signbitand added<cmath>include.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/vcpkg/metrics.cpp | Uses <cmath> and std::isfinite for elapsed-time validation in metrics submission. |
| src/vcpkg/base/json.cpp | Uses <cmath> and std::isfinite to validate JSON number values at construction time. |
| src/vcpkg-test/json.cpp | Uses <cmath> and std::signbit to validate signed zero behavior in float parsing tests. |
|
This was previously litigated in #359 (See in particular the ultimate outcome #359 (comment) ) We need an understanding of why math.h is broken on these. We are intentionally trying to use the C / macro version. |
|
I think you intended that link to take me to a specific comment, but it just takes me to the closed issue. The last comment on the issue says:
I don't know what to say about 2 or 3, but for 1 you can reproduce it on a build on BSD using a VM. I created the action LegalizeAdulthood/vcpkg-github-cache to make caching with NuGet packages easy. While getting BSD support working, I ended up having to patch vcpkg for this scenario. You will find an example workflow of building under BSD in the ReadMe for that action. Since vcpkg doesn't yet currently provide a vcpkg binary for BSD environments, the tool has to be bootstrapped by building from source. That's when you run into this problem. You're right to say that if you include My action patches vcpkg-tool on BSD to get around this problem and that is the origin of this PR. |
Emphasis mine: before "flipping the bit" on this I think we need a more concrete understanding of what's happening. |
|
(The reason I'm being such a stickler about this is that this fixes that broken BSD implementation and breaks the unknown set of happy customers who are currently working today. I don't think it's unreasonable to ask someone who is seriously trying to support one of these BSDs to describe concretely what happens on that platform by, for example, figuring out where the correct macros are supposed to come from on that platform or showing the specific condition their standard library headers are using to snip that off) |
Perfectly understandable. I agree that it's best to be conservative here and the proposed PR needs more validation beyond "it builds for me in a VM" before being accepted. |
|
Since the vcpkg team can't advance this right now I'm going to mark this as draft. Please feel free to un-draft if you find out how the broken BSD implementation is broken. (My guess is that they're trying to use libstdc++, which mostly assumes glibc, with a different C runtime. They do an |
|
Draft is fine and is realistically more accurate for where we are. I'll put Codex on the job and see what it can determine about the root cause of the failure. |
|
Richard Thomson (@LegalizeAdulthood) I don't know if that's the exact issue you're experiencing, but when I was testing on BSD systems, by just using the builtin toolchain on FreeBSD worked just fine, but for NetBSD and DragonFly BSD (and possibly OpenBSD I don't remember exactly) the workaround was installing some more newer toolchain like GCC 14 from the official repositories. I remember debugging this exact issue and it boils down to most C++ standard libraries are gonna ship their own |
|
Thank you, that is good information. |
They generally need to |
Use and std::isfinite/std::signbit for floating point checks in JSON and metrics code. Some C++ standard libraries do not expose these functions unqualified through the global namespace.
This fixes some build errors on BSD