You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "llama90 (via GitHub)" <gi...@apache.org> on 2023/10/29 13:37:08 UTC
[PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
llama90 opened a new pull request, #38504:
URL: https://github.com/apache/arrow/pull/38504
<!--
Thanks for opening a pull request!
If this is your first pull request you can find detailed information on how
to contribute here:
* [New Contributor's Guide](https://arrow.apache.org/docs/dev/developers/guide/step_by_step/pr_lifecycle.html#reviews-and-merge-of-the-pull-request)
* [Contributing Overview](https://arrow.apache.org/docs/dev/developers/overview.html)
If this is not a [minor PR](https://github.com/apache/arrow/blob/main/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose
Opening GitHub issues ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
Then could you also rename the pull request title in the following format?
GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
or
MINOR: [${COMPONENT}] ${SUMMARY}
In the case of PARQUET issues on JIRA the title also supports:
PARQUET-${JIRA_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
-->
### Rationale for this change
As mentioned in the issue, the `Repeat` function does not currently check for overflow when multiplying the repeat count by the string size.
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
### What changes are included in this PR?
* Added check logic for overflow.
* Added unit test for overflow scenarios.
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
### Are these changes tested?
Yes.
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
### Are there any user-facing changes?
Yes.
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please uncomment the line below and explain which changes are breaking.
-->
<!-- **This PR includes breaking changes to public APIs.** -->
<!--
Please uncomment the line below (and provide explanation) if the changes fix either (a) a security vulnerability, (b) a bug that caused incorrect or invalid data to be produced, or (c) a bug that causes a crash (even when the API contract is upheld). We use this to highlight fixes to issues that may affect users without their knowledge. For this reason, fixing bugs that cause errors don't count, since those are usually obvious.
-->
<!-- **This PR contains a "Critical Fix".** -->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on PR #38504:
URL: https://github.com/apache/arrow/pull/38504#issuecomment-3592896555
Hello, I don't have enough time to work on this issue/PR at the moment.
So I will close this PR. If anyone is interested, please feel free to continue the work.
Thank you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #38504:
URL: https://github.com/apache/arrow/pull/38504#discussion_r1394362901
##########
cpp/src/arrow/array/util.cc:
##########
@@ -862,6 +854,12 @@ class RepeatedArrayFactory {
template <typename OffsetType>
Status CreateOffsetsBuffer(OffsetType value_length, std::shared_ptr<Buffer>* out) {
+ if (length_ > std::numeric_limits<OffsetType>::max()) {
Review Comment:
... because what you need to check here is not `length_` but `value_length`, however `value_length` is already cast to OffsetType here
##########
cpp/src/arrow/array/util.cc:
##########
@@ -673,22 +673,14 @@ class RepeatedArrayFactory {
std::shared_ptr<Buffer> values_buffer, offsets_buffer;
auto size = static_cast<typename T::offset_type>(value->size());
- typename T::offset_type length_casted;
- if (length_ > std::numeric_limits<typename T::offset_type>::max()) {
- return Status::Invalid(
- "length exceeds the maximum value of offset_type: ", std::to_string(length_),
- " is greater than ",
- std::to_string(std::numeric_limits<typename T::offset_type>::max()));
- }
- length_casted = static_cast<typename T::offset_type>(length_);
-
typename T::offset_type total_size;
- if (MultiplyWithOverflow(size, length_casted, &total_size)) {
+ if (MultiplyWithOverflow(size, static_cast<typename T::offset_type>(length_),
Review Comment:
I think in order to make this correct, this multiplication must be moved into CreateOffsetsBuffer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on PR #38504:
URL: https://github.com/apache/arrow/pull/38504#issuecomment-1786897785
@AlenkaF Hello. Is it appropriate to fix the part you mentioned as an issue like this?
If you have time, I would appreciate it if you could leave a review.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on PR #38504:
URL: https://github.com/apache/arrow/pull/38504#issuecomment-1801555897
@js8544 Thank you for your review!! I'll take your advice and try to improve it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "js8544 (via GitHub)" <gi...@apache.org>.
js8544 commented on code in PR #38504:
URL: https://github.com/apache/arrow/pull/38504#discussion_r1389013727
##########
cpp/src/arrow/array/util.cc:
##########
@@ -669,11 +671,29 @@ class RepeatedArrayFactory {
enable_if_base_binary<T, Status> Visit(const T&) {
const std::shared_ptr<Buffer>& value = scalar<T>().value;
std::shared_ptr<Buffer> values_buffer, offsets_buffer;
- RETURN_NOT_OK(CreateBufferOf(value->data(), value->size(), &values_buffer));
auto size = static_cast<typename T::offset_type>(value->size());
+
+ typename T::offset_type length_casted;
+ if (length_ < 0) {
+ return Status::Invalid("length cannot be negative: " + std::to_string(length_));
Review Comment:
I think the negativity check can be moved to the top level `MakeArrayFromScalar` since it applies to all types.
##########
cpp/src/arrow/array/array_test.cc:
##########
@@ -685,6 +685,50 @@ TEST_F(TestArray, TestMakeArrayFromScalarSliced) {
}
}
+TEST_F(TestArray, TestMakeArrayFromScalarStringOverflow) {
+ auto scalar = std::make_shared<StringScalar>("aa");
+
+ // Use a length that will cause an overflow when multiplied by the size of the string
+ int64_t length = static_cast<int32_t>(std::numeric_limits<int32_t>::max()) / 2 + 1;
+ auto array_result = MakeArrayFromScalar(*scalar, length);
+
+ std::string err_msg = "offset overflow in repeated array construction";
+ EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, ::testing::HasSubstr(err_msg), array_result);
Review Comment:
For each case, can you also add a case where it "just" works to avoid false positives. E.g. "aa" and `int32::max/2`
##########
cpp/src/arrow/array/util.cc:
##########
@@ -669,11 +671,29 @@ class RepeatedArrayFactory {
enable_if_base_binary<T, Status> Visit(const T&) {
const std::shared_ptr<Buffer>& value = scalar<T>().value;
std::shared_ptr<Buffer> values_buffer, offsets_buffer;
- RETURN_NOT_OK(CreateBufferOf(value->data(), value->size(), &values_buffer));
auto size = static_cast<typename T::offset_type>(value->size());
+
+ typename T::offset_type length_casted;
+ if (length_ < 0) {
+ return Status::Invalid("length cannot be negative: " + std::to_string(length_));
+ } else if (length_ > std::numeric_limits<typename T::offset_type>::max()) {
+ return Status::Invalid(
+ "length exceeds the maximum value of offset_type: " + std::to_string(length_) +
+ " is greater than " +
+ std::to_string(std::numeric_limits<typename T::offset_type>::max()));
Review Comment:
I have no problem having this check here.
BTW You can just use `Status::Invalid("some message ", length_, " some message ", number)` without using `to_string` and `+`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 closed pull request #38504: GH-36388: [C++][Array] Add Overflow check for string Repeat
URL: https://github.com/apache/arrow/pull/38504
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #38504:
URL: https://github.com/apache/arrow/pull/38504#discussion_r1391363053
##########
cpp/src/arrow/array/util.cc:
##########
@@ -669,11 +671,29 @@ class RepeatedArrayFactory {
enable_if_base_binary<T, Status> Visit(const T&) {
const std::shared_ptr<Buffer>& value = scalar<T>().value;
std::shared_ptr<Buffer> values_buffer, offsets_buffer;
- RETURN_NOT_OK(CreateBufferOf(value->data(), value->size(), &values_buffer));
auto size = static_cast<typename T::offset_type>(value->size());
+
+ typename T::offset_type length_casted;
+ if (length_ < 0) {
+ return Status::Invalid("length cannot be negative: " + std::to_string(length_));
+ } else if (length_ > std::numeric_limits<typename T::offset_type>::max()) {
+ return Status::Invalid(
+ "length exceeds the maximum value of offset_type: " + std::to_string(length_) +
+ " is greater than " +
+ std::to_string(std::numeric_limits<typename T::offset_type>::max()));
Review Comment:
It could be larger than in32::max if the string were empty. We should only apply this check to the product of `(length of repeated string) * (number of repeats)`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "js8544 (via GitHub)" <gi...@apache.org>.
js8544 commented on code in PR #38504:
URL: https://github.com/apache/arrow/pull/38504#discussion_r1385967985
##########
cpp/src/arrow/array/util.cc:
##########
@@ -669,11 +671,18 @@ class RepeatedArrayFactory {
enable_if_base_binary<T, Status> Visit(const T&) {
const std::shared_ptr<Buffer>& value = scalar<T>().value;
std::shared_ptr<Buffer> values_buffer, offsets_buffer;
- RETURN_NOT_OK(CreateBufferOf(value->data(), value->size(), &values_buffer));
auto size = static_cast<typename T::offset_type>(value->size());
+
+ int64_t total_size;
Review Comment:
Should this be `typename T::offset_type`? If `total_size` is fixed to be int64_t, it always calls the int64 version of `MultiplyWithOverflow`. But `StringArray` uses int32_t for offsets so some false positive cases can go through.
For example:
```cpp
auto scalar = std::make_shared<StringScalar>("aa");
int64_t length = static_cast<int32_t>(std::numeric_limits<int32_t>::max()) / 2 + 1;
auto array_result = MakeArrayFromScalar(*scalar, length);
```
`array_result` will be `ok` but when I print the array it says `Invalid array: Negative offsets in binary array`
##########
cpp/src/arrow/array/array_test.cc:
##########
@@ -685,6 +685,19 @@ TEST_F(TestArray, TestMakeArrayFromScalarSliced) {
}
}
+TEST_F(TestArray, TestMakeArrayFromScalarOverflow) {
+ auto scalar = std::make_shared<StringScalar>("aa");
+
+ // Use a length that will cause an overflow when multiplied by the size of the string
+ int64_t length = static_cast<int64_t>(std::numeric_limits<int64_t>::max()) / 2 + 1;
Review Comment:
We should probably test `StringArray` with `int32_t::max` and `LargeStringArray` with `int64_t::max`
##########
cpp/src/arrow/array/array_test.cc:
##########
@@ -685,6 +685,19 @@ TEST_F(TestArray, TestMakeArrayFromScalarSliced) {
}
}
+TEST_F(TestArray, TestMakeArrayFromScalarOverflow) {
+ auto scalar = std::make_shared<StringScalar>("aa");
+
+ // Use a length that will cause an overflow when multiplied by the size of the string
+ int64_t length = static_cast<int64_t>(std::numeric_limits<int64_t>::max()) / 2 + 1;
+ auto array_result = MakeArrayFromScalar(*scalar, length);
+
+ std::string err_msg = "offset overflow in repeated array construction";
+ ASSERT_FALSE(array_result.ok());
+ ASSERT_EQ(array_result.status().code(), StatusCode::Invalid);
+ ASSERT_EQ(array_result.status().message().substr(0, err_msg.length()), err_msg);
Review Comment:
You can use `EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, ::testing::HasSubstr(err_msg), array_result);` to simplify this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on PR #38504:
URL: https://github.com/apache/arrow/pull/38504#issuecomment-1809325096
I have found that preventing empty strings affects the pyarrow tests. Due to my main job responsibilities, I will make the necessary corrections as soon as possible.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "js8544 (via GitHub)" <gi...@apache.org>.
js8544 commented on code in PR #38504:
URL: https://github.com/apache/arrow/pull/38504#discussion_r1389019657
##########
cpp/src/arrow/array/util.cc:
##########
@@ -669,11 +671,29 @@ class RepeatedArrayFactory {
enable_if_base_binary<T, Status> Visit(const T&) {
const std::shared_ptr<Buffer>& value = scalar<T>().value;
std::shared_ptr<Buffer> values_buffer, offsets_buffer;
- RETURN_NOT_OK(CreateBufferOf(value->data(), value->size(), &values_buffer));
auto size = static_cast<typename T::offset_type>(value->size());
+
+ typename T::offset_type length_casted;
+ if (length_ < 0) {
+ return Status::Invalid("length cannot be negative: " + std::to_string(length_));
+ } else if (length_ > std::numeric_limits<typename T::offset_type>::max()) {
+ return Status::Invalid(
+ "length exceeds the maximum value of offset_type: " + std::to_string(length_) +
+ " is greater than " +
+ std::to_string(std::numeric_limits<typename T::offset_type>::max()));
Review Comment:
On second thought, what if the input scalar an empty string ""? Can the length be larger than int32::max?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "thisisnic (via GitHub)" <gi...@apache.org>.
thisisnic commented on PR #38504:
URL: https://github.com/apache/arrow/pull/38504#issuecomment-3547442732
Thank you for your contribution. Unfortunately, this
pull request has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label
or comment below, or this PR will be closed in 14 days. Feel free to re-open this if it has been closed in error. If you
do not have repository permissions to reopen the PR, please tag a maintainer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #38504:
URL: https://github.com/apache/arrow/pull/38504#issuecomment-1784114906
:warning: GitHub issue #36388 **has been automatically assigned in GitHub** to PR creator.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "llama90 (via GitHub)" <gi...@apache.org>.
llama90 commented on PR #38504:
URL: https://github.com/apache/arrow/pull/38504#issuecomment-1804095631
@js8544 Hello.
I've done as you mentioned. I've now utilized `typename T::offset_type` to accommodate type-specific maximum lengths. Additionally, I've included tests for `String` and `LargeString` to ensure verification.
Furthermore, I've put in place checks for when the length exceeds the `maximum` allowed size or is `negative`. Do you think these checks could be excessive?
Thank you for your insightful review. It has certainly helped in refining my approach and making sure all bases are covered. Your guidance is greatly appreciated!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #38504:
URL: https://github.com/apache/arrow/pull/38504#discussion_r1392930361
##########
cpp/src/arrow/array/util.cc:
##########
@@ -669,11 +671,27 @@ class RepeatedArrayFactory {
enable_if_base_binary<T, Status> Visit(const T&) {
const std::shared_ptr<Buffer>& value = scalar<T>().value;
std::shared_ptr<Buffer> values_buffer, offsets_buffer;
- RETURN_NOT_OK(CreateBufferOf(value->data(), value->size(), &values_buffer));
auto size = static_cast<typename T::offset_type>(value->size());
+
+ typename T::offset_type length_casted;
+ if (length_ > std::numeric_limits<typename T::offset_type>::max()) {
Review Comment:
This check is not correct; please remove it. We only need to check whether `(length of repeated string) * (number of repeats)` can be represented as an offset, and I think it'd be better for that check to reside in CreateOffsetsBuffer (since then it will also be applied to list types)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-36388: [C++][Array] Add Overflow check for string Repeat [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #38504:
URL: https://github.com/apache/arrow/pull/38504#discussion_r1394400239
##########
cpp/src/arrow/array/util.cc:
##########
@@ -844,6 +848,18 @@ class RepeatedArrayFactory {
template <typename OffsetType>
Status CreateOffsetsBuffer(OffsetType value_length, std::shared_ptr<Buffer>* out) {
+ OffsetType total_size;
+ if (MultiplyWithOverflow(value_length, static_cast<OffsetType>(length_),
+ &total_size)) {
+ return Status::Invalid("offset overflow in repeated array construction");
+ }
+
+ if (length_ > std::numeric_limits<OffsetType>::max()) {
+ return Status::Invalid(
+ "length exceeds the maximum value of offset_type: ", std::to_string(length_),
+ " is greater than ", std::to_string(std::numeric_limits<OffsetType>::max()));
Review Comment:
```suggestion
int64_t total_size;
if (MultiplyWithOverflow(static_cast<int64_t>(value_length), length_, &total_size) ||
total_size > std::numeric_limits<OffsetType>::max()) {
return Status::Invalid(
"length exceeds the maximum value of offset_type: ", std::to_string(total_size),
" is greater than ", std::to_string(std::numeric_limits<OffsetType>::max()));
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org