Skip to content

Commit e689204

Browse files
committed
Fix ezxml problems when loading files >2GB in size (e.g. directrf packing .net file).
ezxml relied upon a single read() system call to load the full file. However read() may only return 2GB at a time. New implementation uses fread() instead.
1 parent b7f2bb1 commit e689204

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

libarchfpga/ezxml.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <stdarg.h>
3737
#include <string.h>
3838
#include <ctype.h>
39+
#include <assert.h>
3940

4041
/* Ted Campbell, Aug 14 2007 */
4142
#if !defined(WIN32) && !defined(_WIN32)
@@ -787,24 +788,20 @@ ezxml_t ezxml_parse_str(char *s, size_t len) {
787788
/* Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire */
788789
/* stream into memory and then parses it. For xml files, use ezxml_parse_file() */
789790
/* or ezxml_parse_fd() */
790-
ezxml_t ezxml_parse_fp(FILE * fp) {
791+
ezxml_t ezxml_parse_fp(FILE * fp, size_t size) {
791792
ezxml_root_t root;
792-
size_t l, len = 0;
793-
char *s;
793+
char *s; //The buffer
794794

795795
/* Jason Luu, Aug 29, 2007. Removed assignment in conditional statement */
796-
s = (char*)malloc(EZXML_BUFSIZE);
796+
s = (char*)malloc(size);
797797
if (!s)
798798
return NULL;
799-
do {
800-
len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp));
801-
if (l == EZXML_BUFSIZE)
802-
s = (char*)realloc(s, len + EZXML_BUFSIZE);
803-
} while (s && l == EZXML_BUFSIZE);
804799

805-
if (!s)
806-
return NULL;
807-
root = (ezxml_root_t) ezxml_parse_str(s, len);
800+
size_t bytes_read = fread(s, 1, size, fp);
801+
802+
assert(bytes_read == size);
803+
804+
root = (ezxml_root_t) ezxml_parse_str(s, bytes_read);
808805
/* Ted Campbell, Aug 14, 2007. Added explicit cast. */
809806
root->len = (size_t) (-1); /* so we know to free s in ezxml_free() */
810807
return &root->xml;
@@ -844,11 +841,18 @@ ezxml_t ezxml_parse_fd(int fd) {
844841

845842
/* a wrapper for ezxml_parse_fd that accepts a file name */
846843
ezxml_t ezxml_parse_file(const char *file) {
847-
int fd = open(file, O_RDONLY, 0);
848-
ezxml_t xml = ezxml_parse_fd(fd);
844+
ezxml_t xml;
845+
846+
FILE* fp = fopen(file, "r");
847+
if (fp) {
848+
//Determine the file size
849+
struct stat st;
850+
stat(file, &st);
851+
849852

850-
if (fd >= 0)
851-
close(fd);
853+
xml = ezxml_parse_fp(fp, st.st_size);
854+
fclose(fp);
855+
}
852856
return xml;
853857
}
854858

0 commit comments

Comments
 (0)