Mercurial > octave
changeset 18518:0bdecd41b2dd stable
correctly size fread result (bug #41648)
* oct-stream.cc (octave_base_stream::read): When reading to EOF, don't
add extra column to the result matrix if the number of elements found
is an exact multiple of the number of rows requested.
Avoid mixed signed/unsigned comparisons.
* io.tst: New tests.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 22 Feb 2014 13:06:18 -0500 |
parents | fdd27f68b011 |
children | c9ace0567f13 |
files | libinterp/corefcn/oct-stream.cc test/io.tst |
diffstat | 2 files changed, 40 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/oct-stream.cc Fri Feb 21 15:28:02 2014 -0500 +++ b/libinterp/corefcn/oct-stream.cc Sat Feb 22 13:06:18 2014 -0500 @@ -3276,7 +3276,7 @@ char_count += gcount; - size_t nel = gcount / input_elt_size; + octave_idx_type nel = gcount / input_elt_size; count += nel; @@ -3297,7 +3297,7 @@ // the original position? seek (orig_pos, SEEK_SET); - size_t remaining = eof_pos - orig_pos; + off_t remaining = eof_pos - orig_pos; if (remaining < skip) seek (0, SEEK_END); @@ -3312,7 +3312,12 @@ if (read_to_eof) { if (nc < 0) - nc = count / nr + 1; + { + nc = count / nr; + + if (count % nr != 0) + nc ++; + } else nr = count; }
--- a/test/io.tst Fri Feb 21 15:28:02 2014 -0500 +++ b/test/io.tst Sat Feb 22 13:06:18 2014 -0500 @@ -555,6 +555,11 @@ %! [data, count] = fread (id, inf, "2*uint8", 2); %! assert (data, [0; 1; 4; 5; 8; 9; 12; 13]); %! assert (count, 8); +%! fclose (id); + +%!test +%! id = tmpfile (); +%! fwrite (id, char (0:15)); %! frewind (id); %! [data, count] = fread (id, 3, "2*uint8", 3); %! assert (data, [0; 1; 5]); @@ -569,3 +574,30 @@ %! assert (data, []); %! assert (count, 0); %! fclose (id); + +%!test +%! id = tmpfile (); +%! fwrite (id, char (0:15)); +%! frewind (id); +%! [data, count] = fread (id, [1, Inf], "4*uint16", 3); +%! assert (data, [256, 770, 1284, 1798, 3083, 3597]); +%! assert (count, 6); +%! fclose (id); + +%!test +%! id = tmpfile (); +%! fwrite (id, char (0:15)); +%! frewind (id); +%! [data, count] = fread (id, [1, Inf], "4*uint16", 3); +%! assert (data, [256, 770, 1284, 1798, 3083, 3597]); +%! assert (count, 6); +%! fclose (id); + +%!test +%! id = tmpfile (); +%! fwrite (id, char (0:15)); +%! frewind (id); +%! [data, count] = fread (id, [3, Inf], "4*uint16", 3); +%! assert (data, [256, 1798; 770, 3083; 1284, 3597]); +%! assert (count, 6); +%! fclose (id);