aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2021-11-08 14:58:46 +0000
committerAndrew Burgess <aburgess@redhat.com>2021-11-16 17:45:45 +0000
commit8579fd136a614985bd27f20539c7bb7c5a51287d (patch)
treefb84850409a44e13e832cbadc9025d40c1d33d9f /gdbsupport
parent2bb7589ddf61e163f2e414e7033fad56ea17e784 (diff)
downloadgdb-8579fd136a614985bd27f20539c7bb7c5a51287d.zip
gdb-8579fd136a614985bd27f20539c7bb7c5a51287d.tar.gz
gdb-8579fd136a614985bd27f20539c7bb7c5a51287d.tar.bz2
gdb/gdbsupport: make xstrprintf and xstrvprintf return a unique_ptr
The motivation is to reduce the number of places where unmanaged pointers are returned from allocation type routines. All of the callers are updated. There should be no user visible changes after this commit.
Diffstat (limited to 'gdbsupport')
-rw-r--r--gdbsupport/common-utils.cc9
-rw-r--r--gdbsupport/common-utils.h6
2 files changed, 8 insertions, 7 deletions
diff --git a/gdbsupport/common-utils.cc b/gdbsupport/common-utils.cc
index 7a84345..42bce36 100644
--- a/gdbsupport/common-utils.cc
+++ b/gdbsupport/common-utils.cc
@@ -32,19 +32,18 @@ xzalloc (size_t size)
/* Like asprintf/vasprintf but get an internal_error if the call
fails. */
-char *
+gdb::unique_xmalloc_ptr<char>
xstrprintf (const char *format, ...)
{
- char *ret;
va_list args;
va_start (args, format);
- ret = xstrvprintf (format, args);
+ gdb::unique_xmalloc_ptr<char> ret = xstrvprintf (format, args);
va_end (args);
return ret;
}
-char *
+gdb::unique_xmalloc_ptr<char>
xstrvprintf (const char *format, va_list ap)
{
char *ret = NULL;
@@ -56,7 +55,7 @@ xstrvprintf (const char *format, va_list ap)
happen, but just to be sure. */
if (ret == NULL || status < 0)
internal_error (__FILE__, __LINE__, _("vasprintf call failed"));
- return ret;
+ return gdb::unique_xmalloc_ptr<char> (ret);
}
int
diff --git a/gdbsupport/common-utils.h b/gdbsupport/common-utils.h
index 4a75e67..1e90a5c 100644
--- a/gdbsupport/common-utils.h
+++ b/gdbsupport/common-utils.h
@@ -23,6 +23,7 @@
#include <string>
#include <vector>
#include "gdbsupport/byte-vector.h"
+#include "gdbsupport/gdb_unique_ptr.h"
#include "poison.h"
@@ -54,8 +55,9 @@ void *xzalloc (size_t);
/* Like asprintf and vasprintf, but return the string, throw an error
if no memory. */
-char *xstrprintf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
-char *xstrvprintf (const char *format, va_list ap)
+gdb::unique_xmalloc_ptr<char> xstrprintf (const char *format, ...)
+ ATTRIBUTE_PRINTF (1, 2);
+gdb::unique_xmalloc_ptr<char> xstrvprintf (const char *format, va_list ap)
ATTRIBUTE_PRINTF (1, 0);
/* Like snprintf, but throw an error if the output buffer is too small. */