[PATCH] io: ftw: Use state stack instead of recursion (BZ 33882)

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Feb 24 12:12:59 GMT 2026



On 18/02/26 17:18, Adhemerval Zanella wrote:
> The previous implementation of ftw relied on recursion to traverse
> directories (ftw_dir calling process_entry calling ftw_dir).  On
> deep directory trees, this could lead to stack overflow (as exercised
> with the new tst-nftw-bz33882.c test).
> 
> This patch refactors ftw to use an explicit, heap-allocated stack
> to manage directory traversal:
> 
>   * The 'struct ftw_frame' encapsulates the state of a single
>     directory level (directory stream, stat buffer, and path offset).
> 
>   * The new dynamic stack fielf (data::stack) holds pointers to
>     ftw_frame.
> 
>   * The ftw_dir is rewritten to use a loop instead of recursion
>     and integrats the 'process_entry' logic directly into the
>     iterative loop to enable immediate state transitions without
>     function call overhead.
> 
>   * Ensured 'data::dirstreams' pointers remain valid by decoupling
>     them from the resizeable stack logic.
> 
> The patch also cleanups some unused definitions and assumptions
> (like free clobbering errno) and fixes an UB while handling the
> callback function for ftw.
> 
> Checked on x86_64-linux-gnu and i686-linux-gnu.

The Linaro CI triggers some regressions for FTW_SKIP_SUBTREE and
FTW_SKIP_SIBLINGS because it uses a different filesystem than I did
on my testing (which returns the path entries in a different order).
This shows some issues with the strategy I used to track states.  
I will send a new version.


More information about the Libc-alpha mailing list