8000 BUG: fix out of bound access in unicode argmin/argmax · numpy/numpy@5a14415 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a14415

Browse files
committed
BUG: fix out of bound access in unicode argmin/argmax
elsize is in bytes and the pointer of unicode type, so it must divided by the size. Closes gh-5082
1 parent 9af5028 commit 5a14415

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,7 @@ static int
29912991
memcpy(mp, ip, elsize);
29922992
*max_ind = 0;
29932993
for (i = 1; i < n; i++) {
2994-
ip += elsize;
2994+
ip += elsize / sizeof(@type@);
29952995
if (@fname@_compare(ip, mp, aip) > 0) {
29962996
memcpy(mp, ip, elsize);
29972997
*max_ind = i;
@@ -3048,7 +3048,7 @@ static int
30483048
memcpy(mp, ip, elsize);
30493049
*min_ind = 0;
30503050
for(i=1; i<n; i++) {
3051-
ip += elsize;
3051+
ip += elsize / sizeof(@type@);
30523052
if (@fname@_compare(mp,ip,aip) > 0) {
30533053
memcpy(mp, ip, elsize);
30543054
*min_ind=i;

numpy/core/tests/test_multiarray.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,11 @@ def test_output_shape(self):
21442144
a.argmax(-1, out=out)
21452145
assert_equal(out, a.argmax(-1))
21462146

2147+
def test_argmax_unicode(self):
2148+
d = np.zeros(6031, dtype='<U9')
2149+
d[5942] = "as"
2150+
assert_equal(d.argmax(), 5942)
2151+
21472152

21482153
class TestArgmin(TestCase):
21492154

@@ -2249,6 +2254,11 @@ def test_output_shape(self):
22492254
a.argmin(-1, out=out)
22502255
assert_equal(out, a.argmin(-1))
22512256

2257+
def test_argmin_unicode(self):
2258+
d = np.ones(6031, dtype='<U9')
2259+
d[6001] = "0"
2260+
assert_equal(d.argmin(), 6001)
2261+
22522262

22532263
class TestMinMax(TestCase):
22542264
def test_scalar(self):

0 commit comments

Comments
 (0)
0