From 43d4066aa093f714a46dec476ae749bdf9428610 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 17 Sep 2020 11:02:50 -0400 Subject: [PATCH] Additional fixes Added fix for overflow check --- .../ccsrc/minddata/dataset/api/transforms.cc | 18 +++++++++++++----- .../minddata/dataset/kernels/image/affine_op.h | 1 - .../dataset/kernels/image/image_utils.cc | 8 ++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/mindspore/ccsrc/minddata/dataset/api/transforms.cc b/mindspore/ccsrc/minddata/dataset/api/transforms.cc index 906b5102962..b2c2a0cd5cf 100644 --- a/mindspore/ccsrc/minddata/dataset/api/transforms.cc +++ b/mindspore/ccsrc/minddata/dataset/api/transforms.cc @@ -343,8 +343,12 @@ bool CenterCropOperation::ValidateParams() { } // We have to limit crop size due to library restrictions, optimized to only iterate over size_ once for (int i = 0; i < size_.size(); ++i) { - if (size_[i] <= 0 || size_[i] == INT_MAX) { - MS_LOG(ERROR) << "Crop: invalid size, size must be greater than zero, got: " << size_[i]; + if (size_[i] <= 0) { + MS_LOG(ERROR) << "CenterCrop: invalid size, size must be greater than 0, got: " << size_[i]; + return false; + } + if (size_[i] == INT_MAX) { + MS_LOG(ERROR) << "CenterCrop: invalid size, size too large, got: " << size_[i]; return false; } } @@ -381,8 +385,12 @@ bool CropOperation::ValidateParams() { } // We have to limit crop size due to library restrictions, optimized to only iterate over size_ once for (int i = 0; i < size_.size(); ++i) { - if (size_[i] <= 0 || size_[i] == INT_MAX) { - MS_LOG(ERROR) << "Crop: invalid size, size must be greater than zero, got: " << size_[i]; + if (size_[i] <= 0) { + MS_LOG(ERROR) << "Crop: invalid size, size must be greater than 0, got: " << size_[i]; + return false; + } + if (size_[i] == INT_MAX) { + MS_LOG(ERROR) << "Crop: invalid size, size too large, got: " << size_[i]; return false; } } @@ -490,7 +498,7 @@ bool NormalizeOperation::ValidateParams() { } // check std value for (int i = 0; i < std_.size(); ++i) { - if (std_[i] < 0.0f || mean_[i] > 255.0f || CmpFloat(std_[i], 0.0f)) { + if (std_[i] < 0.0f || std_[i] > 255.0f || CmpFloat(std_[i], 0.0f)) { MS_LOG(ERROR) << "Normalize: std vector has incorrect value: " << std_[i]; return false; } diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/affine_op.h b/mindspore/ccsrc/minddata/dataset/kernels/image/affine_op.h index c84863e4d53..947125a3365 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/affine_op.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/affine_op.h @@ -39,7 +39,6 @@ class AffineOp : public TensorOp { static const std::vector kFillValue; /// Constructor - public: explicit AffineOp(float_t degrees, const std::vector &translation = kTranslation, float_t scale = kScale, const std::vector &shear = kShear, InterpolationMode interpolation = kDefInterpolation, const std::vector &fill_value = kFillValue); diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.cc index f28abb75fdb..2907fbb829a 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.cc @@ -352,6 +352,14 @@ Status Crop(const std::shared_ptr &input, std::shared_ptr *outpu if (input_cv->Rank() != 3 && input_cv->Rank() != 2) { RETURN_STATUS_UNEXPECTED("Shape not or "); } + // account for integer overflow + if (y < 0 || (y + h) > input_cv->shape()[0] || (y + h) < 0) { + RETURN_STATUS_UNEXPECTED("Invalid y coordinate value for crop"); + } + // account for integer overflow + if (x < 0 || (x + w) > input_cv->shape()[1] || (x + w) < 0) { + RETURN_STATUS_UNEXPECTED("Invalid x coordinate value for crop"); + } try { TensorShape shape{h, w}; int num_channels = input_cv->shape()[2];