Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UBSAN] Fix runtime error about variable length array bound #41881

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,21 @@ void SiStripFedZeroSuppression::suppress(const std::vector<SiStripDigi>& in,
uint32_t detID,
const SiStripNoises& noise,
const SiStripThreshold& threshold) {
int inSize = in.size();
selectedSignal.clear();
size_t inSize = in.size();
Copy link
Contributor

Choose a reason for hiding this comment

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

why changing it to size_t?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was not needed to fix the UBSAN error but thought that as inSize is >=0 ( std::vector::size() returns size_t ... rigth) that is why I converted it to size_t and then update loop index i to be of same type and avoid extra arithmetic [a]

[a]

RecoLocalTracker/SiStripZeroSuppression/src/SiStripFedZeroSuppression.cc:144:22: error: comparison of unsigned expression in '>= 0' is always true [-Werror=type-limits]
  144 |     } else if (i - 1 >= 0 && in[i - 1].strip() == strip - 1) {
      |                ~~~~~~^~~~
RecoLocalTracker/SiStripZeroSuppression/src/SiStripFedZeroSuppression.cc:148:24: error: comparison of unsigned expression in '>= 0' is always true [-Werror=type-limits]
  148 |       } else if (i - 2 >= 0 && in[i - 2].strip() == strip - 2) {
      |                  ~~~~~~^~~~

if (inSize == 0) {
return;
}

SiStripNoises::Range detNoiseRange = noise.getRange(detID);
SiStripThreshold::Range detThRange = threshold.getRange(detID);

// reserving more than needed, but quicker than one at a time
selectedSignal.clear();
selectedSignal.reserve(inSize);

// load status
uint8_t stat[inSize];
for (int i = 0; i < inSize; i++) {
for (size_t i = 0; i < inSize; i++) {
auto strip = (uint32_t)in[i].strip();

auto ladc = in[i].adc();
Expand All @@ -114,7 +118,7 @@ void SiStripFedZeroSuppression::suppress(const std::vector<SiStripDigi>& in,
stat[i] = highTh;
}

for (int i = 0; i < inSize; i++) {
for (size_t i = 0; i < inSize; i++) {
auto strip = (uint32_t)in[i].strip();
Payload ldata;

Expand All @@ -137,11 +141,11 @@ void SiStripFedZeroSuppression::suppress(const std::vector<SiStripDigi>& in,

if (((strip) % 128) == 0) {
ldata.statPrev = zeroTh;
} else if (i - 1 >= 0 && in[i - 1].strip() == strip - 1) {
} else if (i >= 1 && in[i - 1].strip() == strip - 1) {
ldata.statPrev = stat[i - 1];
if (((strip) % 128) == 1) {
ldata.statPrev2 = zeroTh;
} else if (i - 2 >= 0 && in[i - 2].strip() == strip - 2) {
} else if (i >= 2 && in[i - 2].strip() == strip - 2) {
ldata.statPrev2 = stat[i - 2];
}
}
Expand Down