Skip to content

Commit fa8ffb7

Browse files
committed
Add smarter recursion limit checking
1 parent d6b3e2e commit fa8ffb7

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/asar/asar.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ virtual_file_error asar_get_last_io_error();
6060
extern volatile int recursioncount;
6161
extern int pass;
6262

63+
size_t check_stack_left();
64+
6365
class recurseblock {
6466
public:
6567
recurseblock()
6668
{
6769
recursioncount++;
68-
if (recursioncount > 1000) asar_throw_error(pass, error_type_fatal, error_id_recursion_limit);
70+
if(check_stack_left() < 32768 || recursioncount > 10000)
71+
asar_throw_error(pass, error_type_fatal, error_id_recursion_limit);
6972
}
7073
~recurseblock()
7174
{

src/asar/platform/generic/thread-helpers-pthread.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,18 @@ bool run_as_thread(functor&& callback) {
5555

5656
return wrapper.result;
5757
}
58+
59+
size_t check_stack_left() {
60+
pthread_attr_t attrs;
61+
void *stack_start;
62+
size_t stack_size;
63+
64+
pthread_getattr_np(pthread_self(), &attrs);
65+
pthread_attr_getstack(&attrs, &stack_start, &stack_size);
66+
pthread_attr_destroy(&attrs);
67+
68+
// using a random local as a rough estimate for current top-of-stack
69+
size_t stack_left = (char*)&stack_size - (char*)stack_start;
70+
return stack_left;
71+
if(stack_left < 32768) asar_throw_error(pass, error_type_fatal, error_id_recursion_limit);
72+
}

src/asar/platform/windows/thread-helpers-win32.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,11 @@ bool run_as_thread(functor&& callback) {
8282

8383
return wrapper.result;
8484
}
85+
86+
size_t check_stack_left() {
87+
void *stack_low, *stack_high;
88+
GetCurrentThreadStackLimits(&stack_low, &stack_high);
89+
size_t stack_left = (char*)&stack_low - (char*)stack_low;
90+
return stack_left;
91+
}
8592
#endif

0 commit comments

Comments
 (0)