Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 21 Jul 2014 22:37:34 +0000 (UTC)
From:      "Pedro F. Giffuni" <pfg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r268962 - stable/10/lib/libc/gen
Message-ID:  <201407212237.s6LMbYbp086531@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pfg
Date: Mon Jul 21 22:37:33 2014
New Revision: 268962
URL: http://svnweb.freebsd.org/changeset/base/268962

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/10/lib/libc/gen/arc4random.c
  stable/10/lib/libc/gen/readpassphrase.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/lib/libc/gen/arc4random.c
==============================================================================
--- stable/10/lib/libc/gen/arc4random.c	Mon Jul 21 22:21:09 2014	(r268961)
+++ stable/10/lib/libc/gen/arc4random.c	Mon Jul 21 22:37:33 2014	(r268962)
@@ -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@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/10/lib/libc/gen/readpassphrase.c
==============================================================================
--- stable/10/lib/libc/gen/readpassphrase.c	Mon Jul 21 22:21:09 2014	(r268961)
+++ stable/10/lib/libc/gen/readpassphrase.c	Mon Jul 21 22:37:33 2014	(r268962)
@@ -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;
 		}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201407212237.s6LMbYbp086531>