Skip to content

Commit c087365

Browse files
committed
opal/util: Always support BSD behavior of asprintf
Open MPI's developers like to assume that asprintf() always sets the ptr to NULL on error, but the standard (and Linux glibc) do not guarantee this. As a result, we're making opal_asprintf() always available for developers, which will guarantee that ptr is set to NULL on error. Signed-off-by: Brian Barrett <bbarrett@amazon.com>
1 parent a7964bf commit c087365

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

opal/util/printf.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ int opal_asprintf(char **ptr, const char *fmt, ...)
200200
va_list ap;
201201

202202
va_start(ap, fmt);
203+
/* opal_vasprintf guarantees that *ptr is set to NULL on error */
203204
length = opal_vasprintf(ptr, fmt, ap);
204205
va_end(ap);
205206

@@ -212,6 +213,13 @@ int opal_vasprintf(char **ptr, const char *fmt, va_list ap)
212213
int length;
213214
va_list ap2;
214215

216+
#ifdef HAVE_VASPRINTF
217+
length = vasprintf(ptr, fmt, ap);
218+
if (length < 0) {
219+
*ptr = NULL;
220+
}
221+
#else
222+
215223
/* va_list might have pointer to internal state and using
216224
it twice is a bad idea. So make a copy for the second
217225
use. Copy order taken from Autoconf docs. */
@@ -246,6 +254,7 @@ int opal_vasprintf(char **ptr, const char *fmt, va_list ap)
246254
errno = ENOMEM;
247255
return -1;
248256
}
257+
#endif
249258

250259
return length;
251260
}

opal/util/printf.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ OPAL_DECLSPEC int opal_vsnprintf(char *str, size_t size, const char *fmt, va_li
9595
*
9696
* Returns the number of characters printed.
9797
*
98-
* THIS IS A PORTABILITY FEATURE: USE asprintf() in CODE.
98+
* Unlike opal_snprintf and opal_vsnprintf, opal_asprintf() is always
99+
* available and guarantees that *ptr is NULL when the underlying
100+
* asprintf fails. The standard does not require *ptr be set to NULL
101+
* on error and some implementations (modern Linux) do not guarantee
102+
* such behavior.
103+
*
99104
*/
100105
OPAL_DECLSPEC int opal_asprintf(char **ptr, const char *fmt, ...) __opal_attribute_format__(__printf__, 2, 3);
101106

@@ -119,7 +124,12 @@ OPAL_DECLSPEC int opal_asprintf(char **ptr, const char *fmt, ...) __opal_attrib
119124
*
120125
* Returns the number of characters printed.
121126
*
122-
* THIS IS A PORTABILITY FEATURE: USE vasprintf() in CODE.
127+
* Unlike opal_snprintf and opal_vsnprintf, opal_vasprintf() is always
128+
* available and guarantees that *ptr is NULL when the underlying
129+
* asprintf fails. The standard does not require *ptr be set to NULL
130+
* on error and some implementations (modern Linux) do not guarantee
131+
* such behavior.
132+
*
123133
*/
124134
OPAL_DECLSPEC int opal_vasprintf(char **ptr, const char *fmt, va_list ap) __opal_attribute_format__(__printf__, 2, 0);
125135

0 commit comments

Comments
 (0)