Bug 2540

Summary: Adds xstrndup() to xmalloc.h/xmalloc.c in OpenSSH 7.x
Product: Portable OpenSSH Reporter: Bill Parker <wp02855>
Component: MiscellaneousAssignee: Assigned to nobody <unassigned-bugs>
Status: CLOSED WONTFIX    
Severity: enhancement CC: dtucker, wp02855
Priority: P5    
Version: 7.1p1   
Hardware: All   
OS: All   
Attachments:
Description Flags
patch file for this bug report
none
Patch file for this bug report wp02855: ok?

Description Bill Parker 2016-02-14 08:30:39 AEDT
Created attachment 2785 [details]
patch file for this bug report

Hello All,

	The code below implements the C function 'strndup()' in xmalloc.h
and xmalloc.c (for future use, if needed):

--- xmalloc.h.orig      2016-02-13 09:15:50.418982399 -0800
+++ xmalloc.h   2016-02-13 09:16:24.760347010 -0800
@@ -20,6 +20,7 @@
 void   *xcalloc(size_t, size_t);
 void   *xreallocarray(void *, size_t, size_t);
 char   *xstrdup(const char *);
+char   *xstrndup(const char *, size_t);
 int     xasprintf(char **, const char *, ...)
                 __attribute__((__format__ (printf, 2, 3)))
                 __attribute__((__nonnull__ (2)));
				 
=======================================================================

--- xmalloc.c.orig      2016-02-13 09:33:10.108121542 -0800
+++ xmalloc.c   2016-02-13 09:34:07.203378056 -0800
@@ -79,6 +79,20 @@
        return cp;
 }
 
+char *
+xstrndup(const char *str, size_t dst_size)
+{
+       char *cp;
+       size_t len;
+
+       len = strlen(str) + 1;
+       if (len > size)
+               len = size + 1;
+       cp = xmalloc(len);
+       strlcpy(cp, str, len);
+       return cp;
+}
+
 int
 xasprintf(char **ret, const char *fmt, ...)
 {
 
 ======================================================================
 
 I am attaching the patch file(s) to this report...
 
 Bill Parker (wp02855 at gmail dot com)
Comment 1 Bill Parker 2016-02-14 08:31:28 AEDT
Created attachment 2786 [details]
Patch file for this bug report
Comment 2 Darren Tucker 2016-02-15 09:15:54 AEDT
(In reply to Bill Parker from comment #0)
> for future use, if needed):

Thanks, but I think we should wait until we have an actual need before we add something.
Comment 3 Darren Tucker 2016-02-15 10:21:09 AEDT
To elaborate on my earlier comment: if you'd like to do something like this then you should be able to demonstrate that it's worth doing.

For example, by showing that factoring out this code simplifies or shortens existing code significantly, or it fixes a bug in existing code (eg by handling a corner case that the inline code doesn't).
Comment 4 Damien Miller 2016-08-02 10:42:06 AEST
Close all resolved bugs after 7.3p1 release