util.crand: Try getrandom() again until buffer is filled
authorKim Alvefur <zash@zash.se>
Sat, 02 Dec 2017 10:58:37 +0100
changeset 8446 980885ba062c
parent 8445 3a390fa561bf
child 8447 adb079840714
util.crand: Try getrandom() again until buffer is filled
util-src/crand.c
--- a/util-src/crand.c	Sat Dec 02 02:12:06 2017 +0100
+++ b/util-src/crand.c	Sat Dec 02 10:58:37 2017 +0100
@@ -68,12 +68,22 @@
 	 * This acts like a read from /dev/urandom with the exception that it
 	 * *does* block if the entropy pool is not yet initialized.
 	 */
-	ret = getrandom(buf, len, 0);
+	int left = len;
+	char *b = buf;
+
+	do {
+		ret = getrandom(b, left, 0);
 
-	if(ret < 0) {
-		lua_pushstring(L, strerror(errno));
-		return lua_error(L);
-	}
+		if(ret < 0) {
+			lua_pushstring(L, strerror(errno));
+			return lua_error(L);
+		}
+
+		b += ret;
+		left -= ret;
+	} while(left > 0);
+
+	ret = len;
 
 #elif defined(WITH_ARC4RANDOM)
 	arc4random_buf(buf, len);