#886 closed Defect (fixed)
read_file_string quite inefficient and fails with binary files
| Reported by: | Nicolas | Owned by: | davea |
|---|---|---|---|
| Priority: | Minor | Milestone: | Undetermined |
| Component: | BOINC - API | Version: | |
| Keywords: | Cc: |
Description
The read_file_string function defined in lib/util.cpp is quite inefficient. It calls read_file_malloc to load the file into a malloc'd char buffer, then creates a std::string from that buffer and frees the buffer.
In addition, it doesn't pass the file length to the std::string constructor, so the string is cut at the first null byte in the file.
I attached two patches: one makes minor code cleanup to both read_file_string and read_file_malloc, the second rewrites read_file_string to do its own reading, without calling the _malloc version. (It's a bit of a mess; the basic operation without 'tail' and 'max_len' could be done in three lines if we were using C++ iostreams instead of FILE*, but whatever...)
(I haven't tested this patch beyond "it compiles"; I've been busy writing tests for and fixing a more serious bug)
Attachments (2)
Change History (6)
Changed 17 years ago by
| Attachment: | read_file-integer-types-fix.diff added |
|---|
Changed 17 years ago by
| Attachment: | read_file-load-directly-to-string.diff added |
|---|
comment:1 Changed 17 years ago by
| Owner: | changed from Bruce Allen to davea |
|---|
comment:2 Changed 17 years ago by
comment:3 Changed 17 years ago by
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
comment:4 Changed 17 years ago by
This wasn't really fixed, it was "wontfix"ed... (read_file_string is still inefficient, and still fails with binary, which is what I reported here)

The "data truncated at first null byte" issue causes #887.