Skip to content

Commit

Permalink
Fix UFA非法地址访问(UFA illegal address access) of case3: paddle.crop (#49994)
Browse files Browse the repository at this point in the history
* add range check for crop_kernel

* remove shape negative check

* add unittest
  • Loading branch information
RedContritio authored Feb 1, 2023
1 parent fd5b8ee commit 34bf3d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions paddle/phi/kernels/impl/crop_kernel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ void CropTensorFunction(const Context& dev_ctx,
out->Resize(out_dims);
dev_ctx.template Alloc<T>(out);
for (size_t i = 0; i < offsets_vec.size(); ++i) {
PADDLE_ENFORCE_GE(
offsets_vec[i],
0,
errors::InvalidArgument("The offsets (%d) of the %uth elements of"
" Op(crop_tensor) "
"should be greater than or "
"equal to 0.",
offsets_vec[i],
i));

PADDLE_ENFORCE_LE(offsets_vec[i] + shape_vec[i],
x_dims[i],
errors::InvalidArgument(
Expand Down
7 changes: 7 additions & 0 deletions python/paddle/fluid/tests/unittests/test_crop_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ def test_crop_none_shape(self):
self.assertEqual(crop.shape, (3, 6, 6))


class TestCropError(unittest.TestCase):
def test_neg_offset_error(self):
with self.assertRaises(ValueError):
x = fluid.data(name='input2', shape=[1], dtype="float32")
out = paddle.crop(x, offsets=[-1])


if __name__ == '__main__':
paddle.enable_static()
unittest.main()

0 comments on commit 34bf3d0

Please sign in to comment.