Skip to content

Commit c4c9fb3

Browse files
committed
Added checks for the malloc call results in operator new.
The operator new is not supposed to return null pointers, so throw bad_alloc when malloc returns null. This should avoid segmentation faults if malloc fails.
1 parent f459ec9 commit c4c9fb3

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

test/threads/thread/constr/FArgs_pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ void* operator new(std::size_t s) throw (std::bad_alloc)
4040
//std::cout << __FILE__ << ":" << __LINE__ << std::endl;
4141
if (throw_one == 0) throw std::bad_alloc();
4242
--throw_one;
43-
return std::malloc(s);
43+
void* p = std::malloc(s);
44+
if (!p) throw std::bad_alloc();
45+
return p;
4446
}
4547

4648
#if defined BOOST_MSVC

test/threads/thread/constr/F_pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ void* operator new(std::size_t s) throw (std::bad_alloc)
4040
//std::cout << __FILE__ << ":" << __LINE__ << std::endl;
4141
if (throw_one == 0) throw std::bad_alloc();
4242
--throw_one;
43-
return std::malloc(s);
43+
void* p = std::malloc(s);
44+
if (!p) throw std::bad_alloc();
45+
return p;
4446
}
4547

4648
#if defined BOOST_MSVC

test/threads/thread/constr/lambda_pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ void* operator new(std::size_t s) throw (std::bad_alloc)
4141
{
4242
if (throw_one == 0) throw std::bad_alloc();
4343
--throw_one;
44-
return std::malloc(s);
44+
void* p = std::malloc(s);
45+
if (!p) throw std::bad_alloc();
46+
return p;
4547
}
4648

4749
#if defined BOOST_MSVC

0 commit comments

Comments
 (0)