# HG changeset patch # User John W. Eaton # Date 1393092378 18000 # Node ID 0bdecd41b2dd33aff2325a0d82038aedc897b8d5 # Parent fdd27f68b0118c097cd88bf5b84a77f2c5b7f0ea 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. diff -r fdd27f68b011 -r 0bdecd41b2dd libinterp/corefcn/oct-stream.cc --- 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; } diff -r fdd27f68b011 -r 0bdecd41b2dd test/io.tst --- 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);