svn commit: r268963 - stable/9/lib/libc/gen

Pedro F. Giffuni pfg at FreeBSD.org
Mon Jul 21 22:44:07 UTC 2014


Author: pfg
Date: Mon Jul 21 22:44:06 2014
New Revision: 268963
URL: http://svnweb.freebsd.org/changeset/base/268963

Log:
  MFC	r268642:
  libc/gen: small updates to code originating at OpenBSD
  
  arc4random.c
  - CVS rev. 1.22
  Change arc4random_uniform() to calculate ``2**32 % upper_bound'' as
  ``-upper_bound % upper_bound''. Simplifies the code and makes it the
  same on both ILP32 and LP64 architectures, and also slightly faster on
  LP64 architectures by using a 32-bit remainder instead of a 64-bit
  remainder.
  - CVS rev. 1.23
  Spacing
  
  readpassphrase.c
  -CVS rev. v 1.24
  most obvious unsigned char casts for ctype
  
  Obtained from:	OpenBSD

Modified:
  stable/9/lib/libc/gen/arc4random.c
  stable/9/lib/libc/gen/readpassphrase.c
Directory Properties:
  stable/9/lib/libc/   (props changed)

Modified: stable/9/lib/libc/gen/arc4random.c
==============================================================================
--- stable/9/lib/libc/gen/arc4random.c	Mon Jul 21 22:37:33 2014	(r268962)
+++ stable/9/lib/libc/gen/arc4random.c	Mon Jul 21 22:44:06 2014	(r268963)
@@ -1,4 +1,4 @@
-/*	$OpenBSD: arc4random.c,v 1.22 2010/12/22 08:23:42 otto Exp $	*/
+/*	$OpenBSD: arc4random.c,v 1.24 2013/06/11 16:59:50 deraadt Exp $	*/
 
 /*
  * Copyright (c) 1996, David Mazieres <dm at uun.org>
@@ -182,8 +182,7 @@ arc4_stir_if_needed(void)
 {
 	pid_t pid = getpid();
 
-	if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid)
-	{
+	if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != pid) {
 		arc4_stir_pid = pid;
 		arc4_stir();
 	}
@@ -276,18 +275,8 @@ arc4random_uniform(u_int32_t upper_bound
 	if (upper_bound < 2)
 		return 0;
 
-#if (ULONG_MAX > 0xffffffffUL)
-	min = 0x100000000UL % upper_bound;
-#else
-	/* Calculate (2**32 % upper_bound) avoiding 64-bit math */
-	if (upper_bound > 0x80000000)
-		min = 1 + ~upper_bound;		/* 2**32 - upper_bound */
-	else {
-		/* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */
-		min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
-	}
-#endif
-
+	/* 2**32 % x == (2**32 - x) % x */
+	min = -upper_bound % upper_bound;
 	/*
 	 * This could theoretically loop forever but each retry has
 	 * p > 0.5 (worst case, usually far better) of selecting a

Modified: stable/9/lib/libc/gen/readpassphrase.c
==============================================================================
--- stable/9/lib/libc/gen/readpassphrase.c	Mon Jul 21 22:37:33 2014	(r268962)
+++ stable/9/lib/libc/gen/readpassphrase.c	Mon Jul 21 22:44:06 2014	(r268963)
@@ -1,4 +1,4 @@
-/*	$OpenBSD: readpassphrase.c,v 1.23 2010/05/14 13:30:34 millert Exp $	*/
+/*	$OpenBSD: readpassphrase.c,v 1.24 2013/11/24 23:51:29 deraadt Exp $	*/
 
 /*
  * Copyright (c) 2000-2002, 2007, 2010
@@ -122,11 +122,11 @@ restart:
 		if (p < end) {
 			if ((flags & RPP_SEVENBIT))
 				ch &= 0x7f;
-			if (isalpha(ch)) {
+			if (isalpha((unsigned char)ch)) {
 				if ((flags & RPP_FORCELOWER))
-					ch = (char)tolower(ch);
+					ch = (char)tolower((unsigned char)ch);
 				if ((flags & RPP_FORCEUPPER))
-					ch = (char)toupper(ch);
+					ch = (char)toupper((unsigned char)ch);
 			}
 			*p++ = ch;
 		}


More information about the svn-src-stable-9 mailing list