From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) by gnuweeb.org (Postfix) with ESMTPSA id 6E9DC7E323 for ; Mon, 21 Mar 2022 08:17:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1647850627; bh=aRuKNv6OImAv6rU0npvnV+udgUusMFxeqroXXsjRtvM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ktj7zhbZv2g1vsGMBDNOdMYLaOD1Vd3XL801A+afLd/uXm/2xBBn1csbfbW3c2f/E gkdITSA4cdsjUs14QVvZRQT+Dpp11+jGCK8LSeyoRBlOCrZDN4P8bl5q06gtkjbtkT Z58gVIsr+byZ4V6YOhDMjbKd0RhFTPbZsLc85q2mNU2J4rojy4f6iucZVbxPICV+xX fX2h5wnCSGw/khdp092Sey0NxyjnQZpbnn1iYn6fem/bAxhmFTwviAEfJSC9ESCbUG OM7q77ln8TbowaYDR2mPE2WAyfds31/lKEN9IfNofki6tyvlj+TwH2dKSrsUu37IxL Id/rsBCOkBceA== Received: by mail-lj1-f170.google.com with SMTP id h11so18743354ljb.2 for ; Mon, 21 Mar 2022 01:17:07 -0700 (PDT) X-Gm-Message-State: AOAM5335gc3ogX+02Uxjy+Lp++rr67xsL1s30YBZ/x4IKjyVhIttGDp9 M8LhIZqqDrw2r5ZCAUn4j+z1SfKRPVlrBab2VV8= X-Google-Smtp-Source: ABdhPJw8JJLln1bH9vT0H8vH/0meIvWmGJy1t2F2kpKKJa9tzHDAbYlqgKYMoh3cJZKrOldl2Y0UGoeeks4nlypoNrY= X-Received: by 2002:a2e:3013:0:b0:247:ea0d:11e2 with SMTP id w19-20020a2e3013000000b00247ea0d11e2mr14639310ljw.2.1647850625490; Mon, 21 Mar 2022 01:17:05 -0700 (PDT) MIME-Version: 1.0 References: <20220320093750.159991-1-ammarfaizi2@gnuweeb.org> <20220320093750.159991-7-ammarfaizi2@gnuweeb.org> <20220321075308.GD29580@1wt.eu> In-Reply-To: <20220321075308.GD29580@1wt.eu> From: Alviro Iskandar Setiawan Date: Mon, 21 Mar 2022 15:16:54 +0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH v1 6/6] tools/include/string: Implement `strdup()` and `strndup()` To: Willy Tarreau Cc: Ammar Faizi , "Paul E. McKenney" , Nugraha , Linux Kernel Mailing List , "GNU/Weeb Mailing List" Content-Type: text/plain; charset="UTF-8" List-Id: On Mon, Mar 21, 2022 at 2:53 PM Willy Tarreau wrote: > 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; Maybe better to use strnlen(), see the detail at man 3 strnlen. size_t strnlen(const char *s, size_t maxlen); The strnlen() function returns the number of bytes in the string pointed to by s, excluding the terminating null byte ('\0'), but at most maxlen. In doing this, strnlen() looks only at the first maxlen characters in the string pointed to by s and never beyond s[maxlen-1]. Should be trivial to add strnlen() with a separate patch before this patch. So it can be: size_t len; char *ret; len = strnlen(str, maxlen); ret = malloc(len + 1); if (__builtin_expect(ret != NULL, 1)) { memcpy(ret, str, len); ret[len] = '\0'; } return ret; Thoughts? -- Viro