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)
Created attachment 2786 [details] Patch file for this bug report
(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.
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).
Close all resolved bugs after 7.3p1 release