From 6390b11db5fc33b14f0087449ba3c608688c2925 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Sat, 4 Jan 2025 11:11:15 +0100 Subject: [PATCH] corec: use a char for the array pointer It is less flexible than void* be can still be casted to other types more loosely than other types. It allows finding bogus cast alignment that void* doesn't see. --- corec/corec/array/array.c | 10 +++++----- corec/corec/array/array.h | 4 ++-- corec/corec/memheap.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/corec/corec/array/array.c b/corec/corec/array/array.c index 2c91f039..361e6959 100644 --- a/corec/corec/array/array.c +++ b/corec/corec/array/array.c @@ -350,9 +350,9 @@ static void SlowSort(array* p, size_t Count, size_t Width, arraycmp Cmp, const v #else uint8_t Tmp[Width]; #endif - uint8_t* End = ARRAYBEGIN(*p,uint8_t) + Count*Width; - uint8_t* i; - uint8_t* j; + char* End = ARRAYBEGIN(*p,char) + Count*Width; + char* i; + char* j; j = p->_Begin; for (i=j+Width; i!=End; i+=Width) @@ -387,7 +387,7 @@ static void SlowSort(array* p, size_t Count, size_t Width, arraycmp Cmp, const v memcpy(j,i,Width); } } - p->_Used = j - (uint8_t*)p->_Begin + Width; + p->_Used = j - p->_Begin + Width; } } @@ -488,7 +488,7 @@ intptr_t ArrayFindEx(const array* p, size_t Count, size_t Width, const void* Dat else { intptr_t No = 0; - const uint8_t* i; + const char* i; for (i=p->_Begin;Count--;i+=Width,++No) if (memcmp(i,Data,Width)==0) { diff --git a/corec/corec/array/array.h b/corec/corec/array/array.h index 894a74bf..c9f1d83a 100644 --- a/corec/corec/array/array.h +++ b/corec/corec/array/array.h @@ -27,7 +27,7 @@ typedef struct cc_memheap cc_memheap; typedef struct array { // these are private members, use ARRAY macros to access them - void* _Begin; + char* _Begin; size_t _Used; } array; @@ -61,7 +61,7 @@ ARRAY_DLL void ArrayDelete(array* p, size_t Ofs, size_t Length); #define ArraySort(p,type,Cmp,CmpParam,Unique) ArraySortEx(p,ARRAYCOUNT(*p,type),sizeof(type),Cmp,CmpParam,Unique) #ifdef CONFIG_DEBUGCHECKS -#define ARRAYBEGIN(Array,Type) (assert(&(Array)!=NULL),(Type*)((Array)._Begin)) +#define ARRAYBEGIN(Array,Type) (assert(&(Array)!=NULL),(Type*)(void*)((Array)._Begin)) #define ARRAYEMPTY(Array) (assert(&(Array)!=NULL),(Array)._Used==0) #define ARRAYCOUNT(Array,Type) (assert(&(Array)!=NULL),(((Array)._Used))/sizeof(Type)) #else diff --git a/corec/corec/memheap.h b/corec/corec/memheap.h index 60d2de83..41d83391 100644 --- a/corec/corec/memheap.h +++ b/corec/corec/memheap.h @@ -24,7 +24,7 @@ typedef void (*memheap_write)(const void* This,void*,const void* Src,size_t Pos, #define ARRAY_POINTER_HOLDER \ size_t Size; \ - alignas(max_align_t) uint8_t data[] + alignas(max_align_t) char data[] // we can't use a type with a flexible array inside another structure, so we // use the same define everywhere