Bug 2540 - Adds xstrndup() to xmalloc.h/xmalloc.c in OpenSSH 7.x
Summary: Adds xstrndup() to xmalloc.h/xmalloc.c in OpenSSH 7.x
Status: CLOSED WONTFIX
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: Miscellaneous (show other bugs)
Version: 7.1p1
Hardware: All All
: P5 enhancement
Assignee: Assigned to nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-14 08:30 AEDT by Bill Parker
Modified: 2016-08-02 10:42 AEST (History)
2 users (show)

See Also:


Attachments
patch file for this bug report (425 bytes, application/octet-stream)
2016-02-14 08:30 AEDT, Bill Parker
no flags Details
Patch file for this bug report (404 bytes, patch)
2016-02-14 08:31 AEDT, Bill Parker
wp02855: ok?
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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