From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, RCVD_IN_DNSWL_NONE autolearn=no autolearn_force=no version=3.4.4 Received: (qmail 12299 invoked from network); 18 Dec 2020 09:05:49 -0000 Received: from ewsd.inri.net (107.191.116.128) by inbox.vuxu.org with ESMTPUTF8; 18 Dec 2020 09:05:49 -0000 Received: from smtp26.services.sfr.fr ([93.17.128.214]) by ewsd; Fri Dec 18 04:02:31 -0500 2020 Received: from cauchy.polynum.local (89.121.198.77.rev.sfr.net [77.198.121.89]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by msfrf2638.sfr.fr (SMTP Server) with ESMTPS id 38AC31C002C3F; Fri, 18 Dec 2020 10:02:14 +0100 (CET) X-mail-filterd: 1.0.0 X-sfr-mailing: LEGIT X-sfr-spamrating: 40 X-sfr-spam: not-spam DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=polynum.com; s=202006; t=1608282134; h=Date:From:To:Cc:Subject:References:In-Reply-To; bh=JvfOODZJO W17TR47t9sCCT97ghK9oxfS5T5+QSJovbc=; b=Tz80Q+LSrsaU5ZQw6LPgWijXkPMLM6cGEKObA z10pMBU8eYekB8OL5mlBdqA670Jk8rmq+PlL7GZ9fQvxPvmTDIpmBzLoSe9qaeXvom9Hg4deAXWw /6B21N+2xrE05ez0e+drN89OWOL1sYX7Du7QzALMOQLfQnFw2cJZoDqTDfYvn8RJwXz7dZ2tx8R+ ifKoNFmvCFUAUAPMT+lmpC1SiKnRniPnY7GA/aJcZjrj57GfeR7npeUR5ex4t5gUuLqp5ik/cQ7+ QU+q14nbFFlBgSqCtCTmjaVTRiq1k6g0E80dum21P/h6CmRrZ7aNfLZ8bjv/3i+MpPQsj/uHs89j A==; Received: from cauchy.polynum.local (89.121.198.77.rev.sfr.net [77.198.121.89]) by msfrf2638.sfr.fr (SMTP Server) with ESMTP id F38741C002C14; Fri, 18 Dec 2020 10:02:13 +0100 (CET) Received: from cauchy.polynum.local (89.121.198.77.rev.sfr.net [77.198.121.89]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by msfrf2638.sfr.fr (SMTP Server) with ESMTPS; Fri, 18 Dec 2020 10:02:13 +0100 (CET) Received: from cauchy.polynum.local (localhost [127.0.0.1]) by cauchy.polynum.local (8.15.2/8.15.2) with ESMTPS id 0BI91iaE000698 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 18 Dec 2020 10:01:44 +0100 (CET) Received: (from tlaronde@localhost) by cauchy.polynum.local (8.15.2/8.14.9/Submit) id 0BI91iuS000237; Fri, 18 Dec 2020 10:01:44 +0100 (CET) X-Authentication-Warning: cauchy.polynum.local: tlaronde set sender to thierry.laronde@sfr.fr using -f Date: Fri, 18 Dec 2020 10:01:44 +0100 From: tlaronde@polynum.com To: 9front@9front.org Cc: meta.jxy@gmail.com Message-ID: <20201218090144.GB742@polynum.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Content-Transfer-Encoding: quoted-printable List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: ACPI over HTTP DOM-oriented storage app rails Subject: Re: [9front] strndup should use strnlen and memcpy Reply-To: 9front@9front.org Precedence: bulk On Fri, Dec 18, 2020 at 09:38:00AM +0100, Sigrid Solveig Hafl=C3=ADnud=C3= =B3ttir wrote: > In 9front, memcpy =3D=3D memmove. >=20 > Why is strlen dangerous? Because the terminating '\0' may not be here. But it's simpler to inline: size_t l; for (l =3D 0; l < n && s[l]; l++) ; preceeded by an assertion about the not 'nil'ity of s. FWIW, T. Laronde > Date: Fri, 18 Dec 2020 02:22:25 -0600 > From: Xiao-Yong Jin > Subject: [9front] strndup should use strnlen and memcpy > To: 9front@9front.org > X-Mailer: Apple Mail (2.3654.40.0.2.32) >=20 > I just saw the new commit of strndup that uses strlen and memmove. > memmove is unnecessary. > strlen is dangerous. >=20 > Here is a reference implementation from musl. >=20 > http://git.musl-libc.org/cgit/musl/tree/src/string/strndup.c >=20 > #include > #include >=20 > char *strndup(const char *s, size_t n) > { > size_t l =3D strnlen(s, n); > char *d =3D malloc(l+1); > if (!d) return NULL; > memcpy(d, s, l); > d[l] =3D 0; > return d; > } --=20 Thierry Laronde http://www.kergis.com/ http://kertex.kergis.com/ http://www.sbfa.fr/ Key fingerprint =3D 0FF7 E906 FBAF FE95 FD89 250D 52B1 AE95 6006 F40C