Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2261868pxp; Mon, 21 Mar 2022 15:15:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdjz4M1c/VD1z97INR6BN8OYXzJq7RpuNqppNcI22nZ1oFtK7Al5IE4Dn4UY9fE4dF6skr X-Received: by 2002:a63:445f:0:b0:381:6cef:d841 with SMTP id t31-20020a63445f000000b003816cefd841mr19869396pgk.363.1647900909528; Mon, 21 Mar 2022 15:15:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647900909; cv=none; d=google.com; s=arc-20160816; b=KkWMZPF3GrYPH8N2gqSyHsvEGjwU9U/F6Zc6pzIz0/lAZ5Sdj0b2yRxdoXmwHltG7j WEyeN7MlH3Le3JeX9pV3B6eAamIvgZZ4zSKIz6gDyIUQ2WbNP3eyB3c4zyHV0V159vOg A2EV6l1P1z5OxUSC71RWhmoeaDjU3Di2c/BS8hzhpRoSzmyLTnjaiAWLrYQtmvBGDGMy DFhWuHtO4pLWMLk2c3EMdnsM09iT5d1mjKh69oEnpsKDsQjbWyiYSXDygPMO5Ts7ePKK Y7YjqATLcSpsGBsOq2gueSDsZZez8LM1bbCCMD0b1jiJDRbk0MTP5WbMzqFT/S1jsl1G B0Yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=caqjT5qYQVybY9daK7Zq8mmlzmk3TDcqH5WlcX1Fmw4=; b=0sYQhOpovRpCN2KA6y0oukaw14yobHK/bfzk2rLWFE3c6OfP3W1b6vid//CCbjG2nb HgcNJTX6CMaS4/ZFJBpMABIBZKDTrL0/oyaNiHb03C2tkpr6aXUO5c7YS3j/87PEL20R gq8hzw08qkWEB57fhKd8ZjgHSIVs04B1K4nKDUjiVGKeWc9U4MNDN6EjOVr3c5HpocHx 7kxrWOjfrb7576+lQsYCKgD42d4XPudo2ZxbG0ys7S74kdY4Sf5iZuyeLbtmoeiKq1y/ GrO3kplxVCIB+xRBEjC0+d9RXwpnvW8ZPxWhNp1TgxwtuM8h/zOqgaFY+rBJjaPcQU4T hn9A== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id y16-20020a056a00191000b004fa3f687674si10550664pfi.256.2022.03.21.15.15.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 15:15:09 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 64A18241B5B; Mon, 21 Mar 2022 14:33:34 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345031AbiCUHyx (ORCPT + 99 others); Mon, 21 Mar 2022 03:54:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236143AbiCUHyt (ORCPT ); Mon, 21 Mar 2022 03:54:49 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EE4E7140B0 for ; Mon, 21 Mar 2022 00:53:21 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 22L7r8hC005539; Mon, 21 Mar 2022 08:53:08 +0100 Date: Mon, 21 Mar 2022 08:53:08 +0100 From: Willy Tarreau To: Ammar Faizi Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Nugraha , Linux Kernel Mailing List , GNU/Weeb Mailing List Subject: Re: [RFC PATCH v1 6/6] tools/include/string: Implement `strdup()` and `strndup()` Message-ID: <20220321075308.GD29580@1wt.eu> References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> <20220320093750.159991-7-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220320093750.159991-7-ammarfaizi2@gnuweeb.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Ammar, On Sun, Mar 20, 2022 at 04:37:50PM +0700, Ammar Faizi wrote: > Add strdup and strndup support. These functions are only available on > architectures that have my_syscall6() macro from nolibc. > > Signed-off-by: Ammar Faizi > --- > tools/include/nolibc/string.h | 68 +++++++++++++++++++++++++++++++++++ > 1 file changed, 68 insertions(+) > > diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h > index 4554b6fcb400..413c65f7c853 100644 > --- a/tools/include/nolibc/string.h > +++ b/tools/include/nolibc/string.h > @@ -9,6 +9,10 @@ > > #include "std.h" > > +static void free(void *ptr); > +static void *malloc(size_t len); > +static void *realloc(void *old_ptr, size_t new_size); Better include the required h files here. > /* > * As much as possible, please keep functions alphabetically sorted. > */ > @@ -127,6 +131,70 @@ size_t nolibc_strlen(const char *str) > nolibc_strlen((str)); \ > }) > > +static __attribute__((unused)) > +char *strdup(const char *str) > +{ > + size_t allocated = 2048; > + size_t i; > + char *ret; > + char *tmp; > + > + ret = malloc(allocated); > + if (__builtin_expect(!ret, 0)) > + return NULL; > + > + i = 0; > + for (;;) { > + char c = *str; > + if (!c) > + break; > + > + if (i == allocated) { > + allocated += 2048; > + tmp = realloc(ret, allocated); > + if (__builtin_expect(!tmp, 0)) { > + free(ret); > + return NULL; > + } > + ret = tmp; > + } > + > + ret[i++] = c; > + str++; > + } > + > + ret[i] = '\0'; > + return ret; > +} This version is suboptimal in terms of code size, CPU usage and memory usage. And it even seems it contains a buffer overflow: if the string is exactly a multiple of 2048, it seems to me that you'll write the trailing zero past the end. Please instead use the more intuitive form below (not tested but you get the idea): size_t len = strlen(str); char *ret = malloc(len + 1); if (ret) memcpy(ret, str, len); return ret; > +static __attribute__((unused)) > +char *strndup(const char *str, size_t maxlen) > +{ > + size_t i; > + char *ret; > + > + ret = malloc(maxlen + 1); > + if (__builtin_expect(!ret, 0)) > + return NULL; > + > + i = 0; > + for (;;) { > + char c = *str; > + if (!c) > + break; > + > + if (i == maxlen) > + break; > + > + ret[i++] = c; > + str++; > + } > + > + ret[i] = '\0'; > + return ret; > +} Here it can cost quite a lot for large values of maxlen. Please just use a variant of the proposal above like this one: size_t len; char *ret; len = strlen(str); if (len > maxlen) len = maxlen; ret = malloc(len + 1); if (ret) memcpy(ret, str, len); return ret; Thanks, Willy