From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM02-BL2-obe.outbound.protection.outlook.com (mail-bl2nam02on0706.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe46::706]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 4C9331A1E0B for ; Tue, 27 Sep 2016 05:29:23 -0700 (PDT) Received: from AT5PR84MB0291.NAMPRD84.PROD.OUTLOOK.COM (10.162.138.25) by AT5PR84MB0290.NAMPRD84.PROD.OUTLOOK.COM (10.162.138.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.639.5; Tue, 27 Sep 2016 12:29:21 +0000 Received: from AT5PR84MB0291.NAMPRD84.PROD.OUTLOOK.COM ([10.162.138.25]) by AT5PR84MB0291.NAMPRD84.PROD.OUTLOOK.COM ([10.162.138.25]) with mapi id 15.01.0639.011; Tue, 27 Sep 2016 12:29:21 +0000 From: "Cohen, Eugene" To: Laszlo Ersek CC: "edk2-devel@lists.01.org" , "Kinney, Michael D" , Alexei Fedorov Thread-Topic: [edk2] What is the right way to print a UINTN? Thread-Index: AdIX/FZUkJ+/GDfSRZe7u7ShyrQtxgADrrkAACt3LCA= Date: Tue, 27 Sep 2016 12:29:21 +0000 Message-ID: References: <0de4dd03-faa7-1608-9625-369ab5d6e682@redhat.com> In-Reply-To: <0de4dd03-faa7-1608-9625-369ab5d6e682@redhat.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=eugene@hp.com; x-originating-ip: [15.65.252.13] x-ms-office365-filtering-correlation-id: 4da50cc7-25c5-48a3-eb78-08d3e6d1e894 x-microsoft-exchange-diagnostics: 1; AT5PR84MB0290; 6:bhZs0hKTZIc2ys3V03CA3rwtd03gHD+G0wLyEGOzmGQfXG/LDzmlgZJ91FNv8EZietQ4mlKw58jtoenErUSppawjUBNZWoASdUS1Cw7iK3zaBprum4jCmj81eMsvvRTzBgBkzALTgoT4kTpg8t8y/ASazD06dV+wrpVl5wHGn07/+sKOJ6ecorkbvKB1YH94TUaEKHPTaVQJzJGLVjUdWRvdc504Wqkl11gl1s6HEsGGFJMF+MdXD7x1loPWB7NNWkOmGyy5Uc0VOFDW1A/6FPi/++RJlO+sKWFh5ZCrOfU=; 5:jGKrFjeuZWKYEivjEk/4+uO358ITPQcKXqzMmIUXyYX4QLIP44RmA5NfB/xTJwv0FrDA+F+/rhIoE3Lg/vxEcc3G7jQe7y7tBjs4SgpkASUy0fjYLtKKCD4lGONttCUmXf3ajpEdTHonnsvgLBRWHA==; 24:ncpZGL609f7Yuc1jiP1bXIm7GFaCSObK/ImjOJkXa2+9YIZHQNyViAxDEGS+ILWdFpIYArpHlhNk06FF6irVfGXIdX7Zu/BbnDJ1+ODn07o=; 7:FXq2unr6f4XIVIBE51gwP515F51f0gj3D4MIa9IOvYkRCpAB/LtJnNFEjIvSp/oYOJA217xFIfF2SeQ+LfNZ1M8ia5tPQuEFDvrGaSP6KaqLRpiQF80Y5EIrA5flMYwMzM7C+Y8nGxZhYQ4kOAJL3VuW+RiDhfWc89zNJSQCVC3S0LhWT5uQQWHVhGmdX9FvU9y4mX9UHbo/No3VHijx8njMfhd3EOltSO4UmsYRtelZ9ln1HMrQsASsrhDS3hlh/ltwFy8piXQd4IF0k6/Akt9pTWS5X3WqQUMdIwwVcGZ/uim+31KcYqHJDRqrJep4 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:AT5PR84MB0290; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001); SRVR:AT5PR84MB0290; BCL:0; PCL:0; RULEID:; SRVR:AT5PR84MB0290; x-forefront-prvs: 007814487B x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(6009001)(7916002)(6602003)(189002)(199003)(50986999)(92566002)(5002640100001)(54356999)(97736004)(10400500002)(7736002)(6116002)(2900100001)(2906002)(76176999)(101416001)(3846002)(305945005)(9686002)(86362001)(33656002)(81156014)(102836003)(77096005)(586003)(81166006)(8936002)(4326007)(99286002)(189998001)(8676002)(7846002)(7696004)(87936001)(110136003)(122556002)(2950100002)(74316002)(6916009)(11100500001)(105586002)(5660300001)(3660700001)(3280700002)(106356001)(68736007)(66066001)(43043002); DIR:OUT; SFP:1102; SCL:1; SRVR:AT5PR84MB0290; H:AT5PR84MB0291.NAMPRD84.PROD.OUTLOOK.COM; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; received-spf: None (protection.outlook.com: hp.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: hp.com X-MS-Exchange-CrossTenant-originalarrivaltime: 27 Sep 2016 12:29:21.1766 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: ca7981a2-785a-463d-b82a-3db87dfc3ce6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AT5PR84MB0290 Subject: Re: What is the right way to print a UINTN? X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Sep 2016 12:29:23 -0000 Content-Language: en-US Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable Laszlo, =20 > I print INTN / UINTN values with: > - casting them unconditionally to INT64 / UINT64, > - printing the converted values with the matching conversion > specifiers, > such as %Ld (for INT64) and %Lu or %Lx (for UINT64). >=20 > This solution requires a bit more typing, and it is a bit pessimistic > for 32-bit builds. On the positive side, it is robust / portable, and > completely valid C. >=20 > It is inspired by the standard C types intmax_t / uintmax_t. If you > write portable C code and want to print a value of some integer type, > where the spec only states "signed" or "unsigned integer type", but > the > actual type is either implementation defined or unspecified, > converting > the value to intmax_t / uintmax_t, and then printing it with %jd vs. %ju > / %jx, is safe. Thanks - this makes sense. If this methodology is consistent with standard= C then perhaps it's the best compromise even if it's messy to read. >>From a consistency perspective I see a lot of variation in usage - often UI= NTNs are printed with %x / %d (technically it should be %u but this is a co= mmon error - just compare the number of occurrences of %u in MdeModulePkg v= ersus %d). This means that the caller is expecting that the value will nev= er exceed 2^32-1 on 64-bit systems since we are doing 64-bit to 32-bit trun= cation through the cast in the VA_ARG macro. I'm concerned that this requi= res the developer to know the constraints on the value in all circumstances= which seems dubious - after all that's why we have types in the first plac= e, so the tools can help us do the right thing. I'm envisioning having to create a slide in the future for UEFI training ab= out the proper use of UINTNs and describing "If you think it may exceed 2^3= 2-1 then upcast to UINT64, otherwise don't worry about it" and it makes me = squirm. Eugene