]> git.proxmox.com Git - qemu-server.git/commitdiff
gen_rand_chars: handle errors properly
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Mon, 22 Jun 2020 10:03:01 +0000 (12:03 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Mon, 22 Jun 2020 10:03:01 +0000 (12:03 +0200)
should not really happen on modern systems, but random_bytes just
returns false if it fails to generate random bytes, in which case we
want to die instead of returning an empty 'random' string.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
PVE/API2/Qemu.pm

index dcb364d952771f4b06e52f9cfa1ce9876e281bb4..3965c269fdf1a850f7044bb85c453250e5c965cd 100644 (file)
@@ -1597,8 +1597,12 @@ my $gen_rand_chars = sub {
     die "invalid length $length" if $length < 1;
 
     my $min = ord('!'); # first printable ascii
-    my @rand_bytes = split '', Crypt::OpenSSL::Random::random_bytes($length);
-    my $str = join('', map { chr((ord($_) & 0x3F) + $min) } @rand_bytes);
+
+    my $rand_bytes = Crypt::OpenSSL::Random::random_bytes($length);
+    die "failed to generate random bytes!\n"
+      if !$rand_bytes;
+
+    my $str = join('', map { chr((ord($_) & 0x3F) + $min) } split('', $rand_bytes));
 
     return $str;
 };