Skip to content

Commit f98c795

Browse files
zou3519soumith
authored andcommitted
Fix use after free (#4559)
In `THPTensor_(_convertToTensorIndexers)`, a `vector<THPIndexTensor>` is created by constructing `THPTensor`s from sequences/tensors/etc. Each `THPIndexTensor` is then freed with the following: ``` for (auto& idx : indexers) { THIndexTensor_(free)(LIBRARY_STATE idx->cdata); Py_DECREF(idx); } ``` This is a problem because `Py_DECREF(idx)` will turn `idx->ob_refcnt` to 0 since this function created the relevant `THPIndexTensor`s and owns them, causing `THPTensor_(dealloc)` to be called. `THPTensor_(dealloc)` already has a line that calls `THIndexTensor_(free)(LIBRARY_STATE idx->cdata)`. So `THIndexTensor_(free)(LIBRARY_STATE idx->cdata)` gets called twice on the same `cdata`. After the first call frees `cdata`, the second attempts to access flags/members of `cdata` to determine if it should free it.
1 parent 24a4881 commit f98c795

File tree

1 file changed

+0
-4
lines changed

1 file changed

+0
-4
lines changed

torch/csrc/generic/Tensor.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,6 @@ static bool THPTensor_(_convertToTensorIndexers)(
829829

830830
// Clean up Indexers
831831
for (auto& idx : indexers) {
832-
THIndexTensor_(free)(LIBRARY_STATE idx->cdata);
833832
Py_DECREF(idx);
834833
}
835834
return false;
@@ -890,7 +889,6 @@ static bool THPTensor_(_convertToTensorIndexers)(
890889

891890
// Clean up Indexers
892891
for (auto& idx : indexers) {
893-
THIndexTensor_(free)(LIBRARY_STATE idx->cdata);
894892
Py_DECREF(idx);
895893
}
896894

@@ -909,15 +907,13 @@ static bool THPTensor_(_convertToTensorIndexers)(
909907

910908
// Clean up Indexers
911909
for (auto& idx : indexers) {
912-
THIndexTensor_(free)(LIBRARY_STATE idx->cdata);
913910
Py_DECREF(idx);
914911
}
915912
return false;
916913
}
917914

918915
// Clean up Indexers
919916
for (auto& idx : indexers) {
920-
THIndexTensor_(free)(LIBRARY_STATE idx->cdata);
921917
Py_DECREF(idx);
922918
}
923919
return true;

0 commit comments

Comments
 (0)