aboutsummaryrefslogtreecommitdiff
path: root/libctf/ctf-util.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2019-09-17 06:57:00 +0100
committerNick Alcock <nick.alcock@oracle.com>2019-09-24 14:06:32 +0100
commit2ca5b4ab8a02b17ac4ee4a6f8474c288cb3fbef9 (patch)
tree3411d9d3f44258b642b7f606430a8b4948d19163 /libctf/ctf-util.c
parent6c1ab5fe1eec2cfc6d28668a73964a48b507a25b (diff)
downloadbinutils-2ca5b4ab8a02b17ac4ee4a6f8474c288cb3fbef9.zip
binutils-2ca5b4ab8a02b17ac4ee4a6f8474c288cb3fbef9.tar.gz
binutils-2ca5b4ab8a02b17ac4ee4a6f8474c288cb3fbef9.tar.bz2
libctf: make ctf_dump not crash on OOM
ctf_dump calls ctf_str_append extensively but never checks to see if it returns NULL (on OOM). If it ever does, we truncate the string we are appending to and leak it! Instead, create a variant of ctf_str_append that returns the *original string* on OOM, and use it in ctf-dump. It is far better to omit a tiny piece of a dump on OOM than to omit a bigger piece, and it is also better to do this in what is after all purely debugging code than it is to uglify ctf-dump.c with huge numbers of checks for the out-of-memory case. Slightly truncated debugging output is better than no debugging output at all and an out-of-memory message. New in v4. libctf/ * ctf-impl.h (ctf_str_append_noerr): Declare. * ctf-util.c (ctf_str_append_noerr): Define in terms of ctf_str_append. * ctf-dump.c (str_append): New, call it. (ctf_dump_format_type): Use str_append, not ctf_str_append. (ctf_dump_label): Likewise. (ctf_dump_objts): Likewise. (ctf_dump_funcs): Likewise. (ctf_dump_var): Likewise. (ctf_dump_member): Likewise. (ctf_dump_type): Likewise. (ctf_dump): Likewise.
Diffstat (limited to 'libctf/ctf-util.c')
-rw-r--r--libctf/ctf-util.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/libctf/ctf-util.c b/libctf/ctf-util.c
index d10b2b5..d4a1c5a 100644
--- a/libctf/ctf-util.c
+++ b/libctf/ctf-util.c
@@ -103,7 +103,7 @@ ctf_sym_to_elf64 (const Elf32_Sym *src, Elf64_Sym *dst)
return dst;
}
-/* A string appender working on dynamic strings. */
+/* A string appender working on dynamic strings. Returns NULL on OOM. */
char *
ctf_str_append (char *s, const char *append)
@@ -127,6 +127,19 @@ ctf_str_append (char *s, const char *append)
return s;
}
+/* A version of ctf_str_append that returns the old string on OOM. */
+
+char *
+ctf_str_append_noerr (char *s, const char *append)
+{
+ char *new_s;
+
+ new_s = ctf_str_append (s, append);
+ if (!new_s)
+ return s;
+ return new_s;
+}
+
/* A realloc() that fails noisily if called with any ctf_str_num_users. */
void *
ctf_realloc (ctf_file_t *fp, void *ptr, size_t size)