/soc/2015/mmcc/rand: 8cf1307f1ed8: Finish /dev/urandom read logic
Michael McConville
mmcconville at mykolab.com
Sat Aug 15 01:08:40 EDT 2015
Changeset: 8cf1307f1ed8ad6acb8b7ff1507eec8f847983e8
Author: Michael McConville <mmcconville at mykolab.com>
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;
}
}
More information about the Commits
mailing list