* [pushed] Fix double-free corruption
@ 2017-10-17 11:42 Pedro Alves
2017-10-17 12:20 ` [PATCH] xml_fetch_content_from_file: Read in whole file in one go (Re: [pushed] Fix double-free corruption) Pedro Alves
0 siblings, 1 reply; 4+ messages in thread
From: Pedro Alves @ 2017-10-17 11:42 UTC (permalink / raw)
To: gdb-patches
Fixes a double-free regression introduced by commit b7b030adc405
("Return unique_xmalloc_ptr from target_read_stralloc"):
gdb.sum:
Running src/gdb/testsuite/gdb.base/catch-syscall.exp ...
ERROR: Process no longer exists
Valgrind shows:
(gdb) catch syscall
==3687== Thread 1:
==3687== Invalid free() / delete / delete[] / realloc()
==3687== at 0x4C29CF0: free (vg_replace_malloc.c:530)
==3687== by 0x610862: xfree(void*) (common-utils.c:101)
==3687== by 0x440D5D: gdb::xfree_deleter<char>::operator()(char*) const (gdb_unique_ptr.h:34)
==3687== by 0x446CC6: std::unique_ptr<char, gdb::xfree_deleter<char> >::reset(char*) (unique_ptr.h:344)
==3687== by 0x81BE50: xml_fetch_content_from_file(char const*, void*) (xml-support.c:1042)
==3687== by 0x81DA86: xml_init_syscalls_info(char const*) (xml-syscall.c:366)
==3687== by 0x81DBDD: init_syscalls_info(gdbarch*) (xml-syscall.c:398)
==3687== by 0x81E131: get_syscall_by_number(gdbarch*, int, syscall*) (xml-syscall.c:599)
==3687== by 0x5BE86F: catch_syscall_command_1(char*, int, cmd_list_element*) (break-catch-syscall.c:481)
==3687== by 0x4B46B1: do_sfunc(cmd_list_element*, char*, int) (cli-decode.c:138)
==3687== by 0x4B76B8: cmd_func(cmd_list_element*, char*, int) (cli-decode.c:1952)
==3687== by 0x7E91C7: execute_command(char*, int) (top.c:615)
==3687== Address 0x14332ae0 is 0 bytes inside a block of size 4,096 free'd
==3687== at 0x4C2AB8B: realloc (vg_replace_malloc.c:785)
==3687== by 0x610792: xrealloc (common-utils.c:62)
==3687== by 0x81BE3E: xml_fetch_content_from_file(char const*, void*) (xml-support.c:1042)
==3687== by 0x81DA86: xml_init_syscalls_info(char const*) (xml-syscall.c:366)
==3687== by 0x81DBDD: init_syscalls_info(gdbarch*) (xml-syscall.c:398)
==3687== by 0x81E131: get_syscall_by_number(gdbarch*, int, syscall*) (xml-syscall.c:599)
==3687== by 0x5BE86F: catch_syscall_command_1(char*, int, cmd_list_element*) (break-catch-syscall.c:481)
==3687== by 0x4B46B1: do_sfunc(cmd_list_element*, char*, int) (cli-decode.c:138)
==3687== by 0x4B76B8: cmd_func(cmd_list_element*, char*, int) (cli-decode.c:1952)
==3687== by 0x7E91C7: execute_command(char*, int) (top.c:615)
==3687== by 0x6A422D: command_handler(char*) (event-top.c:583)
==3687== by 0x6A45F2: command_line_handler(char*) (event-top.c:773)
[...]
The problem is that if xrealloc decides it needs a new memory block,
it frees the previous block/pointer, and then text.reset() frees it
again.
gdb/ChangeLog:
2017-10-17 Pedro Alves <palves@redhat.com>
* xml-support.c (xml_fetch_content_from_file): Call
unique_ptr::release() instead unique_ptr::get() when passing
through xrealloc.
---
gdb/ChangeLog | 6 ++++++
gdb/xml-support.c | 2 +-
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 6a8d16f..65952c4 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2017-10-17 Pedro Alves <palves@redhat.com>
+
+ * xml-support.c (xml_fetch_content_from_file): Call
+ unique_ptr::release() instead unique_ptr::get() when passing
+ through xrealloc.
+
2017-10-17 Yao Qi <yao.qi@linaro.org>
* regcache.c (regcache::xfer_part): Remove parameters read and
diff --git a/gdb/xml-support.c b/gdb/xml-support.c
index 76d03b9..42a4c91 100644
--- a/gdb/xml-support.c
+++ b/gdb/xml-support.c
@@ -1039,7 +1039,7 @@ xml_fetch_content_from_file (const char *filename, void *baton)
break;
len = len * 2;
- text.reset ((char *) xrealloc (text.get (), len));
+ text.reset ((char *) xrealloc (text.release (), len));
}
text.get ()[offset] = '\0';
--
2.5.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] xml_fetch_content_from_file: Read in whole file in one go (Re: [pushed] Fix double-free corruption)
2017-10-17 11:42 [pushed] Fix double-free corruption Pedro Alves
@ 2017-10-17 12:20 ` Pedro Alves
2017-10-18 22:30 ` John Baldwin
0 siblings, 1 reply; 4+ messages in thread
From: Pedro Alves @ 2017-10-17 12:20 UTC (permalink / raw)
To: gdb-patches
On 10/17/2017 12:41 PM, Pedro Alves wrote:
> Fixes a double-free regression introduced by commit b7b030adc405
> ("Return unique_xmalloc_ptr from target_read_stralloc"):
>
> gdb.sum:
> Running src/gdb/testsuite/gdb.base/catch-syscall.exp ...
> ERROR: Process no longer exists
>
...
> The problem is that if xrealloc decides it needs a new memory block,
> it frees the previous block/pointer, and then text.reset() frees it
> again.
Looking a bit deeper, I can't seem to find a reason this code is
reading in chunks in the first place? Why not read it all in one
go? Like patch below.
From a08ecd67cf5bd87c7d2e64ca443017b78d76aa04 Mon Sep 17 00:00:00 2001
From: Pedro Alves <palves@redhat.com>
Date: Tue, 17 Oct 2017 13:02:13 +0100
Subject: [PATCH] xml_fetch_content_from_file: Read in whole file in one go
There doesn't seem to be a good reason we're reading the file one
chunk at a time.
gdb/ChangeLog:
2017-10-17 Pedro Alves <palves@redhat.com>
* xml-support.c (xml_fetch_content_from_file): Don't read in
chunks. Instead use fseek to determine the file's size, and read
it in one go.
---
gdb/xml-support.c | 36 +++++++++++++-----------------------
1 file changed, 13 insertions(+), 23 deletions(-)
diff --git a/gdb/xml-support.c b/gdb/xml-support.c
index 42a4c91..69aa9db 100644
--- a/gdb/xml-support.c
+++ b/gdb/xml-support.c
@@ -998,7 +998,6 @@ xml_fetch_content_from_file (const char *filename, void *baton)
{
const char *dirname = (const char *) baton;
gdb_file_up file;
- size_t len, offset;
if (dirname && *dirname)
{
@@ -1015,34 +1014,25 @@ xml_fetch_content_from_file (const char *filename, void *baton)
if (file == NULL)
return NULL;
- /* Read in the whole file, one chunk at a time. */
- len = 4096;
- offset = 0;
- gdb::unique_xmalloc_ptr<char> text ((char *) xmalloc (len));
- while (1)
- {
- size_t bytes_read;
+ /* Read in the whole file. */
- /* Continue reading where the last read left off. Leave at least
- one byte so that we can NUL-terminate the result. */
- bytes_read = fread (text.get () + offset, 1, len - offset - 1,
- file.get ());
- if (ferror (file.get ()))
- {
- warning (_("Read error from \"%s\""), filename);
- return NULL;
- }
+ size_t len;
- offset += bytes_read;
+ if (fseek (file.get (), 0, SEEK_END) == -1)
+ perror_with_name (_("seek to end of file"));
+ len = ftell (file.get ());
+ rewind (file.get ());
- if (feof (file.get ()))
- break;
+ gdb::unique_xmalloc_ptr<char> text ((char *) xmalloc (len + 1));
- len = len * 2;
- text.reset ((char *) xrealloc (text.release (), len));
+ fread (text.get (), 1, len, file.get ());
+ if (ferror (file.get ()))
+ {
+ warning (_("Read error from \"%s\""), filename);
+ return {};
}
- text.get ()[offset] = '\0';
+ text.get ()[len] = '\0';
return text;
}
--
2.5.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] xml_fetch_content_from_file: Read in whole file in one go (Re: [pushed] Fix double-free corruption)
2017-10-17 12:20 ` [PATCH] xml_fetch_content_from_file: Read in whole file in one go (Re: [pushed] Fix double-free corruption) Pedro Alves
@ 2017-10-18 22:30 ` John Baldwin
2017-10-19 14:27 ` Pedro Alves
0 siblings, 1 reply; 4+ messages in thread
From: John Baldwin @ 2017-10-18 22:30 UTC (permalink / raw)
To: gdb-patches; +Cc: Pedro Alves
On Tuesday, October 17, 2017 01:20:14 PM Pedro Alves wrote:
> On 10/17/2017 12:41 PM, Pedro Alves wrote:
> > Fixes a double-free regression introduced by commit b7b030adc405
> > ("Return unique_xmalloc_ptr from target_read_stralloc"):
> >
> > gdb.sum:
> > Running src/gdb/testsuite/gdb.base/catch-syscall.exp ...
> > ERROR: Process no longer exists
> >
>
>
> ...
>
>
> > The problem is that if xrealloc decides it needs a new memory block,
> > it frees the previous block/pointer, and then text.reset() frees it
> > again.
>
> Looking a bit deeper, I can't seem to find a reason this code is
> reading in chunks in the first place? Why not read it all in one
> go? Like patch below.
Seems sensible. It's unlikely that an XML file will be stored directly
on a tape such that seek() is non-optimal. :)
--
John Baldwin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] xml_fetch_content_from_file: Read in whole file in one go (Re: [pushed] Fix double-free corruption)
2017-10-18 22:30 ` John Baldwin
@ 2017-10-19 14:27 ` Pedro Alves
0 siblings, 0 replies; 4+ messages in thread
From: Pedro Alves @ 2017-10-19 14:27 UTC (permalink / raw)
To: John Baldwin, gdb-patches
On 10/18/2017 06:43 PM, John Baldwin wrote:
> On Tuesday, October 17, 2017 01:20:14 PM Pedro Alves wrote:
>> Looking a bit deeper, I can't seem to find a reason this code is
>> reading in chunks in the first place? Why not read it all in one
>> go? Like patch below.
>
> Seems sensible. It's unlikely that an XML file will be stored directly
> on a tape such that seek() is non-optimal. :)
>
:-)
Thanks much for the review. I pushed this in now.
--
Pedro Alves
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-10-19 14:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-17 11:42 [pushed] Fix double-free corruption Pedro Alves
2017-10-17 12:20 ` [PATCH] xml_fetch_content_from_file: Read in whole file in one go (Re: [pushed] Fix double-free corruption) Pedro Alves
2017-10-18 22:30 ` John Baldwin
2017-10-19 14:27 ` Pedro Alves
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).