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-19  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.
This commit is contained in:
Pedro Alves 2017-10-19 15:25:59 +01:00
parent 280958942b
commit 2edf834e29
2 changed files with 22 additions and 26 deletions

View File

@ -1,3 +1,9 @@
2017-10-19 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.
2017-11-18 Keith Seitz <keiths@redhat.com>
* c-exp.y (oper): Canonicalize conversion operators of user-defined

View File

@ -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)
/* Read in the whole file. */
size_t len;
if (fseek (file.get (), 0, SEEK_END) == -1)
perror_with_name (_("seek to end of file"));
len = ftell (file.get ());
rewind (file.get ());
gdb::unique_xmalloc_ptr<char> text ((char *) xmalloc (len + 1));
fread (text.get (), 1, len, file.get ());
if (ferror (file.get ()))
{
size_t bytes_read;
/* 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;
}
offset += bytes_read;
if (feof (file.get ()))
break;
len = len * 2;
text.reset ((char *) xrealloc (text.release (), len));
warning (_("Read error from \"%s\""), filename);
return NULL;
}
text.get ()[offset] = '\0';
text.get ()[len] = '\0';
return text;
}