-
Notifications
You must be signed in to change notification settings - Fork 8.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Find a safer safe math #4013
Comments
I'd like to note that a number of the places I've seen "safe math" functions used in the VT code, that's actually not the behavior we need. If something overflows, what you often want is for the result to be clamped rather than having it fail. I've seen the same sort of problems with the safe conversion functions (e.g. So before we start replacing everything with a new library, it may be worth auditing the existing safe math usage to see if that's really the behavior we want. |
Good input. I'm trying to discern if that should be a separate effort from this one or all in the same. |
@j4james, I think that should be the same effort as this one or that we should add some functions when settling on a safe math library. I think we could make some templates like the ones in SafeInt.hpp above that are basically like add_clamped that will try to use the add safe math and on failure, just clamp it to the std::numeric_limits().max value. And then walk through the VT adapters and use that instead of the incorrect safemath usages. |
For clamped (aka "saturation-") arithmetics not much can be found that's written in C++ and readily available. (…apart from the first result on Google of course - StefanHamminga's library.) Arguably the best option I'm personally aware of is Chrome's base/numerics library btw (BSD license). |
The Chrome one looks interesting but it would probably take some adaptation to get it into a format we could consume. @DHowett-MSFT, opinion? I'm still mostly inclined to just use the MSVC-related SafeInt with some |
@miniksa The reason I mentioned the Chromium one is because proper saturation arithmetic is of course just as annoying to implement as e.g. SafeInt itself (since in both cases you need to carefully check for overflows even in case of e.g. 64-bit multiplications). |
A couple of points I wanted to make regarding this issue...
|
I would imagine what we would do is create Yes, most of the conhost system uses shorts. I did want to expand those to larger values to some degree internally such that we could address a more "limitless" backing buffer size in the future because it's been a big request in the past. But perhaps it's still reasonable to maintain that VT only applies to Generally speaking, I'm fine if the VT stuff would use |
Though, glancing at the saturating math library from @lhecker, maybe I should just run it through our licensing process to see if we could just be approved to use that... that sounds easier. Then maybe we include both SafeInt.h for regular safe math and saturating for saturating math. |
@miniksa I wrote a saturating integer class a long time ago and extracted parts of the logic into a Gist. |
@miniksa From a cursory look at the SafeInt lib, I don't think using something like But I'm probably fixating on performance issues that aren't really a big deal. The most important thing for me is readability, and it sounds like that is not a problem for any of these libs. |
@j4james, not having to clamp after every operation but finally clamping on the transition is part of why I was trying to use I don't think any of these will drastically ruin performance. If it does, we can optimize after we fix the problem at hand. |
The problem with Anyway, if we can fix all of that just by changing a few variable types to |
|
@lhecker, @j4james, as of right now, I'm liking the "lift the chromium numerics library" option the best because it provides a consistent and unified way of handling all of these math problems. I'm looking into the best way we can include it into our repository and transition to it. I think it will handle all of our concerns. |
## Summary of the Pull Request <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist * [x] Closes #4013 * [x] I work here. * [x] Existing tests should be OK. Real changes, just adding a lib to use. * [x] Couldn't find any existing docs about intsafe. * [x] Am core contributor. ## Detailed Description of the Pull Request / Additional comments * [x] Can we remove min/max completely or rename it in the two projects where it had to be reintroduced? This is now moved into #4152 * [x] How many usages of the old safe math are there? **79** * [x] If not a ton, can we migrate them here or in a follow on PR? This is now moved into #4153 Files with old safe math: - TerminalControl: TSFInputControl.cpp - TerminalCore: TerminalDispatch.cpp - TerminalCore: TerminalSelection.cpp - Host: directio.cpp - RendererGdi: invalidate.cpp - RendererGdi: math.cpp - RendererGdi: paint.cpp - RendererVt: paint.cpp - TerminalAdapter: adaptDispatch.cpp - Types: viewport.cpp - Types: WindowUiaProviderBase.cpp ## Validation Steps Performed
intsafe.h
is apparently included in the SDK but is cumbersome and somewhat gross.For instance, it always returns an
HRESULT
.But the bigger problem is that it's neither
constexpr
nornoexcept
which makes an issue when combined with the extended rollout of cppcorechecks through audit mode.This issue represents switching over to a more modern safe math class.
https://github.com/dcleblanc/SafeInt/blob/master/SafeInt.hpp is the candidate that I found as most likely at this moment.
The text was updated successfully, but these errors were encountered: