From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6CBA6C433F5 for ; Thu, 3 Mar 2022 04:58:42 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.94.2) (envelope-from ) id 1nPdY1-0002ua-K1; Thu, 03 Mar 2022 04:58:40 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nPdXy-0002uQ-J2 for linux-f2fs-devel@lists.sourceforge.net; Thu, 03 Mar 2022 04:58:37 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:Content-Type:MIME-Version :In-Reply-To:References:Message-ID:Date:Subject:CC:To:From:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=AX7+ZsOTJ+tvi0gVsM0w+fZgOUK9/kNDCw1KP+h77Lo=; b=ma9kuHnEkICqTHpS7/O4AOj8Xl AeBfz0VIu+dGUwe9l0ilyR3NRjqz+1Z4ng2BqYxxv395X65GNcz00NRz8iKWO+Zlgdk8wLqCzDQuD y8XESXZ+kLsQF0jYFHqVW5OgOrBnOMD6PW7U3/sY9S6zXJxXBQjdxZwkDIOZ5tye8RBA=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:Content-Type:MIME-Version:In-Reply-To: References:Message-ID:Date:Subject:CC:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=AX7+ZsOTJ+tvi0gVsM0w+fZgOUK9/kNDCw1KP+h77Lo=; b=J9fz/yg9XdBN3b3YnlMh4JafbS 1niCmI/VXi9shVzZba5/JRceF+gifJyMZpVEZta6ELMuBmuBmYweR4WbqiNvGpURuiz02WpSFZtpe Mbs4K9l+b4lPH2FjGTWNckQyjzRsXc+J4bl9yf3IRHqOQIulRQCCcn42ikGymDFvK4HE=; Received: from eu-smtp-delivery-151.mimecast.com ([185.58.86.151]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.94.2) id 1nPdXu-0003S5-91 for linux-f2fs-devel@lists.sourceforge.net; Thu, 03 Mar 2022 04:58:37 +0000 Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mtapsc-5-c9saVIHFMHux_Sno3vxg6w-1; Thu, 03 Mar 2022 04:58:25 +0000 X-MC-Unique: c9saVIHFMHux_Sno3vxg6w-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) by AcuMS.aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Thu, 3 Mar 2022 04:58:23 +0000 Received: from AcuMS.Aculab.com ([fe80::994c:f5c2:35d6:9b65]) by AcuMS.aculab.com ([fe80::994c:f5c2:35d6:9b65%12]) with mapi id 15.00.1497.028; Thu, 3 Mar 2022 04:58:23 +0000 From: David Laight To: 'Xiaomeng Tong' Thread-Topic: [PATCH 2/6] treewide: remove using list iterator after loop body as a ptr Thread-Index: AQHYLhg9+DU/OogLf0+tiSFmjztyUKysHu+QgADRVYCAACVtoA== Date: Thu, 3 Mar 2022 04:58:23 +0000 Message-ID: <39404befad5b44b385698ff65465abe5@AcuMS.aculab.com> References: <1077f17e50d34dc2bbfdf4e52a1cb2fd@AcuMS.aculab.com> <20220303022729.9321-1-xiam0nd.tong@gmail.com> In-Reply-To: <20220303022729.9321-1-xiam0nd.tong@gmail.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Headers-End: 1nPdXu-0003S5-91 Subject: Re: [f2fs-dev] [PATCH 2/6] treewide: remove using list iterator after loop body as a ptr X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "alsa-devel@alsa-project.org" , "kvm@vger.kernel.org" , "gustavo@embeddedor.com" , "linux-iio@vger.kernel.org" , "kgdb-bugreport@lists.sourceforge.net" , "linux@rasmusvillemoes.dk" , "dri-devel@lists.freedesktop.org" , "c.giuffrida@vu.nl" , "amd-gfx@lists.freedesktop.org" , "torvalds@linux-foundation.org" , "samba-technical@lists.samba.org" , "linux1394-devel@lists.sourceforge.net" , "drbd-dev@lists.linbit.com" , "linux-arch@vger.kernel.org" , "linux-cifs@vger.kernel.org" , "linux-aspeed@lists.ozlabs.org" , "linux-scsi@vger.kernel.org" , "linux-rdma@vger.kernel.org" , "linux-staging@lists.linux.dev" , "h.j.bos@vu.nl" , "jgg@ziepe.ca" , "intel-wired-lan@lists.osuosl.org" , "nouveau@lists.freedesktop.org" , "bcm-kernel-feedback-list@broadcom.com" , "dan.carpenter@oracle.com" , "linux-media@vger.kernel.org" , "keescook@chromium.org" , "arnd@arndb.de" , "linux-pm@vger.kernel.org" , "intel-gfx@lists.freedesktop.org" , "bjohannesmeyer@gmail.com" , "linux-block@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "christophe.jaillet@wanadoo.fr" , "jakobkoschel@gmail.com" , "v9fs-developer@lists.sourceforge.net" , "linux-tegra@vger.kernel.org" , "tglx@linutronix.de" , "andriy.shevchenko@linux.intel.com" , "linux-arm-kernel@lists.infradead.org" , "linux-sgx@vger.kernel.org" , "nathan@kernel.org" , "netdev@vger.kernel.org" , "linux-usb@vger.kernel.org" , "linux-wireless@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-f2fs-devel@lists.sourceforge.net" , "tipc-discussion@lists.sourceforge.net" , "linux-crypto@vger.kernel.org" , "dmaengine@vger.kernel.org" , "linux-mediatek@lists.infradead.org" , "akpm@linux-foundation.org" , "linuxppc-dev@lists.ozlabs.org" , "christian.koenig@amd.com" , "rppt@kernel.org" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net From: Xiaomeng Tong > Sent: 03 March 2022 02:27 > > On Wed, 2 Mar 2022 14:04:06 +0000, David Laight > wrote: > > I think that it would be better to make any alternate loop macro > > just set the variable to NULL on the loop exit. > > That is easier to code for and the compiler might be persuaded to > > not redo the test. > > No, that would lead to a NULL dereference. Why, it would make it b ethe same as the 'easy to use': for (item = head; item; item = item->next) { ... if (...) break; ... } if (!item) return; > The problem is the mis-use of iterator outside the loop on exit, and > the iterator will be the HEAD's container_of pointer which pointers > to a type-confused struct. Sidenote: The *mis-use* here refers to > mistakely access to other members of the struct, instead of the > list_head member which acutally is the valid HEAD. The problem is that the HEAD's container_of pointer should never be calculated at all. This is what is fundamentally broken about the current definition. > IOW, you would dereference a (NULL + offset_of_member) address here. Where? > Please remind me if i missed something, thanks. > > Can you share your "alternative definitions" details? thanks! The loop should probably use as extra variable that points to the 'list node' in the next structure. Something like: for (xxx *iter = head->next; iter == &head ? ((item = NULL),0) : ((item = list_item(iter),1)); iter = item->member->next) { ... With a bit of casting you can use 'item' to hold 'iter'. > > > OTOH there may be alternative definitions that can be used to get > > the compiler (or other compiler-like tools) to detect broken code. > > Even if the definition can't possibly generate a working kerrnel. > > The "list_for_each_entry_inside(pos, type, head, member)" way makes > the iterator invisiable outside the loop, and would be catched by > compiler if use-after-loop things happened. It is also a compete PITA for anything doing a search. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales) _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel