Updating random number generation to not overflow#268
Updating random number generation to not overflow#268epicfarmer wants to merge 2 commits intodwavesystems:mainfrom
Conversation
|
Thanks for finding this! The main thing is to keep the initialization and the class member consistent. So IMO we should either change line 215/216 to //! distribution over [0, 0xffffffff]
uniform_int_distribution<std::uint32_t> rand;and line 240 to rand(0, std::numeric_limits<std::uint32_t>::max()),or update both to be //! distribution over [0, 0x7fffffff]
uniform_int_distribution<int> rand; rand(0, std::numeric_limits<int>::max()),I think the latter is probably the better approach. There may be other places in the code that need to be updated for consistency as well. |
|
I've udpated the PR based on the latter. As far as
this is the only place where I encountered overflow that interferes with bounds checking. There are generally lots of conversions between signed/unsigned larger/smaller integer types in the codebase, but it's not obvious to me which if any are problematic. Separately, I think the gtests are broken for me locally, and I cannot see them run on ci. I'll file a separate issue for that |
|
Thanks for your attention to this; it's been a long while since I've been through this code but I'm pretty sure that the right solution is to make everything unsigned -- I don't think that I used -1 as a sentinel value anywhere, but that would be the thing to look for. |
I'd argue consistency is all that matters. We're not short on possible values 😄 Though there is also a local consistency argument 🤷 I have no strong feelings here. |
|
Huh, unless I am missing something it seems like we only use the type of rand still looking... |
That feels right to me. I think if you were really drawing numbers between 0 and -1, you'd notice in the python test suite somewhere. As for
I enabled -Wsign-conversion as a test, and this seems like a lot of work. It's only ~400 lines of warnings, but it seems to touch a lot of files. Here are my compilation results if you're interested: |
It is a lot of work. I see the use of |
|
I don't mind submitting more PRs towards this goal. However, I think that:
|
rand is a
std::uniform_int_distribution<>, which defaults toint. So when it's called with0xffffffff, that is-1.Not sure if the right fix is to change the integer type or the initialization.