Disable overflow check

When I compile and execute a RUST program, I receive the error 'thread' main 'panicked at' attempt to shift left with overflow '. This seems to be due to the automatic overflow check by cargo causing the program to fail. How can I prohibit overflow checks? In which files are modifications made?

I found a post that introduced some methods, but I don't know how to use them.
How can integer overflow protection be turned off? - Stack Overflow
When I searched for the. toml file in the file library, I found ' Cargo. toml '

Use the wrapping_shl method instead. So, instead of x << y, you do x.wrapping_shl(y).

1 Like

This is the location of the error code

What would you like this function to return when shift is 32?

I used the function you mentioned instead of "<<", and the compiled report looks like this.

For that particular error, the fix suggested by the compiler is correct.

So, what do you think would be better to change this ?

What I think would be best is that you find an answer to this question:

What would you like this function to return when shift is 32?

If you don't know the answer to this, then its rather difficult to help you. I can come up with some answer that I think is reasonable and tell you how to implement that, but ultimately, the correct answer depends on how you're using pow2_size, and the behavior I suggest might be incorrect for your application.


This function is used here.

Sorry, I don't know what that is.

opentitan/sw/host/opentitanlib/src/spiflash/sfdp.rs at master · lowRISC/opentitan · GitHub
This is the source code

Is this perhaps code you did not write? In that case, I would file a bug with the owners of the code.

Yes, this is the code for an open source project, and I encountered an error when running it

Your function has the possibility to try and produce a number that is bigger than the u32 it returns can represent. I think you have to decide what the result should be in that case.

That panic is trying to tell the programmer that their program has a bug. A situation that if allowed to proceed would make no sense. Something they have not thought about.

I notice the code contains a check for a shift of zero, why not add a check for a shift bigger than 31?
Having added that check you will have to decide what the return value should be in the error case. Or perhaps this function should return a Result type.

1 Like

I'm suspicious of the declared sector sizes just being wrong. Does this device actually have more than 4 GB of flash to erase?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.