Skip to content

Avoid UBSan report in arrayElement#19347

Merged
alexey-milovidov merged 4 commits intomasterfrom
array-element-ubsan
Jan 21, 2021
Merged

Avoid UBSan report in arrayElement#19347
alexey-milovidov merged 4 commits intomasterfrom
array-element-ubsan

Conversation

@alexey-milovidov
Copy link
Copy Markdown
Member

@alexey-milovidov alexey-milovidov commented Jan 21, 2021

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Avoid UBSan reports in arrayElement function, substring and arraySum. Fixes #19305. Fixes #19287. This closes #19336.

@robot-clickhouse robot-clickhouse added the pr-build Pull request with build/testing/packaging improvement label Jan 21, 2021
@tavplubix tavplubix self-assigned this Jan 21, 2021
else if (index.getType() == Field::Types::Int64)
{
/// Cast to UInt64 before negation allows to avoid undefined behaviour for negation of the most negative number.
/// NOTE: this would be undefined behaviour in C++ sense, but nevertheless, compiler cannot see it on user provided data,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, it's never an undefined behaviour according to the standard. Conversion from negative signed to unsigned integer is defined as addition of 2^{N-1} and negation of unsigned integer value is defined as 2^N - value. Am I missing something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the comment explaining why do we have this code (this code prevents UB that will be otherwise possible).

@alexey-milovidov alexey-milovidov merged commit 37c345f into master Jan 21, 2021
@alexey-milovidov alexey-milovidov deleted the array-element-ubsan branch January 21, 2021 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-docs-needed pr-build Pull request with build/testing/packaging improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ArrayElement + ubsan UBSan: substringUTF8: negation of -9223372036854775808 cannot be represented in type 'DB::Int64'

4 participants