9front - general discussion about 9front
 help / color / mirror / Atom feed
From: "Sigrid Solveig Haflínudóttir" <ftrvxmtrx@gmail.com>
To: meta.jxy@gmail.com, 9front@9front.org
Subject: Re: [9front] strndup should use strnlen and memcpy
Date: Fri, 18 Dec 2020 09:38:00 +0100	[thread overview]
Message-ID: <D25C33461CDCAB236440E6BC767C9D97@gmail.com> (raw)
In-Reply-To: <CEDEBD0B-FC39-43D4-960F-05E348970FE8@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 56 bytes --]

In 9front, memcpy == memmove.

Why is strlen dangerous?

[-- Attachment #2: Type: message/rfc822, Size: 5829 bytes --]

From: Xiao-Yong Jin <meta.jxy@gmail.com>
To: 9front@9front.org
Subject: [9front] strndup should use strnlen and memcpy
Date: Fri, 18 Dec 2020 02:22:25 -0600
Message-ID: <CEDEBD0B-FC39-43D4-960F-05E348970FE8@gmail.com>

I just saw the new commit of strndup that uses strlen and memmove.
memmove is unnecessary.
strlen is dangerous.

Here is a reference implementation from musl.

http://git.musl-libc.org/cgit/musl/tree/src/string/strndup.c

#include <stdlib.h>
#include <string.h>

char *strndup(const char *s, size_t n)
{
	size_t l = strnlen(s, n);
	char *d = malloc(l+1);
	if (!d) return NULL;
	memcpy(d, s, l);
	d[l] = 0;
	return d;
}

  reply	other threads:[~2020-12-18  8:41 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-18  8:22 Xiao-Yong Jin
2020-12-18  8:38 ` Sigrid Solveig Haflínudóttir [this message]
2020-12-18  9:01   ` tlaronde
2020-12-18  9:59     ` Sigrid Solveig Haflínudóttir
2020-12-18 10:21       ` Kemal
2020-12-18 10:31         ` Sigrid Solveig Haflínudóttir
2020-12-18 10:48           ` Kemal
2020-12-18 11:02             ` Eli Cohen
2020-12-18 11:05               ` Eli Cohen
2020-12-18 11:29           ` tlaronde
2020-12-18 11:37             ` Sigrid Solveig Haflínudóttir
2020-12-18 11:43               ` Eli Cohen
2020-12-18 11:50               ` Kemal
2020-12-18 11:55                 ` Eli Cohen
2020-12-18 12:06                   ` Kemal
2020-12-18 13:22                   ` José Miguel Sánchez García
2020-12-18 14:34           ` Devon H. O'Dell
2020-12-18 14:26       ` Devon H. O'Dell
2020-12-18 15:27 ` ori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=D25C33461CDCAB236440E6BC767C9D97@gmail.com \
    --to=ftrvxmtrx@gmail.com \
    --cc=9front@9front.org \
    --cc=meta.jxy@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).