Skip to content

Commit

Permalink
sc_io_file: fix reallocation in read function
Browse files Browse the repository at this point in the history
  • Loading branch information
cburstedde committed Jan 16, 2024
1 parent 6b78c76 commit c8a2a3f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/sc_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,27 +506,30 @@ sc_io_file_load (const char *filename, sc_array_t * buffer)
return file_return (-1, sink, source);
}

/* prepare read buffer */
bpos = 0;
sc_array_resize (buffer, bpos + bwins);

/* perform reading in a loop */
bpos = 0;
for (i = 0;; ++i) {
/* make room in read buffer */
sc_array_resize (buffer, bpos + bwins);

/* read next fixed size batch of data */
if (sc_io_source_read (source, sc_array_index (buffer, bpos),
bwins, &bout)) {
SC_LERRORF ("sc_io_file_load: error reading from %s\n", filename);
return file_return (-1, sink, source);
}

/* examine buffer status after reading */
if (bout < bwins) {
/* we have reached end of file */
/* we have reached end of file: finalize buffer */
sc_array_resize (buffer, bpos += bout);
break;
}
sc_array_resize (buffer, bpos += bwins);

/* TO DO: do not read twice if EOF was reached on window size */
/* update read buffer size */
bpos += bwins;
}
SC_ASSERT (bpos == buffer->elem_count);

/* close file and free metadata */
if (source_destroy_null (&source)) {
Expand Down

0 comments on commit c8a2a3f

Please sign in to comment.