Skip to content

Commit 1345ae0

Browse files
authored
[SYCL] Prevent unintended sign extensions (#15230)
This is to resolve a Coverity hit, pointing out that a potentially unintended sign extension is possible: - `SectionHeaderNum` and `SectionHeaderSize` are unsigned i16. - By C++ promotion rules, `SectionHeaderNum * SectionHeaderSize` results in a signed i32 value. - However, `SectionHeaderOffset` is i64, so the i32 product is promoted to i64 via sign extension - If `SectionHeaderNum * SectionHeaderSize` results in a number greater than 0x7FFFFF (greatest signed i32 number), then the upper bits get set to 1, producing the wrong result. This PR adds a cast to ensure the promotions do not result in unexpected sign extensions: While I recognize that the odds of `SectionHeaderNum` and `SectionHeaderSize` getting as big as sqrt(0x7FFFFFF) is rather unlikely, this change does not hurt or slow down the code, and is probably the safer thing to do anyway. If it is decided that this change is not necessarily, feel free to close the PR, and I will fix the Coverity triage to reflect the decision.
1 parent 153fab3 commit 1345ae0

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

sycl/source/detail/ur.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ static bool checkELFSectionPresent(const std::string &ExpectedSectionName,
306306

307307
// End early if we do not have the expected number of section headers or
308308
// if the read section string header index is out-of-range.
309-
if (ImgSize < SectionHeaderOffset + SectionHeaderNum * SectionHeaderSize ||
309+
if (ImgSize < SectionHeaderOffset + static_cast<uint64_t>(SectionHeaderNum) *
310+
SectionHeaderSize ||
310311
SectionStringsHeaderIndex >= SectionHeaderNum)
311312
return false;
312313

0 commit comments

Comments
 (0)