summaryrefslogtreecommitdiff
path: root/main/utils.c
diff options
context:
space:
mode:
authorRichard Mudgett <rmudgett@digium.com>2017-11-02 18:40:20 -0500
committerRichard Mudgett <rmudgett@digium.com>2017-11-06 11:27:44 -0600
commitbe08bb60765be338fffd33c9c1140df1ac618dbe (patch)
tree707e4eaad349301916e291dd940beecc2efb085f /main/utils.c
parent6f93ee230a609d311dc6911d8bd2bccbd047a34f (diff)
Fix ast_(v)asprintf() malloc failure usage conditions.
When (v)asprintf() fails, the state of the allocated buffer is undefined. The library had better not leave an allocated buffer as a result or no one will know to free it. The most likely way it can return failure is for an allocation failure. If the printf conversion fails then you actually have a threading problem which is much worse because another thread modified the parameter values. * Made __ast_asprintf()/__ast_vasprintf() set the returned buffer to NULL on failure. That is much more useful than either an uninitialized pointer or a pointer that has already been freed. Many uses won't have to check for failure to ensure that the buffer won't be double freed or prevent an attempt to free an uninitialized pointer. * stasis.c: Fixed memory leak in multi_object_blob_to_ami() allocated by ast_asprintf(). * ari/resource_bridges.c:ari_bridges_play_helper(): Remove assignment to the wrong thing which is now not needed even if assigning to the right thing. Change-Id: Ib5252fb8850ecf0f78ed0ee2ca0796bda7e91c23
Diffstat (limited to 'main/utils.c')
-rw-r--r--main/utils.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/main/utils.c b/main/utils.c
index f20ccd36e..dd7176295 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -2349,7 +2349,13 @@ int __ast_asprintf(const char *file, int lineno, const char *func, char **ret, c
va_list ap;
va_start(ap, fmt);
- if ((res = vasprintf(ret, fmt, ap)) == -1) {
+ res = vasprintf(ret, fmt, ap);
+ if (res < 0) {
+ /*
+ * *ret is undefined so set to NULL to ensure it is
+ * initialized to something useful.
+ */
+ *ret = NULL;
MALLOC_FAILURE_MSG;
}
va_end(ap);