NetBSD Problem Report #52778
From www@NetBSD.org Sun Dec 3 08:28:59 2017
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
by mollari.NetBSD.org (Postfix) with ESMTPS id 7C65A7A1C8
for <gnats-bugs@gnats.NetBSD.org>; Sun, 3 Dec 2017 08:28:59 +0000 (UTC)
Message-Id: <20171203082858.64F457A200@mollari.NetBSD.org>
Date: Sun, 3 Dec 2017 08:28:58 +0000 (UTC)
From: daniel+netbsd@loffgren.org
Reply-To: daniel+netbsd@loffgren.org
To: gnats-bugs@NetBSD.org
Subject: strndup contains a reimplementation of strnlen
X-Send-Pr-Version: www-1.0
>Number: 52778
>Category: lib
>Synopsis: strndup contains a reimplementation of strnlen
>Confidential: no
>Severity: non-critical
>Priority: medium
>Responsible: lib-bug-people
>State: open
>Class: change-request
>Submitter-Id: net
>Arrival-Date: Sun Dec 03 08:30:00 +0000 2017
>Originator: Daniel Loffgren
>Release: HEAD
>Organization:
>Environment:
NetBSD kudo 8.99.7 NetBSD 8.99.7 (GENERIC) #0: Thu Nov 30 21:39:35 UTC 2017 mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
>Description:
While looking through the code of strndup, I noticed that the beginning of it contains a reimplementation of what strnlen already does. Shouldn't it defer to the function whose job is to do exactly that, ensuring that any optimizations made to strnlen benefit both?
>How-To-Repeat:
>Fix:
Index: lib/libc/string/strndup.c
===================================================================
RCS file: /cvsroot/src/lib/libc/string/strndup.c,v
retrieving revision 1.4
diff -u -r1.4 strndup.c
--- lib/libc/string/strndup.c 3 Jul 2007 12:11:09 -0000 1.4
+++ lib/libc/string/strndup.c 15 Oct 2017 19:03:45 -0000
@@ -62,8 +62,7 @@
_DIAGASSERT(str != NULL);
- for (len = 0; len < n && str[len]; len++)
- continue;
+ len = strnlen(str, n);
if (!(copy = malloc(len + 1)))
return (NULL);
(Contact us)
$NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2014
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.