From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-vk0-x243.google.com (mail-vk0-x243.google.com [IPv6:2607:f8b0:400c:c05::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 94BF321A6F108 for ; Thu, 18 May 2017 19:45:32 -0700 (PDT) Received: by mail-vk0-x243.google.com with SMTP id h16so707961vkd.0 for ; Thu, 18 May 2017 19:45:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=v8oKF837ET3Nr8uQkuX1mONl7JDBQ1DY+SmUKYAxfpA=; b=enj39lDW7Qgx827BearzBoCWoSr9FotK87Mj82Rx3hUau1+LWJs2rY0E0f+V70aHCl Dc1PXjghLpCwZX9OGbvgPQ9IO2rGsiOKaoa+v+xDrtDy3wHxMLy8DRdmM/1hyzh9IiFx Bpz/zv+xMcBD3QVtTAhoUqq9JaefeMgDDHZoAX8xBeECQ7VXgbZ4aNZ3uZ9dYVJ49mFH xDsagdxTKNaTlXsn/CtHbSKrfomVtvHsjWzNBvGpCNz0KpDqRonmiCdf18tn/S1H71l5 zNFn1Q6QMRuviF+nNaciJm6eYTj83h1EdJyOc+tOqJlvspWssHrFBgm+5cF1Ht/bhr5Z PhpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=v8oKF837ET3Nr8uQkuX1mONl7JDBQ1DY+SmUKYAxfpA=; b=Dfs3Z/TwgGR5BacfTv6UnLCodgjhAM4xAXKDwwxUZpmRbnYtS8nPychf5ExInEMA4c QIuwtZQeyPdSUv57pOxdyAnNiqTtxkbPxORqCt2Q4XgEV/xWxImyiPMOEEX+EIZ4ZT+o o02x/rFLrWxLOXbb+aQsdGpmzieoKiVPpJYDulMCtlGsoL3Wb0TIGVA1eOH0+pMlSqZO qveHIMZavHBrlUZ8zRUC4L4jgLSZcoYcTaAW2wtoSxVFiciyyHrBfwnkX1wbdBXDHhTe bSnCXO92+tu5pKNEVtVHhzGdEwTDCdEnn8FYnBG26enzhVMNTC6u0Kswi5MBMA/s5uy4 itMQ== X-Gm-Message-State: AODbwcBP6ZhOqU1Tmy5pF1804dOJKhyd8qxNq6hMvNpY97hP9Ow+GAiJ 78z1xEEvTDzSHWa6QBQc1sQYw+//1g== X-Received: by 10.31.197.5 with SMTP id v5mr792029vkf.119.1495161931689; Thu, 18 May 2017 19:45:31 -0700 (PDT) MIME-Version: 1.0 Received: by 10.159.35.78 with HTTP; Thu, 18 May 2017 19:45:31 -0700 (PDT) In-Reply-To: References: <1494903391-716-1-git-send-email-s.temerkhanov@gmail.com> <1494903391-716-2-git-send-email-s.temerkhanov@gmail.com> <4A89E2EF3DFEDB4C8BFDE51014F606A14D72E8BC@shsmsx102.ccr.corp.intel.com> From: Sergei Temerkhanov Date: Fri, 19 May 2017 05:45:31 +0300 Message-ID: To: Laszlo Ersek Cc: "Gao, Liming" , "edk2-devel@lists.01.org" Subject: Re: [PATCH] MdePkg: Fix undefined behavior on variadic parameters 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: Fri, 19 May 2017 02:45:32 -0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, May 18, 2017 at 1:19 PM, Laszlo Ersek wrote: > On 05/16/17 14:10, Sergei Temerkhanov wrote: >> On Tue, May 16, 2017 at 8:10 AM, Gao, Liming wrot= e: >>> Sergey: >>> Could you give more detail on the undefined behavior on variadic para= meters? >>> >>> I see https://bugzilla.tianocore.org/show_bug.cgi?id=3D410 describe t= his issues found in the latest CLANG tool chain. Do you find other tool cha= in reports it? >> >> Yes, this is exactly the bug this patch fixes. >> >> As per the C99 standard: >> "The parameter parmN is the identifier of the rightmost parameter in >> the variable parameter list in the function definition (the one just >> before the , ...). If the parameter parmN is declared with the >> register storage class, with a function or array type, or with a type >> that is not compatible with the type that results after application of >> the default argument promotions, the behavior is undefined." >> >> That's exactly the case here since BOOLEAN is a typedef for unsigned >> char. It undergoes a promotion to an unsigned int > > Side topic: > > It is promoted, but not to "unsigned int". > > The standard says, in "6.3.1.1 Boolean, characters, and integers", > paragraph 2, > > The following may be used in an expression wherever an /int/ or > /unsigned int/ may be used: > > =E2=80=94 An object or expression with an integer type whose integer > conversion rank is less than or equal to the rank of /int/ and > /unsigned int/. > =E2=80=94 A bit-field of type /_Bool/, /int/, /signed int/, or > /unsigned int/. > > If an /int/ can represent all values of the original type, the value > is converted to an /int/; otherwise, it is converted to an > /unsigned int/. These are called the /integer promotions/. [...] > > On all supported edk2 platforms, "unsigned char"'s range is 0..255 > inclusive, which can be represented by "int" (again on all supported > edk2 platforms). So the promotion occurs to "int", not "unsigned int" > > > Furthermore, in place of the suggested UINTN type (which is fine), the > following further types would be correct: INT32, UINT32, INT64, UINT64, > INTN. On 32-bit architectures, using 64-bit types here may change the ABI. Which = might affect some corner cases like linking precompiled object files to the library in question. > The reason is that all of these map to standard C types, on all > edk2 platforms, whose integer conversion ranks are not less than that of > "int" and "unsigned int". Hence they are all unaffected by the integer > promotions. > > (This digression does not affect your main point, which remains correct; > I just wanted to be precise here, since we're quoting the standard.) > > Thanks > Laszlo > >> which is not a >> compatible type for unsigned char. Correct me if I'm wrong. >> >> Regards, >> Sergey >> >>> >>> Thanks >>> Liming >>>> -----Original Message----- >>>> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of= Sergey Temerkhanov >>>> Sent: Tuesday, May 16, 2017 10:57 AM >>>> To: edk2-devel@lists.01.org >>>> Subject: [edk2] [PATCH] MdePkg: Fix undefined behavior on variadic par= ameters >>>> >>>> Fix undefined behavior by avoiding parameter type promotion >>>> >>>> Signed-off-by: Sergey Temerkhanov >>>> --- >>>> MdePkg/Include/Library/UefiLib.h | 2 +- >>>> MdePkg/Library/UefiLib/UefiLib.c | 2 +- >>>> 2 files changed, 2 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/MdePkg/Include/Library/UefiLib.h b/MdePkg/Include/Library= /UefiLib.h >>>> index 0b14792..4e4697c 100644 >>>> --- a/MdePkg/Include/Library/UefiLib.h >>>> +++ b/MdePkg/Include/Library/UefiLib.h >>>> @@ -818,7 +818,7 @@ CHAR8 * >>>> EFIAPI >>>> GetBestLanguage ( >>>> IN CONST CHAR8 *SupportedLanguages, >>>> - IN BOOLEAN Iso639Language, >>>> + IN UINTN Iso639Language, >>>> ... >>>> ); >>>> >>>> diff --git a/MdePkg/Library/UefiLib/UefiLib.c b/MdePkg/Library/UefiLib= /UefiLib.c >>>> index a7eee01..74528ec 100644 >>>> --- a/MdePkg/Library/UefiLib/UefiLib.c >>>> +++ b/MdePkg/Library/UefiLib/UefiLib.c >>>> @@ -1514,7 +1514,7 @@ CHAR8 * >>>> EFIAPI >>>> GetBestLanguage ( >>>> IN CONST CHAR8 *SupportedLanguages, >>>> - IN BOOLEAN Iso639Language, >>>> + IN UINTN Iso639Language, >>>> ... >>>> ) >>>> { >>>> -- >>>> 2.7.4 >>>> >>>> _______________________________________________ >>>> edk2-devel mailing list >>>> edk2-devel@lists.01.org >>>> https://lists.01.org/mailman/listinfo/edk2-devel >> _______________________________________________ >> edk2-devel mailing list >> edk2-devel@lists.01.org >> https://lists.01.org/mailman/listinfo/edk2-devel >> >