coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] stat,df: remove an unnecessary alloca


From: Collin Funk
Subject: Re: [PATCH] stat,df: remove an unnecessary alloca
Date: Sun, 01 Feb 2026 14:32:24 -0800
User-agent: Gnus/5.13 (Gnus v5.13)

Pádraig Brady <P@draigBrady.com> writes:

> On 01/02/2026 20:34, Collin Funk wrote:
>> I couldn't get this to overflow the stack, since passing a large file
>> name will run into ENAMETOOLONG errors before find_mount_point is
>> executed. But as far as I can tell, this strdupa call was never needed.
>
> Yes the explicit free() calls aren't too onerous here,
> as the "dir" is only used within this else clause.

You could increase the scope of dir, and initialize it to NULL, so that
calling free is always safe. I.e., written like this:

    extern char *
    find_mount_point (char const *file, struct stat const *file_stat)
    {
      char *dir = NULL;
      if (...)
        {
          /* This branch doesn't use DIR.  */
        }
      else
        {
          dir = dir_name (file);

          if (...)
            {
              /* Some error occurs.  */
              goto done;
            }
        }

    done:
      free (dir)
      /* ...  */
    }

But I find reducing the scope increases clarity more than reducing the
number of calls to free.

> I wouldn't change for this release though,
> just in case.

Yep, I was thinking the same. I'll push it after.

Collin



reply via email to

[Prev in Thread] Current Thread [Next in Thread]