From f819144265f1766d58f4ff23b6fe8456f1326488 Mon Sep 17 00:00:00 2001 From: AlCalzone Date: Mon, 15 Apr 2024 14:20:33 +0200 Subject: [PATCH] fix: infinite loop fragmenting large NVM objects with 8 bytes remaining (#6769) --- packages/nvmedit/src/nvm3/nvm.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/nvmedit/src/nvm3/nvm.ts b/packages/nvmedit/src/nvm3/nvm.ts index 149875483ef9..8646a9e3ad05 100644 --- a/packages/nvmedit/src/nvm3/nvm.ts +++ b/packages/nvmedit/src/nvm3/nvm.ts @@ -5,6 +5,7 @@ import { FLASH_MAX_PAGE_SIZE_700, FLASH_MAX_PAGE_SIZE_800, NVM3_COUNTER_SIZE, + NVM3_OBJ_HEADER_SIZE_LARGE, NVM3_OBJ_HEADER_SIZE_SMALL, NVM3_PAGE_HEADER_SIZE, NVM3_WORD_SIZE, @@ -212,6 +213,11 @@ export function encodeNVM( || obj.type === ObjectType.DataLarge ) { // Large objects may be fragmented + + // We need to start a new page, if the remaining space is not enough for + // the object header plus additional data + if (remainingSpace <= NVM3_OBJ_HEADER_SIZE_LARGE) nextPage(); + fragments = fragmentLargeObject( obj as any, remainingSpace,