Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759412AbXLQSYT (ORCPT ); Mon, 17 Dec 2007 13:24:19 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1754932AbXLQSYG (ORCPT ); Mon, 17 Dec 2007 13:24:06 -0500 Received: from zeniv.linux.org.uk ([195.92.253.2]:35424 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754314AbXLQSYD (ORCPT ); Mon, 17 Dec 2007 13:24:03 -0500 Date: Mon, 17 Dec 2007 18:23:12 +0000 From: Al Viro To: Eric Dumazet Cc: Linus Torvalds , Herbert Xu , John Reiser , Andrew Morton , security@kernel.org, tytso@mit.edu, Linux Kernel Mailing List , mpm@selenic.com, linux-sparse@vger.kernel.org Subject: Re: Signed divides vs shifts (Re: [Security] /dev/urandom uses uninit bytes, leaks user data) Message-ID: <20071217182312.GD8181@ftp.linux.org.uk> References: <20071217185557.0b501e23.dada1@cosmosbay.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20071217185557.0b501e23.dada1@cosmosbay.com> User-Agent: Mutt/1.4.1i Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 963 Lines: 43 On Mon, Dec 17, 2007 at 06:55:57PM +0100, Eric Dumazet wrote: > long *mid(long *a, long *b) > { > return ((a - b) / 2 + a); > } ... is not actually a middle (you'd want b-a, not a-b there), but anyway > It gave : > mid: > movq %rdi, %rdx > subq %rsi, %rdx > sarq $3, %rdx > movq %rdx, %rax > shrq $63, %rax > addq %rdx, %rax > sarq %rax > leaq (%rdi,%rax,8), %rax > ret > > while > > long *mid(long *a, long *b) > { > return ((a - b) / 2u + a); > } ... undefined behaviour if a < b > and while : > > long *mid(long *a, long *b) > { > return (((unsigned long)(a - b)) / 2 + a); > } undefined behaviour, again. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/