Skip to content

Commit 11816af

Browse files
Dmytro Dzhulgakovfacebook-github-bot
authored andcommitted
Safety check for negative alloc_cpu() attempt (pytorch#17071)
Summary: Some legacy TH code was relying on alloc to throw when called with negative number!!! E.g. `torch.linspace(0, 1, -1)`. And it breaks ASAN build. I still believe alloc should receive size_t, but I added a safety enforce inside. It should fix ASAN. I'll follow up with a proper fix for empty_cpu (which is probably the right place to do it) separately Pull Request resolved: pytorch#17071 Differential Revision: D14074157 Pulled By: dzhulgakov fbshipit-source-id: 3ed3bdb873e446edecb558e1df491310fd7179e3
1 parent f0fed41 commit 11816af

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

c10/core/CPUAllocator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ void* alloc_cpu(size_t nbytes) {
4141
if (nbytes == 0) {
4242
return nullptr;
4343
}
44+
// We might have clowny upstream code that tries to alloc a negative number
45+
// of bytes. Let's catch it early.
46+
CAFFE_ENFORCE(
47+
((ptrdiff_t)nbytes) >= 0,
48+
"alloc_cpu() seems to have been called with negative number: ", nbytes);
4449

4550
void* data;
4651
#ifdef __ANDROID__

0 commit comments

Comments
 (0)