From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-x243.google.com (mail-wr0-x243.google.com [IPv6:2a00:1450:400c:c0c::243]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id EC71621D046C7 for ; Tue, 19 Sep 2017 10:01:00 -0700 (PDT) Received: by mail-wr0-x243.google.com with SMTP id g50so122609wra.3 for ; Tue, 19 Sep 2017 10:04:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=VgZgWBJb1tcME+AE6r7tSb0ttIxUMaOfI4A0poyDeJY=; b=qZU+uEJ7tKUCatUTjKC9fN+z9QnbnPFvI5I/MSEgsbJan8k30rv9R5NUJzzrnBb41X CVqPMglsULGEKqhLUDVCgSgz0hdb2YXdD+bD/QxcFJS/d6U38JhRLIDfvJG0NE4zYj/I Kckg0lGHjUsTtErQFe38kpdyavFYNmErcFedwS3Rp3uGvixeDwLYWH89CiJUTTebFxZo DTqXpnpFz+THoyjpyRLFxOXjujatBiSOTjOVEdEDmQFiHWM3ytZVUXfHIIqrAQgwLm6M XqJwnXH93uJJWXn28S51XGiKjgAxmriOQhtAp1/nWSQSW6nl+amW/trfhw/rx2bEtDDA SVGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:cc:references:from:message-id :date:user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=VgZgWBJb1tcME+AE6r7tSb0ttIxUMaOfI4A0poyDeJY=; b=FkE+RLnIrs9ZWGNFs5V2Kvq9mUY2re2/rO+lIPTLv/Q1RUIUTAsxIVZoCvCSJYM89S q5fxOtGrGrBnorWbhSRXPx5OqNdAv3DDg52NesPgiTofITHPfqtHlXJz6V0pe/1CVWzX ZmNEXFmznCxrlY3vINKSxtTJkZHFSLmXeipliLbLW9APy95xE3RVtEzSyQMglUwNotrU eXG8yGwHFt9jGhAc18/knqkubeA3i/Epb77ARgm5tmOG0qLcdhBbDLm5wkgxTE+Kw10d 0SOj3Pj5RFJlWJ10W91VwqtSruvGJPJZ/D6eBm+U+1l4qdsjEgmiGFPZRUmOpLnIG0U6 7D1Q== X-Gm-Message-State: AHPjjUhAocjWSJGcepkayh8UOUqYlFXhdph2+P4xhB8QUIrOeMval7Tx DdWdVBF6mlYIqjAHGjvanTo= X-Google-Smtp-Source: AOwi7QBzFLlKmGTfNhklgdb7nXGkpAalKtaAvspvLxh1NiFFNY4IMPvPsMtUn7mHdA1y0hwrYX5STw== X-Received: by 10.223.154.228 with SMTP id a91mr1942028wrc.246.1505840644403; Tue, 19 Sep 2017 10:04:04 -0700 (PDT) Received: from [192.168.10.165] (dynamic-adsl-78-12-246-117.clienti.tiscali.it. [78.12.246.117]) by smtp.googlemail.com with ESMTPSA id 133sm2251967wmu.4.2017.09.19.10.04.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Sep 2017 10:04:03 -0700 (PDT) Sender: Paolo Bonzini To: Hao Wu , edk2-devel@lists.01.org Cc: Eric Dong , Ye Ting , Wu Jiaxin , Fu Siyuan , Star Zeng References: <20170919114351.18448-1-hao.a.wu@intel.com> <20170919114351.18448-5-hao.a.wu@intel.com> From: Paolo Bonzini Message-ID: <1f272664-5423-9429-a91f-bda8112499da@redhat.com> Date: Tue, 19 Sep 2017 19:04:02 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <20170919114351.18448-5-hao.a.wu@intel.com> Subject: Re: [PATCH 4/6] MdeModulePkg/DxeNetLib: Fix negative value left shift X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Sep 2017 17:01:01 -0000 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 19/09/2017 13:43, Hao Wu wrote: > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=698 > > Within function NetRandomInitSeed(), left shift a negative value is used > in: > "~Time.Hour << 24" > > which involves undefined behavior. > > Since Time.Hour is of type UINT8 (range from 0 to 23), hence ~Time.Hour > will be a negative value (of type int, signed). > > According to the C11 spec, Section 6.5.7: >> 4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated >> bits are filled with zeros. If E1 has an unsigned type, the value >> of the result is E1 * 2^E2 , reduced modulo one more than the >> maximum value representable in the result type. If E1 has a signed >> type and nonnegative value, and E1 * 2^E2 is representable in the >> result type, then that is the resulting value; otherwise, the >> behavior is undefined. > > This commit explicitly cast 'Time.Hour' with UINT32 to resolve this issue. > > Cc: Steven Shi > Cc: Fu Siyuan > Cc: Ye Ting > Cc: Wu Jiaxin > Cc: Star Zeng > Cc: Eric Dong > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Hao Wu > --- > MdeModulePkg/Library/DxeNetLib/DxeNetLib.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c > index 7cd7e3aca0..ca5413edcc 100644 > --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c > +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c > @@ -872,7 +872,7 @@ NetRandomInitSeed ( > UINT64 MonotonicCount; > > gRT->GetTime (&Time, NULL); > - Seed = (~Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second); > + Seed = (~(UINT32)Time.Hour << 24 | Time.Day << 16 | Time.Minute << 8 | Time.Second); > Seed ^= Time.Nanosecond; > Seed ^= Time.Year << 7; Is there any reason why Seed must be XORed with 0xFF000000 (that's what the ~ does)? Would it make sense to either write it explicitly as a XOR, or perhaps the ~ can be removed altogether? Paolo