question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No out of bounds check during advanced array indexing

See original GitHub issue

Numba ignores any indices that are out of bounds for a given array shape. This behavior is present in both basic and advanced indexing.

import numpy as np
import numba

@numba.njit
def something():
    arr = np.ones((3,3))
    arr[100] = 0
    arr[:, np.array([2,3,5,100])] = 0
    return arr

print(something()) # Does not raise error, ignores the out of bound indices completely.
print(something.py_func()) # Raises out of bounds error

Not sure if we should be considering this as a bug or a feature 😃

Should be easily fixed by adding a bounds check during indexing during function runtime.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartarchibaldcommented, Jun 2, 2022

@kc611 suggest adding:

diff --git a/numba/np/arrayobj.py b/numba/np/arrayobj.py
index 850bd1d..5b1d48e 100644
--- a/numba/np/arrayobj.py
+++ b/numba/np/arrayobj.py
@@ -1617,7 +1617,8 @@ def fancy_setslice(context, builder, sig, args, index_types, indices):
     dest_ptr = cgutils.get_item_pointer2(context, builder, dest_data,
                                          dest_shapes, dest_strides,
                                          aryty.layout, dest_indices,
-                                         wraparound=False)
+                                         wraparound=False,
+                                         boundscheck=context.enable_boundscheck)
     store_item(context, builder, aryty, val, dest_ptr)

see if that helps?

1reaction
kc611commented, Jun 2, 2022

Thank you that was exactly what I was looking for.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array Index Out Of Bounds Exception in Java - GeeksforGeeks
The ArrayIndexOutOfBoundsException is a Runtime Exception thrown only at runtime. The Java Compiler does not check for this error during the ...
Read more >
Advanced indexing - managing out of bounds behaviour
I am trying to solve the issue of determining the out of bounds behaviour for indexing arrays. For the prior question see: How...
Read more >
How to Fix the Array Index Out Of Bounds Excepiton in Java
The ArrayIndexOutOfBoundsException is a runtime exception in Java that occurs when an array is accessed with an illegal index.
Read more >
4. Index Out of Bounds - Safe C++ [Book] - O'Reilly
The checks for an index out of bounds are performed inside the index(row, col) function, separately for row and column numbers, and in...
Read more >
Question about safety when accessing an array out of bounds
You've just given an example of a runtime check. It doesn't ensure the index is in bounds at compile time; it simply ensures...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found