According to C99 §7.20.3.4:
If memory for the new object cannot be allocated, the old object is not
deallocated and its value is unchanged.
Therefore, a temporary pointer must be used to ensure the original
object can still be deallocated should realloc(3) return a null pointer.
The standard requires both va_start and va_end to be called from the
same function. On the other hand, vsnprintf and vsprintf leave the
va_list on an undefined state according to the standard, so a copy must
be created before a second call to these functions.
The macros provided by this library would expand to an unguarded
conditional that could have potential unwanted consequences. Consider
the following example:
dynstr_append_or_ret_null(&d, "example");
else
{
/* This is unexpectedly working. */
}
The example above would expand to:
if (dynstr_append(__VA_ARGS__) != DYNSTR_OK) return false;
else
{
/* This is unexpectedly working. */
}
Which is valid C yet allows possibly unexpected behaviour. The solution
is then to enclose these conditions into a `do while (0)` statement.
- Removed trailing ';' from convenience macros.
- Convenience macros did not really need the ## __VA_ARGS__ extension.
Simply grouping all parameters into '...', while decreasing readability,
solves the portability issue.
- Added C99 check to dynstr.c.