From dcd6c58394be65804b75251aef2377a74a66731a Mon Sep 17 00:00:00 2001 From: ToyB-Chan Date: Thu, 5 Nov 2026 17:57:07 +0100 Subject: [PATCH] fixed some bugs (tm) --- minimal_heap.h | 4 ---- minimal_heap_implementation.inl | 17 +++++++++-------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/minimal_heap.h b/minimal_heap.h index da5cf1b..0b0c0c1 100644 --- a/minimal_heap.h +++ b/minimal_heap.h @@ -8,10 +8,6 @@ #include #include -#ifndef MIHP_IMPLEMENTATION - #define MIHP_IMPLEMENTATION 0 -#endif - /* Defines to be configured by the library user */ #ifndef MIHP_ASSERT #define MIHP_ASSERT(Condition) while(1) { if (Condition) break; *(volatile char*)(0x0) = 0; } diff --git a/minimal_heap_implementation.inl b/minimal_heap_implementation.inl index 5721840..391557b 100644 --- a/minimal_heap_implementation.inl +++ b/minimal_heap_implementation.inl @@ -24,7 +24,7 @@ MIHP_HeapConfig MIHP_MakeDefaultConfigPreset() cfg.AllocateFillValue = 0xbe; cfg.FreeFillValue = 0xee; - cfg.HeapValidationMode = MIHP_HVM_MagicNumber; + cfg.HeapValidationMode = MIHP_HVM_None; return cfg; } @@ -43,6 +43,12 @@ MIHP_HeapError MIHP_InitializeHeap(MIHP_Heap* heap, MIHP_HeapConfig config) if (config.PlatformFreeMemoryFn == NULL) return MIHP_HE_BadConfig; + if (config.OnHeapCorruptionDetectedFn == NULL && config.HeapValidationMode != MIHP_HVM_None) + return MIHP_HE_BadConfig; + + if (config.MinMemoryAreaSize < MIHP_GetMinimalPayloadSize(heap) * 2) + return MIHP_HE_BadConfig; + if (config.MaxHeapSize < config.MinMemoryAreaSize) return MIHP_HE_BadConfig; @@ -260,7 +266,7 @@ MIHP_HeapResult MIHP_Reallocate(MIHP_Heap* heap, void* ptr, size_t newSize) segment->Checksum = MIHP_GenerateHeapSegmentChecksum(heap, segment); MIHP_UnlockHeap(heap); - return MIHP_MAKE_RESULT(NULL, MIHP_HE_Success); + return MIHP_MAKE_RESULT(ptr, MIHP_HE_Success); } if (segment->NextSegment) @@ -402,6 +408,7 @@ MIHP_HeapError MIHP_SetPointerCustomMetadata(MIHP_Heap* heap, void* ptr, MIHP_Al MIHP_HeapSegmentHeader* segment = MIHP_GetHeapSegmentHeaderPtr(heap, ptr); MIHP_ValidateHeapSegmentHeader(heap, segment); segment->CustomMetadata = metadata; + segment->Checksum = MIHP_GenerateHeapSegmentChecksum(heap, segment); return MIHP_HE_Success; } @@ -829,9 +836,6 @@ uint32_t MIHP_GenerateHeapSegmentChecksum(const MIHP_Heap* heap, const MIHP_Heap void MIHP_ValidateHeapMemoryAreaHeader(const MIHP_Heap* heap, const MIHP_HeapMemoryAreaHeader* area) { - if (heap->Config.OnHeapCorruptionDetectedFn == NULL) - return; - uint32_t newChecksum; if (heap->Config.HeapValidationMode == MIHP_HVM_None) @@ -858,9 +862,6 @@ void MIHP_ValidateHeapMemoryAreaHeader(const MIHP_Heap* heap, const MIHP_HeapMem void MIHP_ValidateHeapSegmentHeader(const MIHP_Heap* heap, const MIHP_HeapSegmentHeader* segment) { - if (heap->Config.OnHeapCorruptionDetectedFn == NULL) - return; - uint32_t newChecksum; if (heap->Config.HeapValidationMode == MIHP_HVM_None)