RTEMS | cpukit/libblock: Fix resource leak in rtems_fdisk_initialize (!903)

MITHILESH MATTAPALLI (@mithileshm) gitlab at rtems.org
Fri Jan 23 07:12:11 UTC 2026




MITHILESH MATTAPALLI commented on a discussion on cpukit/libblock/src/flashdisk.c: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/903#note_139729

 >       */
 >      fd->copy_buffer = malloc (c->block_size);
 >      if (!fd->copy_buffer)
 > -      return RTEMS_NO_MEMORY;
 > +    {
 > +      sc = RTEMS_NO_MEMORY;
 > +      goto error_cleanup;

The cleanup logic required here is non-trivial: it must iterate over all previously initialized devices (minor \> 0), unlink them from the file system, destroy their mutexes, and free multiple nested pointers (segments, devices, blocks, copy_buffer) to prevent memory leaks and system crashes.

Since there are multiple distinct allocation points that can fail (copy_buffer, blocks, devices, segments), avoiding goto would require duplicating this extensive cleanup logic inside every single error check. That would significantly increase code size and maintenance burden. Using goto error_cleanup centralizes the teardown logic (DRY), keeping the initialization flow linear and readable.

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/903#note_139729
You're receiving this email because of your account on gitlab.rtems.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/bugs/attachments/20260123/197bdaab/attachment.htm>


More information about the bugs mailing list