@@ -444,20 +444,19 @@ tstate_set_stack(PyThreadState *tstate,
444444 void * stack_start_addr , size_t stack_size )
445445{
446446 assert (stack_size > 0 );
447+ assert (stack_size >= (_PyOS_STACK_MARGIN_BYTES * 3 ));
447448
448449 _PyThreadStateImpl * ts = (_PyThreadStateImpl * )tstate ;
449450 ts -> c_stack_hard_limit = (uintptr_t )stack_start_addr ;
450451 ts -> c_stack_top = (uintptr_t )stack_start_addr + stack_size ;
451452
452453 uintptr_t soft_limit = ts -> c_stack_hard_limit ;
453- if (stack_size >= _PyOS_STACK_MARGIN_BYTES ) {
454454#ifdef _Py_THREAD_SANITIZER
455- // Thread sanitizer crashes if we use a bit more than half the stack.
456- soft_limit += (stack_size / 2 );
455+ // Thread sanitizer crashes if we use a bit more than half the stack.
456+ soft_limit += (stack_size / 2 );
457457#else
458- soft_limit += _PyOS_STACK_MARGIN_BYTES ;
458+ soft_limit += _PyOS_STACK_MARGIN_BYTES ;
459459#endif
460- }
461460 ts -> c_stack_soft_limit = soft_limit ;
462461
463462 // Sanity checks
477476PyUnstable_ThreadState_SetStack (PyThreadState * tstate ,
478477 void * stack_start_addr , size_t stack_size )
479478{
480- if (stack_size == 0 ) {
481- PyErr_SetString (PyExc_ValueError , "stack_size must be greater than 0" );
479+ if (stack_size < (_PyOS_STACK_MARGIN_BYTES * 3 )) {
480+ PyErr_Format (PyExc_ValueError ,
481+ "stack_size must be at least %zu bytes" ,
482+ _PyOS_STACK_MARGIN_BYTES * 3 );
482483 return -1 ;
483484 }
484485
0 commit comments