Unverified Commit 6cf08d25 authored by Raul's avatar Raul Committed by GitHub
Browse files

Avoid overflow in large XTC files (#4485)

* Avoid overflow in large XTC files

* Also cast box indices to size_t
parent f82876f0
...@@ -123,20 +123,21 @@ void xtc_read(std::string filename, float* coords_arr, float* box_arr, float* ti ...@@ -123,20 +123,21 @@ void xtc_read(std::string filename, float* coords_arr, float* box_arr, float* ti
throw std::runtime_error("xtc_read(): natoms is 0\n"); throw std::runtime_error("xtc_read(): natoms is 0\n");
} }
XDRFILE_RAII xd(filename, "r"); XDRFILE_RAII xd(filename, "r");
int fidx = 0; size_t fidx = 0;
size_t nframes_long = nframes;
XTCFrame frame(natoms); XTCFrame frame(natoms);
while (exdrOK == frame.readNextFrame(xd)) { while (exdrOK == frame.readNextFrame(xd)) {
time_arr[fidx] = frame.time; time_arr[fidx] = frame.time;
step_arr[fidx] = frame.step; step_arr[fidx] = frame.step;
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) { for (int j = 0; j < 3; j++) {
box_arr[fidx + (3 * i + j) * nframes] = frame.box[i][j]; box_arr[fidx + (3 * i + j) * nframes_long] = frame.box[i][j];
} }
} }
for (int aidx = 0; aidx < natoms; aidx++) { for (size_t aidx = 0; aidx < natoms; aidx++) {
int xidx = Xf(aidx, fidx, nframes); size_t xidx = Xf(aidx, fidx, nframes_long);
int yidx = Yf(xidx, nframes); size_t yidx = Yf(xidx, nframes_long);
int zidx = Zf(yidx, nframes); size_t zidx = Zf(yidx, nframes_long);
coords_arr[xidx] = frame.positions[3 * aidx + 0]; coords_arr[xidx] = frame.positions[3 * aidx + 0];
coords_arr[yidx] = frame.positions[3 * aidx + 1]; coords_arr[yidx] = frame.positions[3 * aidx + 1];
coords_arr[zidx] = frame.positions[3 * aidx + 2]; coords_arr[zidx] = frame.positions[3 * aidx + 2];
...@@ -145,9 +146,9 @@ void xtc_read(std::string filename, float* coords_arr, float* box_arr, float* ti ...@@ -145,9 +146,9 @@ void xtc_read(std::string filename, float* coords_arr, float* box_arr, float* ti
} }
} }
static void box_from_array(matrix& matrix_box, float* box, int frame, int nframes) { static void box_from_array(matrix& matrix_box, float* box, size_t frame, size_t nframes) {
for (int i = 0; i < 3; i++) { for (size_t i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) { for (size_t j = 0; j < 3; j++) {
matrix_box[i][j] = box[(3 * i + j) * nframes + frame]; matrix_box[i][j] = box[(3 * i + j) * nframes + frame];
} }
} }
...@@ -156,12 +157,13 @@ static void box_from_array(matrix& matrix_box, float* box, int frame, int nframe ...@@ -156,12 +157,13 @@ static void box_from_array(matrix& matrix_box, float* box, int frame, int nframe
void xtc_write(std::string filename, int natoms, int nframes, int* step, float* timex, float* pos, float* box) { void xtc_write(std::string filename, int natoms, int nframes, int* step, float* timex, float* pos, float* box) {
XDRFILE_RAII xd(filename, "a"); XDRFILE_RAII xd(filename, "a");
XTCFrame frame(natoms); XTCFrame frame(natoms);
for (int f = 0; f < nframes; f++) { size_t nframes_long = nframes;
box_from_array(frame.box, box, f, nframes); for (size_t f = 0; f < nframes; f++) {
for (int i = 0; i < natoms; i++) { box_from_array(frame.box, box, f, nframes_long);
int xidx = Xf(i, f, nframes); for (size_t i = 0; i < natoms; i++) {
int yidx = Yf(xidx, nframes); size_t xidx = Xf(i, f, nframes_long);
int zidx = Zf(yidx, nframes); size_t yidx = Yf(xidx, nframes_long);
size_t zidx = Zf(yidx, nframes_long);
frame.positions[3 * i + 0] = pos[xidx]; frame.positions[3 * i + 0] = pos[xidx];
frame.positions[3 * i + 1] = pos[yidx]; frame.positions[3 * i + 1] = pos[yidx];
frame.positions[3 * i + 2] = pos[zidx]; frame.positions[3 * i + 2] = pos[zidx];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment