Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp583546rdb; Sun, 18 Feb 2024 02:21:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXmyL0YA4N4UuZZJM31JtRwASxR1VXNB37APNjmrT+qTOTrCdAwlLY82f4synBZhnFsQuYf8HOvxIFPQ2hdEy1c9AGwnUwmiQaAMuXV6g== X-Google-Smtp-Source: AGHT+IHH93s9KeT+JBdm+9GMwy2wjrylOItT+DViQkAlsYarcZW5fxcHnPZT5OXhDb3GBIRW5EEe X-Received: by 2002:a17:902:7ed0:b0:1d9:a4bb:29f2 with SMTP id p16-20020a1709027ed000b001d9a4bb29f2mr7302833plb.46.1708251676302; Sun, 18 Feb 2024 02:21:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708251676; cv=pass; d=google.com; s=arc-20160816; b=uulDsKEBLjz62dV+/2nVAQ4M3tR03KOL4tK1MlyON0WEB7vw7LscwPtOKq7R6SLS1d 7wpujpIeTSWV1Mt9plAA4CQQXTj2+wstLQ7HP9Ai+bBaBBvUjok651n7Fcfw/0j2xtec hDsl5GEMr4BJjaUqSD4Aq/VDMXo87KIPFYXe6t4ItPSSTSAgVW1B+CvxCMHEIl7iWDl+ wFP5HKkUqwQt2X/B9/2e7Xu2bU+jlGlzjdEuFTS/TQwn+RTDPv6swWOPbk9eacA/bsCh 2eP28Vz40cZ6Qm6h5MxHp09JASMluIbhfEtPXyRr6aujPnk2hwkECMOPhDtri5yvhEeW 7tiw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date; bh=xT1Tmi35ODDbYXwrJAeB+FkJq1nvI3me4aSsJGXTr0Q=; fh=E9+ko7sPlyxPjcJvXPlA8k/l1NtMODP18pGC6Tm/ghk=; b=PUG0rta0BhklIAYl2QEwzOcK7/u1y+EAJ6ECzm8bf3uQ+iSSO/YRwZmtJ/rGWZvBCz /q19Ru94UHT7Wpd0Ykdj8VxsFVawfVINJ2OeZX1NWbgeBdGqFHnyLGTZmY6PCoWBi1kx /xKiy91borgOAD4XV/n7Q31xPfia0a/o+nBdGhQR3N86Px+CZLlWyex4RPtnZqP876fp NhThAorpEwBEybOt4hxbpTGO4tsNvjmLHokCB30XC0mlXd1CqWDIAyKhZVBeOjypzPic ke72Nhvq7/DMuUM4JTme+cELybUw4Hs0K1jR861TEDK2yPCMD0wN6z5p67kwZ9VWYqfa iA9A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=1wt.eu); spf=pass (google.com: domain of linux-kernel+bounces-70331-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70331-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i16-20020a170902cf1000b001db4210203fsi2769403plg.122.2024.02.18.02.21.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 02:21:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70331-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=1wt.eu); spf=pass (google.com: domain of linux-kernel+bounces-70331-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70331-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D017A283465 for ; Sun, 18 Feb 2024 10:20:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 85E272E401; Sun, 18 Feb 2024 10:20:19 +0000 (UTC) Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8B2512DF84 for ; Sun, 18 Feb 2024 10:20:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=163.172.96.212 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708251619; cv=none; b=R0SLDF+hA+tIYr/7MeBaW8520Vj/cwKAfj3FiERewx0NWMCeS31aha9oIOoiJjKUCCEU6hn9Cnr/cLhCTksn5tzMjWuEG1FjRkNlsYWTjHG2czlJzwHHrIlAu/f6QgY8kjkZRqdQprMRxEYh+NgoewAquvR+a6H4BwpJldWFE20= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708251619; c=relaxed/simple; bh=ZtWVjcSYaslclUGRlichPSHeNhxbe+UlnZ7ja0YIWBM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=EIhFBS9LxADufLcucjvQNOvlWGTwn9Hp50RUpZmDx1b4PbJuSmJz5fbw6zGoMLKSCXnQH2NonQkmNIhVWxuK85UPG2GXcV6Ze8kO8me7U/hmZfwXX0AMqATMxw6Pq6jY+aK+qLiaabCQramJoRdfReLyvjZb3CDK0p6diDteZ+s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=1wt.eu; spf=pass smtp.mailfrom=1wt.eu; arc=none smtp.client-ip=163.172.96.212 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=1wt.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=1wt.eu Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 41IAK3hM032418; Sun, 18 Feb 2024 11:20:03 +0100 Date: Sun, 18 Feb 2024 11:20:03 +0100 From: Willy Tarreau To: Rodrigo Campos Cc: Thomas =?iso-8859-1?Q?Wei=DFschuh?= , linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/4] tools/nolibc: Fix strlcat() return code and size usage Message-ID: <20240218102003.GA32375@1wt.eu> References: <20240129141516.198636-1-rodrigo@sdfg.com.ar> <20240129141516.198636-3-rodrigo@sdfg.com.ar> <20240211104817.GA19364@1wt.eu> <10b97cd3-5690-40b2-aa8e-3fea5dd4275f@sdfg.com.ar> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <10b97cd3-5690-40b2-aa8e-3fea5dd4275f@sdfg.com.ar> User-Agent: Mutt/1.10.1 (2018-07-13) Hi Rodrigo, On Wed, Feb 14, 2024 at 12:34:46PM -0300, Rodrigo Campos wrote: > Here are two versions that are significantly shorter than the 101 bytes, > that pass the tests (I added more to account for the src vs dst mismatch > that was easy to pass tests when both buffers have the same size as they did > before). > > size_t strlcat_rata(char *dst, const char *src, size_t size) > { > const char *orig_src = src; > size_t len = 0; > for (;len < size; len++) { > if (dst[len] == '\0') > break; > } > > /* Let's copy len < n < size-1 times from src. > * size is unsigned, so instead of size-1, that can wrap around, > * let's use len + 1 */ > while (len + 1 < size) { > dst[len] = *src; > if (*src == '\0') > break; > len++; > src++; > } > > if (src != orig_src) > dst[len] = '\0'; > > while (*src++) > len++; > > return len; > } > > This one compiles to 81 bytes here using gcc 13.2.0 and to 83 using gcc > 9.5.0. Compared to the one posted in the patchset, it is significantly > smaller. OK this looks good to me. I think your test on src != orig_src is not trivial and that testing instead if (len < size) would be better, and possibly even shorter. > One based in the version you posted (uses strlen(dst) instead), is this one: > > size_t strlcat_willy_fixed(char *dst, const char *src, size_t size) > { > const char *orig_src = src; > size_t len = strlen(dst); > if (size < len) > len = size; > > for (;len + 1 < size; len++, src++) { > dst[len] = *src; > if (*src == '\0') > break; > } > > if (orig_src != src) > dst[len] = '\0'; > > while (*src++) > len++; > > return len; > } > > > Note the "if size < len, then len=size", I couldn't get rid of it because we > need to account for the smaller size of dst if we don't get passed it for > the return code. Please no, again as I mentioned earlier, it's wrong to use strlen(dst) in this case: the only situation where we'd accept size < len is if dst is already not zero-terminated, which means that strlen() cannot be used, or you'd need strnlen() for that, I'm just seeing that we have it, I thought we didn't. > This one is 90 bytes here. See the point I was making? Sometimes the amount of fat added around a function call is important compared to just an increment and a comparison. Of course that's not always true but that's one such example. > What do you think? Can you make them shorter? I don't want to enter that activity again this week-end ;-) It's sufficient here. > If you like one of these, I can repost with the improved tests too. Yeah please check the few points above for your version, make sure to clean it up according to the kernel's coding style (empty line after variable declaration, */ on the next line for the multi-line comment etc) and that's fine. Thanks, Willy