9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] strndup should use strnlen and memcpy
@ 2020-12-18  8:22 Xiao-Yong Jin
  2020-12-18  8:38 ` Sigrid Solveig Haflínudóttir
  2020-12-18 15:27 ` ori
  0 siblings, 2 replies; 19+ messages in thread
From: Xiao-Yong Jin @ 2020-12-18  8:22 UTC (permalink / raw)
  To: 9front

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;
}


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2020-12-18 15:37 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-18  8:22 [9front] strndup should use strnlen and memcpy Xiao-Yong Jin
2020-12-18  8:38 ` Sigrid Solveig Haflínudóttir
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

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).