/soc/2015/mmcc/rand: 8cf1307f1ed8: Finish /dev/urandom read logic

Michael McConville <[email protected]>
Newsgroups gmane.comp.gnome.gaim.cvs
Message-ID <[email protected]>
Changeset: 8cf1307f1ed8ad6acb8b7ff1507eec8f847983e8
Author:	 Michael McConville <[email protected]>
Date:	 2015-08-14 22:57 -0400
Branch:	 default
URL: https://hg.pidgin.im/soc/2015/mmcc/rand/rev/8cf1307f1ed8

Description:

Finish /dev/urandom read logic

diffstat:

 libpurple/util.c |  26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

diffs (46 lines):

diff --git a/libpurple/util.c b/libpurple/util.c
--- a/libpurple/util.c
+++ b/libpurple/util.c
@@ -4993,9 +4993,10 @@ purple_util_random(void *buf, size_t len
 {
 	int fd;
 	ssize_t res;
+	size_t total = 0;
 	PurpleSslOps *ops = purple_ssl_get_ops();
 
-	/* use SSL API if possible... */
+	/* use SSL API if available... */
 	if (ops && ops->rand_bytes) {
 		return (ops->rand_bytes)(buf, len);
 	/* ...otherwise, fall back to /dev/urandom */
@@ -5006,16 +5007,21 @@ purple_util_random(void *buf, size_t len
 					"could not open /dev/urandom: %s", g_strerror(errno));
 			return 1;
 		}
-		res = read(fd, buf, len);
-		if (res < 0) {
-			purple_debug_error("purple_random",
-					"/dev/urandom read failed: %s", g_strerror(errno));
-			return 1;
-		/* redundant condition to guard against overflow on cast */
-		} else if (res >= 0 && (size_t)res != len) {
-			purple_debug_error("purple_random", "/dev/urandom read returned too little data");
-			return 1;
+		/* this loop could become infinite, but that would be bizarre */
+		while (total < len) {
+			res = read(fd, buf, len - total);
+			if (res < 0) {
+				purple_debug_error("purple_random",
+						"/dev/urandom read failed: %s", g_strerror(errno));
+				close(fd);
+				return 1;
+			/* condition used to clarify danger of underflow on cast */
+			} else {
+				buf += (size_t)res;
+				total += (size_t)res;
+			}
 		}
+		close(fd);
 		return 0;
 	}
 }

_______________________________________________
Commits mailing list
[email protected]
https://pidgin.im/cgi-bin/mailman/listinfo/commits
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.