From owner-svn-src-all@freebsd.org Sat May 21 19:12:33 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 6785FB44915; Sat, 21 May 2016 19:12:33 +0000 (UTC) (envelope-from cse.cem@gmail.com) Received: from mail-io0-f180.google.com (mail-io0-f180.google.com [209.85.223.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 2820611B7; Sat, 21 May 2016 19:12:32 +0000 (UTC) (envelope-from cse.cem@gmail.com) Received: by mail-io0-f180.google.com with SMTP id 190so170920399iow.1; Sat, 21 May 2016 12:12:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:reply-to:in-reply-to:references :date:message-id:subject:from:to:cc; bh=HO1F7Wzh6QDlNM85QJ6HIFekm7hIRPy6G3vqMXr+qcQ=; b=LX6t/QTQLzWgEboPenGrKPhWO4t8WbCuMWyHoI3XsbiC23ITUQbjPcPwqMmLOp0hsv J1Zv15O4NGyA3VxquqYfnOx+4428rB481UGroUhM+4xiM02nO7bSuGb16UDNIqISjQz9 eJdVivuqdKHY40/eV5WCYehwNKN5gazIbAC0uoJU9owupHXwtMcd+SiBY6z2XNAuzmtf eaFyzECjzdNaXI/VSnMVQSfA0xnikQlK1IqwqhCnO5DVkGX+vPk/kvkVcXNA+72i11O9 hN1ns3/vHmaMjRD2+Hn0JuXPJMr7p2u0JiNY2bMoQYMQ3Q2reP1gWWdXQDF7K43GsXwP b5hA== X-Gm-Message-State: ALyK8tJCVh71ddqR1ax2N9sXWLEJP4wutx0tdNFk58pPvv7ruxeGaifMTwUtRO9GL2/uPA== X-Received: by 10.107.19.11 with SMTP id b11mr1530169ioj.57.1463857557481; Sat, 21 May 2016 12:05:57 -0700 (PDT) Received: from mail-it0-f46.google.com (mail-it0-f46.google.com. [209.85.214.46]) by smtp.gmail.com with ESMTPSA id b67sm7731818iob.33.2016.05.21.12.05.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 21 May 2016 12:05:57 -0700 (PDT) Received: by mail-it0-f46.google.com with SMTP id l63so10406815ita.1; Sat, 21 May 2016 12:05:57 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.36.81.209 with SMTP id s200mr7635838ita.34.1463857556974; Sat, 21 May 2016 12:05:56 -0700 (PDT) Reply-To: cem@FreeBSD.org Received: by 10.36.205.70 with HTTP; Sat, 21 May 2016 12:05:56 -0700 (PDT) In-Reply-To: <201605211752.u4LHqiHQ031457@repo.freebsd.org> References: <201605211752.u4LHqiHQ031457@repo.freebsd.org> Date: Sat, 21 May 2016 12:05:56 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: svn commit: r300377 - head/sys/compat/ndis From: Conrad Meyer To: "Pedro F. Giffuni" Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Content-Type: text/plain; charset=UTF-8 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 19:12:33 -0000 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). 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. 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); Best, Conrad