Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#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)

read_file-integer-types-fix.diff (1.6 KB) - added by Nicolas 16 years ago.
read_file-load-directly-to-string.diff (1.1 KB) - added by Nicolas 16 years ago.

Download all attachments as: .zip

Change History (6)

Changed 16 years ago by Nicolas

Changed 16 years ago by Nicolas

comment:1 Changed 16 years ago by romw

Owner: changed from Bruce Allen to davea

comment:2 Changed 16 years ago by Nicolas

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

comment:3 Changed 16 years ago by davea

Resolution: fixed
Status: newclosed

(In [17966]) - sample bitwise validator: make it work for binary files

fixes #886, #887

comment:4 Changed 16 years ago by Nicolas

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)

Note: See TracTickets for help on using tickets.