]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libiberty/strndup.c
c++/modules: Stream unmergeable temporaries by value again [PR114856]
[thirdparty/gcc.git] / libiberty / strndup.c
index b7647777bc5987680765cec14470b093ae51571f..283e5d756a59f1f50ef63153c06f59d18bc2f7e3 100644 (file)
@@ -1,5 +1,5 @@
 /* Implement the strndup function.
-   Copyright (C) 2005 Free Software Foundation, Inc.
+   Copyright (C) 2005-2024 Free Software Foundation, Inc.
    Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
 
 This file is part of the libiberty library.
@@ -15,8 +15,8 @@ Library General Public License for more details.
 
 You should have received a copy of the GNU Library General Public
 License along with libiberty; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /*
 
@@ -33,23 +33,20 @@ memory was available.  The result is always NUL terminated.
 #include "ansidecl.h"
 #include <stddef.h>
 
-extern size_t  strlen (const char*);
-extern PTR     malloc (size_t);
-extern PTR     memcpy (PTR, const PTR, size_t);
+extern size_t  strnlen (const char *s, size_t maxlen);
+extern void *malloc (size_t);
+extern void *memcpy (void *, const void *, size_t);
 
 char *
 strndup (const char *s, size_t n)
 {
   char *result;
-  size_t len = strlen (s);
+  size_t len = strnlen (s, n);
 
-  if (n < len)
-    len = n;
-
-  result = malloc (len + 1);
+  result = (char *) malloc (len + 1);
   if (!result)
     return 0;
 
   result[len] = '\0';
-  return memcpy (result, s, len);
+  return (char *) memcpy (result, s, len);
 }