Skip to content

BUG: Fix uint-overflow if padding with linear_ramp and negative gain (backport) - #14211

Merged
charris merged 1 commit into
numpy:maintenance/1.16.xfrom
lagru:fix-linear-ramp-backport-1-16
Sep 9, 2019
Merged

charris merged 1 commit into
numpy:maintenance/1.16.xfrom
lagru:fix-linear-ramp-backport-1-16

Conversation

@lagru

@lagru lagru commented Aug 6, 2019

Copy link
Copy Markdown
Contributor

Manual backport (see gh-14191 and gh-14209).

In case end_value was greater than the edge values of the array to pad,
the resulting difference was negative and caused overflows in case the
input array used unsigned types. The new behavior will cast to a
compatible signed dtype that is as small as possible (float16 because of
the division).
Note: Simply casting to float64 doesn't work for complex inputs.

Protect against this regression with a new test for all numeric dtypes.

cc @seberg @charris

@lagru lagru changed the title BUG: Fix uint-overflow if padding with linear_ramp and negative gain (backport) WIP: BUG: Fix uint-overflow if padding with linear_ramp and negative gain (backport) Aug 6, 2019
@lagru

lagru commented Aug 6, 2019

Copy link
Copy Markdown
Contributor Author

I'll update this once gh-14209 is finalized and merged.

linspace supports non-scalar values since version 1.16. This can
simplify construction of the ramp for the pad mode "linear_ramp".

This removes a bug in the old implementation where certain edge and
end values resulted in an integer underflow for unsigned dtypes (see
numpygh-14191). Protect against this regression with a new test for all
numeric dtypes.
@lagru
lagru force-pushed the fix-linear-ramp-backport-1-16 branch from 7e84452 to 1c45e0d Compare September 5, 2019 18:36
@lagru lagru changed the title WIP: BUG: Fix uint-overflow if padding with linear_ramp and negative gain (backport) BUG: Fix uint-overflow if padding with linear_ramp and negative gain (backport) Sep 5, 2019
Comment thread numpy/lib/arraypad.py
@@ -244,27 +244,19 @@ def _prepend_ramp(arr, pad_amt, end, axis=-1):
if pad_amt == 0:
return arr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This optimization path could be removed as well but I left it for now...

Comment thread numpy/lib/arraypad.py
# Reverse linear space in appropriate dimension
ramp_arr = ramp_arr[
_slice_at_axis(ramp_arr.shape, slice(None, None, -1), axis)
]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could avoid this if linspace had something like startpoint=False (similar to endpoint=False) the we could simply switch the start and stop values. IMO not worth it but I'd be interested to know if such a feature was discussed already.

@charris
charris merged commit 6482719 into numpy:maintenance/1.16.x Sep 9, 2019
@charris

charris commented Sep 9, 2019

Copy link
Copy Markdown
Member

Thanks @lagru .

@lagru
lagru deleted the fix-linear-ramp-backport-1-16 branch September 10, 2019 04:55
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.

2 participants