From 830009b890421fb9b456069592eebbae4a08c019 Mon Sep 17 00:00:00 2001 From: CaldurG Date: Thu, 26 Nov 2026 08:43:35 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20NULL=20pointer=20in=20SD?= =?UTF-8?q?=20sort=20without=20name=20caching=20(#28909)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With SDSORT_USES_RAM minus SDSORT_CACHE_NAMES and SDSORT_USES_STACK, the sortnames pointer is declared (as its declaration covers DISABLED(SDSORT_USES_STACK)) but the constructor only assigned it under ENABLED(SDSORT_CACHE_NAMES). presort() then writes through the NULL pointer via SET_SORTNAME, hard-faulting the board (e.g. LPC1768) as soon as an SD card with two or more files is mounted. Assign sortnames whenever it is declared and not heap-allocated: whenever SDSORT_DYNAMIC_RAM is off and either SDSORT_CACHE_NAMES is on or SDSORT_USES_STACK is off. This also skips the wasted static allocation when SDSORT_DYNAMIC_RAM applies. Add a build test (LPC1768/config-07) covering the crashing combo. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Marlin/src/sd/cardreader.cpp | 16 +++++++--------- buildroot/tests/LPC1768/config-07.ini | 13 +++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 buildroot/tests/LPC1768/config-07.ini diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index 57d7ba3503..7c48e83d33 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -163,15 +163,13 @@ CardReader::CardReader() { static uint8_t sort_order_static[SDSORT_LIMIT]; sort_order = sort_order_static; #endif - #if ENABLED(SDSORT_CACHE_NAMES) - #if DISABLED(SDSORT_DYNAMIC_RAM) - static char sortshort_static[SDSORT_LIMIT][FILENAME_LENGTH]; - sortshort = sortshort_static; - #endif - #if !ALL(SDSORT_DYNAMIC_RAM, SDSORT_USES_STACK) - static char sortnames_static[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE]; - sortnames = sortnames_static; - #endif + #if ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM) + static char sortshort_static[SDSORT_LIMIT][FILENAME_LENGTH]; + sortshort = sortshort_static; + #endif + #if ENABLED(SDSORT_USES_RAM) && DISABLED(SDSORT_DYNAMIC_RAM) && (ENABLED(SDSORT_CACHE_NAMES) || DISABLED(SDSORT_USES_STACK)) + static char sortnames_static[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE]; + sortnames = sortnames_static; #endif sort_count = 0; diff --git a/buildroot/tests/LPC1768/config-07.ini b/buildroot/tests/LPC1768/config-07.ini new file mode 100644 index 0000000000..e53477c448 --- /dev/null +++ b/buildroot/tests/LPC1768/config-07.ini @@ -0,0 +1,13 @@ +# +# Marlin Firmware +# Test: LPC1768/config-07.ini +# +# MKS SBASE with SD sorting using RAM without name caching. +# + +[config:base] +motherboard = BOARD_MKS_SBASE +reprap_discount_smart_controller = on +sdsupport = on +sdcard_sort_alpha = on +sdsort_uses_ram = true