From owner-svn-src-all@freebsd.org Sat May 21 20:40:15 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 29403B45C99 for ; Sat, 21 May 2016 20:40:15 +0000 (UTC) (envelope-from pfg@FreeBSD.org) Received: from nm18-vm0.bullet.mail.bf1.yahoo.com (nm18-vm0.bullet.mail.bf1.yahoo.com [98.139.213.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D22BB1C6D for ; Sat, 21 May 2016 20:40:14 +0000 (UTC) (envelope-from pfg@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1463863205; bh=brRxa9SzH+4GCNiO7tNzyJdUgBX+DwAsfT0B5TO6Oho=; h=Subject:To:References:Cc:From:Date:In-Reply-To:From:Subject; b=hWAUI/RGwHT3MdQZ9zsVk+mDtQH2odpFniFdBzEHS4Xua2S/m7c1S4gKblR8w4e9T7Kg85acLiKuPlrv+dkkNi6TZsT8cgRtlOWOmveXTalrK4L2a8x/kIeaVlJBKZ6aV+KpaGL73e/z0h2WM7DGBrDObpNa1gnnCrsIz8YKMZgNSdQ62ch6WmXh+nPnjwd1DE4Dd86yaeExpSrOLCVkqTHMjXXwhZdickge3rT8Pf8AOMEw73vHBq1m8n3hu7mtJyFmhMEBZrl38a+N6wJjrxLF7kUdgNX/jcfuVE+a/DcmWhBCuJthzaNJ9+nOdjvVh6OaGD5JmY1SSiNnC6rrPg== Received: from [98.139.215.140] by nm18.bullet.mail.bf1.yahoo.com with NNFMP; 21 May 2016 20:40:05 -0000 Received: from [98.139.213.15] by tm11.bullet.mail.bf1.yahoo.com with NNFMP; 21 May 2016 20:40:05 -0000 Received: from [127.0.0.1] by smtp115.mail.bf1.yahoo.com with NNFMP; 21 May 2016 20:40:05 -0000 X-Yahoo-Newman-Id: 900308.53108.bm@smtp115.mail.bf1.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: kEYCJZkVM1lYYfyUthZL1o.GrFpVGj30AMjrJ7FMQIzOQrP 7pfTSEKsNyF5CqKbvGBeOa1D5v09YxufVpr3Gizj6lsOnj1Tgz7JFuPWvGAW wjrkhtEbq3QSM0gq4c6SE03W5_oamLU9OL9zZWf2cv9Jl4OoI_3HTp15E9H2 nEmtIXbbhfXyY0nB5tOnE0Vb2PsKJSYkxdH5oUg.5WxvLlNT8ptN_qg3UOSP NIEYxEyytnTZpLZZyOiunNOzSjtIb.nRQF4PpyXBa3B66IiUHwTRvcA2_yfa FD3d41eDsSRDnQzYLFmLYdX8glln6ZtbC7AYKMY5N8mud1EL.bySNHWVoKPx dvqNJ8NTcyB3W0BLZW8Qdv5j53BcB5whN3YoYxM6tGXkLr2uNcssxf0fsYws UuWrCbKVnt15jbwldBZWnhz5TJ3cIoymY4orQIpPpM.aD9OSR6LaeLQYZmmt grBNs9SHER9zyxRlaIGFA48YSBwuv6Wt8rQbRDu0OeUyKPNEmNHVInvysHqF WvMLAXeVqAArE2gxQBUwWEzxYpabczxcm X-Yahoo-SMTP: xcjD0guswBAZaPPIbxpWwLcp9Unf Subject: Re: svn commit: r300377 - head/sys/compat/ndis To: cem@FreeBSD.org References: <201605211752.u4LHqiHQ031457@repo.freebsd.org> Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org From: Pedro Giffuni Message-ID: Date: Sat, 21 May 2016 15:40:00 -0500 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 21 May 2016 20:40:15 -0000 On 05/21/16 14:05, Conrad Meyer wrote: > On Sat, May 21, 2016 at 10:52 AM, Pedro F. Giffuni wrote: >> Author: pfg >> Date: Sat May 21 17:52:44 2016 >> New Revision: 300377 >> URL: https://svnweb.freebsd.org/changeset/base/300377 >> >> Log: >> ndis(4): Avoid overflow. >> >> This is a long standing problem: our random() function returns an >> unsigned integer but the rand provided by ndis(4) returns an int. >> Scale it down. >> >> MFC after: 2 weeks >> >> Modified: >> head/sys/compat/ndis/subr_ntoskrnl.c >> >> Modified: head/sys/compat/ndis/subr_ntoskrnl.c >> ============================================================================== >> --- head/sys/compat/ndis/subr_ntoskrnl.c Sat May 21 17:38:43 2016 (r300376) >> +++ head/sys/compat/ndis/subr_ntoskrnl.c Sat May 21 17:52:44 2016 (r300377) >> @@ -3189,7 +3189,7 @@ static int >> rand(void) >> { >> >> - return (random()); >> + return (random() / 2 + 1); >> } >> >> static void >> > > > Won't this still return a negative integer in many cases? > > random(9) returns u_long, whereas this rand() routine returns 'int'. > > Even on architectures where long is the same size as ordinary > integers, the range of possible results of the 'random() / 2 + 1' > expression, before implicit cast to signed, is [1, 2^31] (inclusive). According to: sys/libkern/random.c The result is uniform on [0, 2^31 - 1]. > 2^31 is not representable by typical signed 32-bit integers, so this > will wrap to INT_MIN. Also, I'm not sure why zero is excluded from > the range. > It is not a good reason but the zero is sometimes inconvenient: if the value is going to be used as a multiplier in some calculation it will basically kill the random component. > On architectures where long is larger than ordinary integers, this > expression has no hope of fitting in the non-negative range of a > signed integer. > > Why not instead: > > return ((u_int)random() / 2); > TBH, I have seen the same conversion formula over and over and I just repeated it, so I am glad you are asking the question. Hopefully some else has a better answer? ;). Pedro. Pedro. > Best, > Conrad >