Introduction of pbs_asprintf()


This is to inform the community that the pbs_asprintf() function is now available in libutil.a. Please note there is a slight difference in how this function works compared to the asprintf() function provided by some operating systems. The synopsis is as follows:

int pbs_asprintf(char **dest, const char *format, ...);

While the return value is identical to asprintf(), the dest parameter will always point to a malloc()'d region of at least one byte even when format is an empty string. This allows the developer to pass the result to printf or str* functions without having to check the value. It is the responsibility of the caller to free() dest when it is no longer needed, or it will result in a memory leak.

The motivation for this function relates to new warnings issued by the gcc 8 compiler where it attempts to calculate string lengths with functions like snprintf(). There were several cases where the compiler detected a potential buffer overrun. These have since been addressed by PR-806.

If you encounter cases where the resulting string length is difficult to predict (e.g. building path names or printing them to a log) then you may find pbs_asprintf() of use. The function is available for both Linux and Windows.