in src/strings.c [134:207]
STRING_HANDLE STRING_construct_sprintf(const char* format, ...)
{
STRING* result;
#ifdef STRINGS_C_SPRINTF_BUFFER_SIZE
size_t maxBufSize = STRINGS_C_SPRINTF_BUFFER_SIZE;
char buf[STRINGS_C_SPRINTF_BUFFER_SIZE];
#else
size_t maxBufSize = 0;
char* buf = NULL;
#endif
if (format != NULL)
{
va_list arg_list;
int length;
va_start(arg_list, format);
va_list arg_list_clone;
va_copy(arg_list_clone, arg_list);
/* Codes_SRS_STRING_07_041: [STRING_construct_sprintf shall determine the size of the resulting string and allocate the necessary memory.] */
length = vsnprintf(buf, maxBufSize, format, arg_list);
va_end(arg_list);
if (length > 0)
{
result = malloc(sizeof(STRING));
if (result != NULL)
{
result->s = malloc(length + (size_t)1);
if (result->s != NULL)
{
if (vsnprintf(result->s, length + 1, format, arg_list_clone) < 0)
{
/* Codes_SRS_STRING_07_040: [If any error is encountered STRING_construct_sprintf shall return NULL.] */
free(result->s);
free(result);
result = NULL;
LogError("Failure: vsnprintf formatting failed.");
}
}
else
{
/* Codes_SRS_STRING_07_040: [If any error is encountered STRING_construct_sprintf shall return NULL.] */
free(result);
result = NULL;
LogError("Failure: allocation sprintf value failed.");
}
}
else
{
LogError("Failure: allocation failed.");
}
}
else if (length == 0)
{
result = STRING_new();
}
else
{
/* Codes_SRS_STRING_07_039: [If the parameter format is NULL then STRING_construct_sprintf shall return NULL.] */
result = NULL;
LogError("Failure: vsnprintf return negative length");
}
va_end(arg_list_clone);
}
else
{
LogError("Failure: invalid argument.");
result = NULL;
}
/* Codes_SRS_STRING_07_045: [STRING_construct_sprintf shall allocate a new string with the value of the specified printf formated const char. ] */
return (STRING_HANDLE)result;
}