Skip to content

BUG: Do not rely on undefined casting behavior in numpy.random.#9680

Closed
yangli2 wants to merge 9 commits intonumpy:masterfrom
yangli2:asan-fix
Closed

BUG: Do not rely on undefined casting behavior in numpy.random.#9680
yangli2 wants to merge 9 commits intonumpy:masterfrom
yangli2:asan-fix

Conversation

@yangli2
Copy link
Contributor

@yangli2 yangli2 commented Sep 12, 2017

Current double to long casting in the zipf function depends on non-standardized behavior. See http://en.cppreference.com/w/c/language/conversion#Real_floating-integer_conversions.

This is potentially dangerous and makes the code fail with tools such as AddressSanitizer.

Adding checks here to prevent overflow during casting and make sure we get the desired behavior.

…andardized behavior. See http://en.cppreference.com/w/c/language/conversion#Real_floating-integer_conversions.

This is potentially dangerous and makes the code fail with tools such as AddressSanitizer.

Adding checks here to prevent overflow during casting and make sure we get the desired behavior.
Current double to long casting in the zipf function depends on non-standardized behavior. See http://en.cppreference.com/w/c/language/conversion#Real_floating-integer_conversions.

This is potentially dangerous and makes the code fail with tools such as AddressSanitizer.

Adding checks here to prevent overflow during casting and make sure we get the desired behavior.
Current double to long casting in the zipf function depends on non-standardized behavior. See http://en.cppreference.com/w/c/language/conversion#Real_floating-integer_conversions.

This is potentially dangerous and makes the code fail with tools such as AddressSanitizer.

Adding checks here to prevent overflow during casting and make sure we get the desired behavior.
Current double to long casting in the zipf function depends on non-standardized behavior. See http://en.cppreference.com/w/c/language/conversion#Real_floating-integer_conversions.

This is potentially dangerous and makes the code fail with tools such as AddressSanitizer.

Adding checks here to prevent overflow during casting and make sure we get the desired behavior.
Copy link
Member

Choose a reason for hiding this comment

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

I'd just use X without the cast to long and only cast it to long for the return. Note that every place else it is cast float again for the computation and that it is non-negative, so the second check may be avoided. So maybe something like

if (X > LONG_MAX) {
    /* X < 1 will be rejected */
    X = 0.0;
    continue;
} 

and then put X < 1 check below first to avoid the big computation. Note that the continue above skips the division by X, so 0.0 is not a problem. Also we put the opening brace on the same line as the condition, see doc/C_STYLE_GUIDE.rst.txt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx for the review! Makes sense, done in my latest commit. We don't need to set X to 0, right? It seems that continue would cause a new X to be computed already. For X<1 we can skip both the big computation as well as the computation in the condition; so I added both conditions in the loop.

Copy link
Member

Choose a reason for hiding this comment

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

No, continue goes to the conditional in a do loop.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, right. Fixed that.

@charris charris changed the title ENH: Stop relying on non-standard casting behavior in numpy.random. BUG: Do not rely on undefined casting behavior in numpy.random. Sep 12, 2017
T = pow(1.0 + 1.0/X, am1);
} while (((V*X*(T-1.0)/(b-1.0)) > (T/b)) || X < 1);
return X;
if (X > LONG_MAX) {
Copy link
Member

@charris charris Sep 17, 2017

Choose a reason for hiding this comment

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

I'd probably just do

if (X > LONG_MAX || X < 1.) {

and remove the condition for calculating T.

@charris
Copy link
Member

charris commented Sep 17, 2017

You can squash your commits together, do git rebase -i HEAD~9 and follow instructions. Follow that with push -f origin HEAD.

@charris
Copy link
Member

charris commented Oct 4, 2017

Cleaned up and squashed in #9824, so closing this. Thanks @yangli2 .

eric-wieser added a commit that referenced this pull request Oct 10, 2017
BUG: Fixes for np.random.zipf
@eric-wieser
Copy link
Member

Closed in #9824

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments