From 18ca9b364b75c14fa21c26ca93b8cd86776017c4 Mon Sep 17 00:00:00 2001 From: Steve Fung Date: Sat, 12 Sep 2015 03:21:59 -0700 Subject: [PATCH] crash_reporter: Ensure crash_sender spread time is not negative When calculating the crash_sender spread time, make sure that the random number is not negative when converted to a shell int variable. Bug: 24004011 Change-Id: I3b95dc244a26270ef2b93d5af4b0593a93eedcad --- crash_reporter/crash_sender | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crash_reporter/crash_sender b/crash_reporter/crash_sender index 7f9062afb..cb7c10c25 100755 --- a/crash_reporter/crash_sender +++ b/crash_reporter/crash_sender @@ -180,10 +180,21 @@ is_device_coredump_upload_allowed() { } # Generate a uniform random number in 0..max-1. +# POSIX arithmetic expansion requires support of at least signed long integers. +# On 32-bit systems, that may mean 32-bit signed integers, in which case the +# 32-bit random number read from /dev/urandom may be interpreted as negative +# when used inside an arithmetic expansion (since the high bit might be set). +# mksh at least is known to behave this way. +# For this case, simply take the absolute value, which will still give a +# roughly uniform random distribution for the modulo (as we are merely ignoring +# the high/sign bit). +# See corresponding Arithmetic Expansion and Arithmetic Expression sections: +# POSIX: http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_04 +# mksh: http://linux.die.net/man/1/mksh generate_uniform_random() { local max=$1 local random="$(od -An -N4 -tu /dev/urandom)" - echo $((random % max)) + echo $(((random < 0 ? -random : random) % max)) } # Check if sending a crash now does not exceed the maximum 24hr rate and