summaryrefslogtreecommitdiff
path: root/crypto/compat/bsd-asprintf.c
diff options
context:
space:
mode:
authorBrent Cook <bcook@openbsd.org>2014-07-30 06:53:02 -0500
committerBrent Cook <bcook@openbsd.org>2014-07-30 06:53:02 -0500
commitb9ff0728e1bab018dd888c173151dba35e40ef3b (patch)
treee140e9287ca6f3a261d5e9fe036e7675846f8edd /crypto/compat/bsd-asprintf.c
parenta07e33702049d63adb06271e4d15412685b44085 (diff)
downloadportable-b9ff0728e1bab018dd888c173151dba35e40ef3b.tar.gz
portable-b9ff0728e1bab018dd888c173151dba35e40ef3b.tar.bz2
portable-b9ff0728e1bab018dd888c173151dba35e40ef3b.zip
harmonize asprintf with OpenSSH
* use the original name for the file from OpenSSH (remove duplicate version) * add va_copy/__va_copy checks to configure * incorporate proposed fixes to openssh version: + include more system headers directly for various definitions + limit the scope of va_copy/va_end to their affected vsnprintf calls + simplify error handling, removing a dead assignment
Diffstat (limited to 'crypto/compat/bsd-asprintf.c')
-rw-r--r--crypto/compat/bsd-asprintf.c29
1 files changed, 13 insertions, 16 deletions
diff --git a/crypto/compat/bsd-asprintf.c b/crypto/compat/bsd-asprintf.c
index 3368195..8ccfa22 100644
--- a/crypto/compat/bsd-asprintf.c
+++ b/crypto/compat/bsd-asprintf.c
@@ -17,12 +17,12 @@
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */ 18 */
19 19
20#include "includes.h"
21
22#ifndef HAVE_VASPRINTF 20#ifndef HAVE_VASPRINTF
23 21
24#include <errno.h> 22#include <errno.h>
23#include <limits.h> /* for INT_MAX */
25#include <stdarg.h> 24#include <stdarg.h>
25#include <stdio.h> /* for vsnprintf */
26#include <stdlib.h> 26#include <stdlib.h>
27 27
28#ifndef VA_COPY 28#ifndef VA_COPY
@@ -42,16 +42,17 @@
42int 42int
43vasprintf(char **str, const char *fmt, va_list ap) 43vasprintf(char **str, const char *fmt, va_list ap)
44{ 44{
45 int ret = -1; 45 int ret;
46 va_list ap2; 46 va_list ap2;
47 char *string, *newstr; 47 char *string, *newstr;
48 size_t len; 48 size_t len;
49 49
50 VA_COPY(ap2, ap);
51 if ((string = malloc(INIT_SZ)) == NULL) 50 if ((string = malloc(INIT_SZ)) == NULL)
52 goto fail; 51 goto fail;
53 52
53 VA_COPY(ap2, ap);
54 ret = vsnprintf(string, INIT_SZ, fmt, ap2); 54 ret = vsnprintf(string, INIT_SZ, fmt, ap2);
55 va_end(ap2);
55 if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */ 56 if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
56 *str = string; 57 *str = string;
57 } else if (ret == INT_MAX || ret < 0) { /* Bad length */ 58 } else if (ret == INT_MAX || ret < 0) { /* Bad length */
@@ -62,25 +63,21 @@ vasprintf(char **str, const char *fmt, va_list ap)
62 if ((newstr = realloc(string, len)) == NULL) { 63 if ((newstr = realloc(string, len)) == NULL) {
63 free(string); 64 free(string);
64 goto fail; 65 goto fail;
65 } else {
66 va_end(ap2);
67 VA_COPY(ap2, ap);
68 ret = vsnprintf(newstr, len, fmt, ap2);
69 if (ret >= 0 && (size_t)ret < len) {
70 *str = newstr;
71 } else { /* failed with realloc'ed string, give up */
72 free(newstr);
73 goto fail;
74 }
75 } 66 }
67 VA_COPY(ap2, ap);
68 ret = vsnprintf(newstr, len, fmt, ap2);
69 va_end(ap2);
70 if (ret < 0 || (size_t)ret >= len) { /* failed with realloc'ed string */
71 free(newstr);
72 goto fail;
73 }
74 *str = newstr;
76 } 75 }
77 va_end(ap2);
78 return (ret); 76 return (ret);
79 77
80fail: 78fail:
81 *str = NULL; 79 *str = NULL;
82 errno = ENOMEM; 80 errno = ENOMEM;
83 va_end(ap2);
84 return (-1); 81 return (-1);
85} 82}
86#endif 83#endif