From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753010AbcBBGVE (ORCPT ); Tue, 2 Feb 2016 01:21:04 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:57501 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751002AbcBBGVC (ORCPT ); Tue, 2 Feb 2016 01:21:02 -0500 Date: Mon, 1 Feb 2016 22:23:51 -0800 From: Andrew Morton To: Andrzej Hajda Cc: linux-kernel@vger.kernel.org (open list), Bartlomiej Zolnierkiewicz , Marek Szyprowski Subject: Re: [PATCH v2] err.h: allow IS_ERR_VALUE to handle properly more types Message-Id: <20160201222351.dbbca353.akpm@linux-foundation.org> In-Reply-To: <1453969648-4036-1-git-send-email-a.hajda@samsung.com> References: <201601072342.xanwLeaS%fengguang.wu@intel.com> <1453969648-4036-1-git-send-email-a.hajda@samsung.com> X-Mailer: Sylpheed 2.7.1 (GTK+ 2.18.9; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 28 Jan 2016 09:27:28 +0100 Andrzej Hajda wrote: > Current implementation of IS_ERR_VALUE works correctly only with > following types: > - unsigned long, > - short, int, long. > Other types are handled incorrectly either on 32-bit either on 64-bit > either on both architectures. > The patch fixes it by comparing argument with MAX_ERRNO casted > to argument's type for unsigned types and comparing with zero for signed > types. As a result all integer types bigger than char are handled properly. > > I have analyzed usage of IS_ERR_VALUE using coccinelle and in about 35 > cases it is used incorrectly, ie it can hide errors depending of 32/64 bit > architecture. Instead of fixing usage I propose to enhance the macro > to cover more types. > And just for the record: the macro is used 101 times with signed variables, > I am not sure if it should be preferred over simple comparison "ret < 0", > but the new version can do it as well. > > And below list of detected potential errors: > > ... > > --- a/include/linux/err.h > +++ b/include/linux/err.h > @@ -18,7 +18,9 @@ > > #ifndef __ASSEMBLY__ > > -#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) > +#define IS_ERR_VALUE(x) ((typeof(x))(-1) <= 0 \ > + ? unlikely((x) < 0) \ > + : unlikely((x) >= (typeof(x))-MAX_ERRNO)) > hm, seems complicated. Can we simply cast the value to long? #define IS_ERR_VALUE(x) ((long)x < 0) && (long)x >= (long)-MAX_ERRNO) and simplify that to #define IS_ERR_VALUE(x) ((unsigned long)(long)x >= (unsigned long)-MAX_ERRNO) or something like that.