1
1

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>
Этот коммит содержится в:
Brian Barrett 2018-09-27 15:26:50 -07:00
родитель a7964bf1ad
Коммит c087365ead
2 изменённых файлов: 21 добавлений и 2 удалений

Просмотреть файл

@ -200,6 +200,7 @@ int opal_asprintf(char **ptr, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
/* opal_vasprintf guarantees that *ptr is set to NULL on error */
length = opal_vasprintf(ptr, fmt, ap);
va_end(ap);
@ -212,6 +213,13 @@ int opal_vasprintf(char **ptr, const char *fmt, va_list ap)
int length;
va_list ap2;
#ifdef HAVE_VASPRINTF
length = vasprintf(ptr, fmt, ap);
if (length < 0) {
*ptr = NULL;
}
#else
/* va_list might have pointer to internal state and using
it twice is a bad idea. So make a copy for the second
use. Copy order taken from Autoconf docs. */
@ -246,6 +254,7 @@ int opal_vasprintf(char **ptr, const char *fmt, va_list ap)
errno = ENOMEM;
return -1;
}
#endif
return length;
}

Просмотреть файл

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