parcimonie.sh icon indicating copy to clipboard operation
parcimonie.sh copied to clipboard

Make sure random numbers are greater than zero.

Open bsvh opened this issue 9 years ago • 6 comments

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.

bsvh avatar Oct 28 '15 23:10 bsvh

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?

EtiennePerot avatar Oct 29 '15 03:10 EtiennePerot

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.

bsvh avatar Oct 29 '15 04:10 bsvh

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.

bsvh avatar Oct 29 '15 05:10 bsvh

As I expected, this has to do with OpenBSD's expr. There is a discussion about it.

bsvh avatar Oct 29 '15 05:10 bsvh

Ping.

EtiennePerot avatar Apr 02 '16 19:04 EtiennePerot

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.

bsvh avatar Apr 02 '16 23:04 bsvh