                                                           tensor, &tensorOffset);
 
     IndexType indexValue = (IndexType)index.data[indexOffset] - TH_INDEX_BASE;
+    assert(indexValue >= 0 && indexValue < tensor.sizes[dim]);
     tensorOffset += indexValue * tensor.strides[dim];
 
     tensor.data[tensorOffset] = src.data[srcOffset];

                                                          src, &srcOffset);

    IndexType indexValue = (IndexType)index.data[indexOffset] - TH_INDEX_BASE;
    srcOffset += indexValue * src.strides[dim];

    tensor.data[tensorOffset] = src.data[srcOffset];

                                                          tensor, &tensorOffset);

    IndexType indexValue = (IndexType)index.data[indexOffset] - TH_INDEX_BASE;
    assert(indexValue >= 0 && indexValue < tensor.sizes[dim]);
    tensorOffset += indexValue * tensor.strides[dim];

    tensor.data[tensorOffset] = value;
