Timeline for variable declared as uint but accepting negative values instead of throwing errors
Current License: CC BY-SA 4.0
13 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 9, 2019 at 5:50 | vote | accept | Rama krishna Manchala | ||
Apr 8, 2019 at 8:32 | answer | added | Mikhail Vladimirov | timeline score: 2 | |
Apr 8, 2019 at 8:03 | comment | added | goodvibration |
The purpose is for you to keep the state variable x as uint . If you change it to int then it impacts all of the arithmetic operations involving x (there aren't any in the code which you posted, but I assume that there are such operations in your real contract).
|
|
Apr 8, 2019 at 7:31 | comment | added | Rama krishna Manchala | thank you.. i added it in the function itself and it worked... but if it accepts negative values then what is the main purpose of introducing uint then.. | |
Apr 8, 2019 at 7:15 | review | Low quality posts | |||
Apr 8, 2019 at 16:45 | |||||
Apr 8, 2019 at 7:05 | comment | added | goodvibration |
Like I said, the value will be converted to uint , so you'll need to add this assertion on the caller side, not in this function. Or you can use int instead of uint , and then require(_x>0) will become useful. You'll also need to change x=_x to x=uint(_x) .
|
|
Apr 8, 2019 at 7:03 | comment | added | Rama krishna Manchala | how to accept only positive values i tried require(_x>0) and assert(_x>0) also... | |
Apr 8, 2019 at 7:00 | comment | added | goodvibration | 5. If you want your code to be displayed properly, then indent it by 4 spaces. | |
Apr 8, 2019 at 7:00 | history | edited | user19510 | CC BY-SA 4.0 |
added 9 characters in body
|
Apr 8, 2019 at 7:00 | review | First posts | |||
Apr 8, 2019 at 16:48 | |||||
Apr 8, 2019 at 7:00 | history | edited | Rama krishna Manchala | CC BY-SA 4.0 |
deleted 2 characters in body
|
Apr 8, 2019 at 6:59 | comment | added | goodvibration |
1. There is no question here. 2. There is no demonstration here of what you are describing (nor the negative value neither the error thrown). 3. If you call the function from the off-chain with a negative value, then it will be converted to unsigned in the on-chain side, using 2s-complement. 4. If you call it from the on-chain, then you're likely to get a compilation error, or at least a compilation warning, but in either case - not a runtime exception.
|
|
Apr 8, 2019 at 6:57 | history | asked | Rama krishna Manchala | CC BY-SA 4.0 |