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.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 18003 invoked from network); 18 Dec 2020 14:34:05 -0000 Received: from ewsd.inri.net (107.191.116.128) by inbox.vuxu.org with ESMTPUTF8; 18 Dec 2020 14:34:05 -0000 Received: from mail-ot1-f53.google.com ([209.85.210.53]) by ewsd; Fri Dec 18 09:26:38 -0500 2020 Received: by mail-ot1-f53.google.com with SMTP id i6so2060067otr.2 for <9front@9front.org>; Fri, 18 Dec 2020 06:26:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=SCDAP1+1A77d7sA8vgVgyjr70lA6auYCXX/d87JFMts=; b=rBSRK5zAyMHwcS1cK+Pk4cnUxcrjSmzEyAwa/8iJ2unA3ncP2zu2TPYDaPlUoU0W0B cr95vtt58cKbBYZwD/QkzaltXfC7+HahlCQfWLbsK9ihHpk4eZlFeS1UTOd0UxkLIyYR 87UI7rU6FuVHqXGcJuZxcK6RjF+O9I/KzZY4fY9+YQWk4yvIIVH6CSRYwH4LdzyBWD4h McqBDTyIXeG3E839Sc3OuCP6KlCSbeKUBdE26GgnljI/MbX99F1wvrLtMtNAJcuBjwv6 TylSuSrIbHkpbOAAk0/jjbZWAAVcmgXRF1gFt6jBrO7HbQTuSTOu0JCIZELUGPE91cT4 XMPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=SCDAP1+1A77d7sA8vgVgyjr70lA6auYCXX/d87JFMts=; b=SXjOig3oZBlzu9cEENBm1C7s7VLf+idGD1YsmrgznfOqqb36/Nhl7gT8cjK/MxC7nA 6BWh2tVUJtv+97i0fFQ/5ccPwU2VO/jnZHhIwOaPdybReh1CHIdva2ZIzaFk5dppyg09 9leimP7LWiMfc8Gs4JIpnGqQvewx4oc+c8PC+HaS204aRId0mrPPin8Ts/cmdc/I0JFN A5+6RpWb7/XHBKOEnK596JvXV2wmZEcjbg89p343fwxmFDTqlz0eUiuyO+A6PWfiiAoR HDLqTMGUJjAf/Z/3sH8d19AhkL2cJincgfUkRUCA18VLsPNDV/soMesh+9szA4j6zInB n0Yg== X-Gm-Message-State: AOAM533q/O6btruZmoyiAuX0eRhznl5H2RmulGeiAwrKp9gl81Y92Ur5 iQQjN2L2jeU+TY3nfBSZwhttYyK9mhtIMSojJThl+Ofw+uDaBxZO X-Google-Smtp-Source: ABdhPJwekDQ6zYUgjJSH1RRmDh+JVwCC0TokxAvIHNRkiFvsnY6Ofy3Fkz2cmSSJr/faMmwmzak2hvo+JBzi2Bl3vXA= X-Received: by 2002:a05:6830:1501:: with SMTP id k1mr3081910otp.12.1608301589349; Fri, 18 Dec 2020 06:26:29 -0800 (PST) MIME-Version: 1.0 References: <20201218090144.GB742@polynum.com> In-Reply-To: From: "Devon H. O'Dell" Date: Fri, 18 Dec 2020 06:26:18 -0800 Message-ID: To: 9front@9front.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: stateless realtime storage-oriented just-in-time storage-based database Subject: Re: [9front] strndup should use strnlen and memcpy Reply-To: 9front@9front.org Precedence: bulk Hi Sigrid, The C2x working draft[1] incorporates strdup and strndup. Of strndup, it says, "[t]he strndup function creates a string initialized with no more than size initial characters of the *array pointed to by s*..." (emphasis mine). Strings in the standard library are always called strings. While any object occupying fewer than `size+1` bytes must be a string, this is a side-effect of how the API is specified to behave. POSIX is ambiguous, but implies that it expects a string either way: "[t]he strndup() function shall be equivalent to the strdup() function...", without subsequently placing exceptions on the input constraints. Many (most?) implementations don't bother to check (or explicitly allow non-strings), and WG14 likely recognized this when standardizing the functionality in libc. Anyway, if taking the "find length before allocation" approach to implementation, the WG14 wording requires use of strnlen semantics. One can implement without a strnlen by always allocating size+1 bytes, which is why the standard can put strnlen_s in an annex. --dho [1]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2454.pdf On Fri, Dec 18, 2020 at 2:01 AM Sigrid Solveig Hafl=C3=ADnud=C3=B3ttir wrote: > > > Because the terminating '\0' may not be here. > > Why wouldn't it be there? Isn't strndup supposed to work on strings? > Is there any (trustworthy standard-like) source that mentions > non-terminated strings as something that should work with strndup? >