From 1270abb67db24ee6c22ca4d63f526135f836f458 Mon Sep 17 00:00:00 2001 From: Rene Rivera Date: Mon, 11 Apr 2022 19:56:42 -0500 Subject: [PATCH] Debug CI crash 11/n [skip ci] Route all stack cacls through new nth method. And use array indexing to compute the offsets instead of possibly UB pointer math. --- src/engine/function.cpp | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/engine/function.cpp b/src/engine/function.cpp index 7c87dbb1a..22f7a2ef2 100644 --- a/src/engine/function.cpp +++ b/src/engine/function.cpp @@ -313,10 +313,7 @@ struct _stack template remove_cref_t & top(int i = 0) const { - assert( ((ptrdiff_t)data) > (1<<4) ); - void * data_n = reinterpret_cast*>(data) + i; - assert( ((ptrdiff_t)data_n) > (1<<4) ); - return *reinterpret_cast( data_n ); + return *reinterpret_cast( nth( i ) ); } // Get a pointer to the last A-th item skipping over any A pre i-th items. @@ -324,7 +321,7 @@ struct _stack remove_cref_t< select_last_t > * get() const { using U = remove_cref_t< select_last_t >; - return reinterpret_cast( advance(data) ); + return static_cast( advance(data) ); } // Move "v" to a new slot in ther stack. Returns a reference to the new item. @@ -341,10 +338,7 @@ struct _stack remove_cref_t pop() { using U = remove_cref_t; - assert( ((ptrdiff_t)this) > (1<<4) ); - auto v = top(); - assert( ((ptrdiff_t)&v) > (1<<4) ); - U result = v; + U result = top(); pop( 1 ); return result; } @@ -353,10 +347,8 @@ struct _stack template void pop( int32_t n ) { - using U = remove_cref_t; check_alignment(); - U* u = reinterpret_cast( data ); - data = u + n; + data = nth( n ); check_alignment(); --cleanups_size; } @@ -376,9 +368,6 @@ struct _stack std::array cleanups; size_t cleanups_size = 0; - template - void do_cleanup(int32_t) {} - struct list_alignment_helper { char ch; @@ -393,6 +382,14 @@ struct _stack assert( (size_t)data % LISTPTR_ALIGN == 0 ); } + template + remove_cref_t * nth( int32_t n ) const + { + using U = remove_cref_t; + assert( ((ptrdiff_t)data) > (1<<4) ); + return &( static_cast( data )[n] ); + } + template struct advance_size { @@ -427,7 +424,7 @@ struct _stack template static void cleanup_item(_stack * s, int32_t n, T*_=nullptr) { - s->data = (char *)s->data + ( n * sizeof(remove_cref_t) ); + s->data = s->nth( n ); s->check_alignment(); } @@ -442,7 +439,7 @@ void _stack::cleanup_item(_stack * s, int32_t n, LIST**) { list_free( s->top(i) ); } - s->data = (char *)s->data + ( n * sizeof(remove_cref_t) ); + s->data = s->nth( n ); s->check_alignment(); } @@ -457,11 +454,11 @@ remove_cref_t * _stack::push( T v, int32_t n ) { using U = remove_cref_t; check_alignment(); - data = (char *)data - ( n * sizeof(U) ); + data = nth( -n ); check_alignment(); - std::uninitialized_fill_n( reinterpret_cast( data ), n, v ); + std::uninitialized_fill_n( static_cast( data ), n, v ); cleanup_push( n ); - return reinterpret_cast( data ); + return static_cast( data ); } template