parcimonie.sh
parcimonie.sh copied to clipboard
Make sure random numbers are greater than zero.
On my system, when sending random numbers from getRandom through the modulo operator the result sometimes is negative. This doesn't affect getRandomKey much since gpg won't try to get a key that is empty or doesn't exist, but the negative sign is seen as a switch in sleep, which errors out. This is probably due to a bug in expr, but this is a simple workaround for it.
This is pretty weird. What shell is this and what version? If not implemented by shell, which version of expr
is it? Can you confirm that getRandom
itself never returns negative numbers? How many keys do you have?
Yes, I enabled debugging mode with bash -x and can confirm that getRandom is never negative. Using bash 4.3.42 on OpenBSD, so this is something with OpenBSD's expr I think.
I only have a handful of keys, so I don't think that is the issue. Here's a specific expression for expr that I got from bash -x that triggers a negative value:
expr 900 + 2663923636 % '(' 2 '*' 604800 / 10 ')'
This returns -18120 when it should return 22456.
As I expected, this has to do with OpenBSD's expr. There is a discussion about it.
Ping.
Sorry, I got busy and forgot about this. I think I got the style right this time. For what it's worth, this issue has been fixed in OpenBSD so these changes aren't required there for OpenBSD >= 5.9. There still could be other expr implementations though where integer overflow might be an issue.