diff options
author | Brent Cook <bcook@openbsd.org> | 2014-07-30 06:53:02 -0500 |
---|---|---|
committer | Brent Cook <bcook@openbsd.org> | 2014-07-30 06:53:02 -0500 |
commit | b9ff0728e1bab018dd888c173151dba35e40ef3b (patch) | |
tree | e140e9287ca6f3a261d5e9fe036e7675846f8edd | |
parent | a07e33702049d63adb06271e4d15412685b44085 (diff) | |
download | portable-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
-rw-r--r-- | configure.ac.tpl | 25 | ||||
-rw-r--r-- | crypto/Makefile.am.tpl | 2 | ||||
-rw-r--r-- | crypto/compat/asprintf.c | 94 | ||||
-rw-r--r-- | crypto/compat/bsd-asprintf.c | 29 |
4 files changed, 39 insertions, 111 deletions
diff --git a/configure.ac.tpl b/configure.ac.tpl index de3995c..b557687 100644 --- a/configure.ac.tpl +++ b/configure.ac.tpl | |||
@@ -117,6 +117,31 @@ AC_CHECK_FUNC(explicit_bzero, | |||
117 | AC_DEFINE(NO_EXPLICIT_BZERO) | 117 | AC_DEFINE(NO_EXPLICIT_BZERO) |
118 | AM_CONDITIONAL(NO_EXPLICIT_BZERO, true)) | 118 | AM_CONDITIONAL(NO_EXPLICIT_BZERO, true)) |
119 | 119 | ||
120 | AC_CACHE_CHECK([whether va_copy exists], ac_cv_have_va_copy, [ | ||
121 | AC_LINK_IFELSE([AC_LANG_PROGRAM([[ | ||
122 | #include <stdarg.h> | ||
123 | va_list x,y; | ||
124 | ]], [[ va_copy(x,y); ]])], | ||
125 | [ ac_cv_have_va_copy="yes" ], | ||
126 | [ ac_cv_have_va_copy="no" | ||
127 | ]) | ||
128 | ]) | ||
129 | if test "x$ac_cv_have_va_copy" = "xyes" ; then | ||
130 | AC_DEFINE([HAVE_VA_COPY], [1], [Define if va_copy exists]) | ||
131 | fi | ||
132 | |||
133 | AC_CACHE_CHECK([whether __va_copy exists], ac_cv_have___va_copy, [ | ||
134 | AC_LINK_IFELSE([AC_LANG_PROGRAM([[ | ||
135 | #include <stdarg.h> | ||
136 | va_list x,y; | ||
137 | ]], [[ __va_copy(x,y); ]])], | ||
138 | [ ac_cv_have___va_copy="yes" ], [ ac_cv_have___va_copy="no" | ||
139 | ]) | ||
140 | ]) | ||
141 | if test "x$ac_cv_have___va_copy" = "xyes" ; then | ||
142 | AC_DEFINE([HAVE___VA_COPY], [1], [Define if __va_copy exists]) | ||
143 | fi | ||
144 | |||
120 | AC_CHECK_FUNC(getauxval, AC_DEFINE(HAVE_GETAUXVAL)) | 145 | AC_CHECK_FUNC(getauxval, AC_DEFINE(HAVE_GETAUXVAL)) |
121 | 146 | ||
122 | AC_CHECK_FUNC(funopen, AC_DEFINE(HAVE_FUNOPEN)) | 147 | AC_CHECK_FUNC(funopen, AC_DEFINE(HAVE_FUNOPEN)) |
diff --git a/crypto/Makefile.am.tpl b/crypto/Makefile.am.tpl index bb30aa2..d203b7b 100644 --- a/crypto/Makefile.am.tpl +++ b/crypto/Makefile.am.tpl | |||
@@ -39,7 +39,7 @@ libcompat_la_SOURCES += compat/strnlen.c | |||
39 | endif | 39 | endif |
40 | 40 | ||
41 | if NO_ASPRINTF | 41 | if NO_ASPRINTF |
42 | libcompat_la_SOURCES += compat/asprintf.c | 42 | libcompat_la_SOURCES += compat/bsd-asprintf.c |
43 | endif | 43 | endif |
44 | 44 | ||
45 | if NO_REALLOCARRAY | 45 | if NO_REALLOCARRAY |
diff --git a/crypto/compat/asprintf.c b/crypto/compat/asprintf.c deleted file mode 100644 index 45a539d..0000000 --- a/crypto/compat/asprintf.c +++ /dev/null | |||
@@ -1,94 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2004 Darren Tucker. | ||
3 | * | ||
4 | * Based originally on asprintf.c from OpenBSD: | ||
5 | * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | #include <errno.h> | ||
21 | #include <stdarg.h> | ||
22 | #include <stdlib.h> | ||
23 | |||
24 | #ifndef VA_COPY | ||
25 | # ifdef HAVE_VA_COPY | ||
26 | # define VA_COPY(dest, src) va_copy(dest, src) | ||
27 | # else | ||
28 | # ifdef HAVE___VA_COPY | ||
29 | # define VA_COPY(dest, src) __va_copy(dest, src) | ||
30 | # else | ||
31 | # define VA_COPY(dest, src) (dest) = (src) | ||
32 | # endif | ||
33 | # endif | ||
34 | #endif | ||
35 | |||
36 | #define INIT_SZ 128 | ||
37 | |||
38 | int | ||
39 | vasprintf(char **str, const char *fmt, va_list ap) | ||
40 | { | ||
41 | int ret = -1; | ||
42 | va_list ap2; | ||
43 | char *string, *newstr; | ||
44 | size_t len; | ||
45 | |||
46 | VA_COPY(ap2, ap); | ||
47 | if ((string = malloc(INIT_SZ)) == NULL) | ||
48 | goto fail; | ||
49 | |||
50 | ret = vsnprintf(string, INIT_SZ, fmt, ap2); | ||
51 | if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */ | ||
52 | *str = string; | ||
53 | } else if (ret == INT_MAX || ret < 0) { /* Bad length */ | ||
54 | free(string); | ||
55 | goto fail; | ||
56 | } else { /* bigger than initial, realloc allowing for nul */ | ||
57 | len = (size_t)ret + 1; | ||
58 | if ((newstr = realloc(string, len)) == NULL) { | ||
59 | free(string); | ||
60 | goto fail; | ||
61 | } else { | ||
62 | va_end(ap2); | ||
63 | VA_COPY(ap2, ap); | ||
64 | ret = vsnprintf(newstr, len, fmt, ap2); | ||
65 | if (ret >= 0 && (size_t)ret < len) { | ||
66 | *str = newstr; | ||
67 | } else { /* failed with realloc'ed string, give up */ | ||
68 | free(newstr); | ||
69 | goto fail; | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | va_end(ap2); | ||
74 | return (ret); | ||
75 | |||
76 | fail: | ||
77 | *str = NULL; | ||
78 | errno = ENOMEM; | ||
79 | va_end(ap2); | ||
80 | return (-1); | ||
81 | } | ||
82 | |||
83 | int asprintf(char **str, const char *fmt, ...) | ||
84 | { | ||
85 | va_list ap; | ||
86 | int ret; | ||
87 | |||
88 | *str = NULL; | ||
89 | va_start(ap, fmt); | ||
90 | ret = vasprintf(str, fmt, ap); | ||
91 | va_end(ap); | ||
92 | |||
93 | return ret; | ||
94 | } | ||
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 @@ | |||
42 | int | 42 | int |
43 | vasprintf(char **str, const char *fmt, va_list ap) | 43 | vasprintf(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 | ||
80 | fail: | 78 | fail: |
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 |