--- ray/src/util/rcollate.c 2019/11/08 02:10:38 2.31 +++ ray/src/util/rcollate.c 2019/11/08 16:36:36 2.33 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.31 2019/11/08 02:10:38 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.33 2019/11/08 16:36:36 greg Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) @@ -394,7 +394,7 @@ get_input_pos(int r, int c) } else if (transpose) /* transpose only */ n = (long)c*ni_columns + r; else /* XXX should never happen! */ - n = (long)r*ni_columns + c; + n = (long)r*no_columns + c; return(n); } @@ -421,8 +421,8 @@ do_reorder(const MEMLOAD *mp) } else if ((ni_rows > 0) & (ni_columns > 0)) { nrecords = ni_rows*ni_columns; if (nrecords > mp->len/(n_comp*comp_size)) { - fprintf(stderr, - "Input too small for specified size and type\n"); + fputs("Input too small for specified size and type\n", + stderr); return(0); } } else @@ -446,13 +446,20 @@ do_reorder(const MEMLOAD *mp) no_columns = ni_columns; if (no_rows <= 0) no_rows = ni_rows; - if ((no_rows != ni_rows) | (no_columns != ni_columns)) - goto badspec; } + if (ni_rows*ni_columns != no_rows*no_columns) { + fputs("Number of input and output records do not match\n", + stderr); + return(0); + } /* reorder records */ for (i = 0; i < no_rows; i++) { for (j = 0; j < no_columns; j++) { long n = get_input_pos(i, j); + if (n >= nrecords) { + fputs("Index past end-of-file\n", stderr); + return(0); + } if (rp != NULL) { /* ASCII output */ print_record(rp, n); putc(tabEOL[j >= no_columns-1], stdout);