Skip to content
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

Add integer overflow detection feature #78

Merged
merged 5 commits into from
Mar 4, 2022

Conversation

fargies
Copy link

@fargies fargies commented Mar 3, 2022

Rework of #75

  • added bool overflows(csubstr) function
  • added fmt::overflow_checked() formatter

Overhead is 10-15% (when it was more 15-20% with previous implementation), I think overhead on u8 and i8 is more important just because there are more numbers with 3 digits spread on the domain (60%) so it has to compare bytes more often.

Also did not benchmark with hex/oct/bin encodings.

overflow_checked
overflow_checked.json.txt

I did not add atoi_rc, it felt a bit too much, instead it may be interesting to implement a couple helpers at RapidYaml level (overriding operator>>), ex:

// on error (overflow included) v will default to "10"
node["test"] >> fmt::default_to(v, 10);

// fetch v check if it's in range [0, 100] on error or out of range default to "10"
node["test"] >> fmt::default_range(v, { 0, 100 }, 10);

// fetch v check if it's in range [0, 100] on out of range trigger an error
node["test"] >> fmt::range_checked(v, { 0, 100 });

What do you think ?

I can keep this as an extension to RapidYaml on our side if you'd prefer.

- added bool overflows<T>(csubstr) function
@codecov
Copy link

codecov bot commented Mar 3, 2022

Codecov Report

Merging #78 (5d99fcd) into master (42bd976) will increase coverage by 0.26%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #78      +/-   ##
==========================================
+ Coverage   93.96%   94.23%   +0.26%     
==========================================
  Files          53       53              
  Lines       11449    11653     +204     
==========================================
+ Hits        10758    10981     +223     
+ Misses        691      672      -19     
Impacted Files Coverage Δ
src/c4/charconv.hpp 97.79% <100.00%> (+0.50%) ⬆️
src/c4/format.hpp 96.34% <100.00%> (+0.07%) ⬆️
test/test_charconv.cpp 100.00% <100.00%> (ø)
test/test_format.cpp 100.00% <100.00%> (ø)
src/c4/ext/fast_float_all.h 26.81% <0.00%> (-1.31%) ⬇️
test/test_memory_resource.cpp 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 42bd976...5d99fcd. Read the comment docs.

@biojppm
Copy link
Owner

biojppm commented Mar 3, 2022

@fargies I think the overflows<T>(s) is ready as-is, all the more so because its cost is rather low.

And indeed sticking to from_chars() is sufficient; it is a lot of work to add atoi_rc() and so on, and all it does is adding API complexity.

Regarding the name, I was starting to write how range_checked would be preferred, but then got your point about the utility suggestions and how range_checked is actually more appropriate for that scenario. I think you are right. So overflow_checked is how it goes.

I've added a commit (or two! hopefully the second one is good) addressing the compilation errors in test_charconv.cpp .

As for the utilities, let's discuss those in a different issue or PR if you want to jump right in. Do have in mind that there is already a value-or-default method in NodeRef (but I can't recall the name ATM), which should be considered to avoid API cruft. Also, I will generally prefer methods in NodeRef rather than functions, as they are easier to find for users, whereas freestanding functions require explicit documentation which no one reads. The overflow is a different situation to this, as it is plainly type-related rather than data-related. ie, it is more c4core-scoped than ryml-scoped.

@fargies
Copy link
Author

fargies commented Mar 3, 2022

@fargies I think the overflows<T>(s) is ready as-is, all the more so because its cost is rather low.

Yay 🎉

Thanks for the fixes

As for the utilities, let's discuss those in a different issue or PR if you want to jump right in. Do have in mind that there is already a value-or-default method in NodeRef (but I can't recall the name ATM), which should be considered to avoid API cruft. Also, I will generally prefer methods in NodeRef rather than functions, as they are easier to find for users, whereas freestanding functions require explicit documentation which no one reads. The overflow is a different situation to this, as it is plainly type-related rather than data-related. ie, it is more c4core-scoped than ryml-scoped.

I think it is NodeRef::get_if, but you're right let's discuss this somewhere else 👍

Copy link
Owner

@biojppm biojppm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fargies we're almost there; just address the two remarks, and I'll merge this in.

src/c4/charconv.hpp Outdated Show resolved Hide resolved
src/c4/charconv.hpp Show resolved Hide resolved
@biojppm
Copy link
Owner

biojppm commented Mar 4, 2022

@fargies the coverage test failure is from one of the coverage services (coveralls, I think I'm going to remove it). So all is good and from my end this is ready to merge. Is there anything else that you want to do? Let me know if I can merge.

@fargies
Copy link
Author

fargies commented Mar 4, 2022

All good for me, can be merged 👍

@biojppm
Copy link
Owner

biojppm commented Mar 4, 2022

Thanks! Great stuff, really appreciated. 🚀

@biojppm biojppm merged commit 3100b70 into biojppm:master Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants