Re build problem with discarding const in tilde_expand():
I worked out that removing const upwards is not needed - the behaviour can
be amended. Perhaps alloca would be offer more performance than this but
I'm really just trying to move on. This just uses xstrdup to copy the
string that is being modified for the sake of it being convenient for
parsing;
diff --git a/src/read.c b/src/read.c
index 75e37743..ff125d32 100644
--- a/src/read.c
+++ b/src/read.c
@@ -3065,20 +3065,31 @@ tilde_expand (const char *name)
else
{
struct passwd *pwent;
- char *userend = strchr (name + 1, '/');
+ char *namecopy = xstrdup(name);
+ char *userend = strchr (namecopy + 1, '/');
+
if (userend != 0)
*userend = '\0';
- pwent = getpwnam (name + 1);
+ pwent = getpwnam (namecopy + 1);
if (pwent != 0)
{
+ char *result = 0;
if (userend == 0)
- return xstrdup (pwent->pw_dir);
-
- *userend = '/';
- return xstrdup (concat (3, pwent->pw_dir, "/", userend + 1));
+ {
+ result = xstrdup (pwent->pw_dir);
+ }
+ else
+ {
+ *userend = '/';
+ result = xstrdup (concat (3, pwent->pw_dir, "/", userend +
1));
+ }
+ free(namecopy);
+ return result;
}
else if (userend != 0)
*userend = '/';
+
+ free(namecopy);
}
# endif /* !MK_OS_W32 */
#endif /* !MK_OS_VMS */
Regards,
Tim
On Wed, 16 Sept 2026 at 13:30, Tim Murphy <[email protected]> wrote:
> FWIW I am getting this error with gcc (GCC) 16.2.1 20260810 on arch linux
> against head (commit b3802782de3eff2c0f1eda9e7c0befd8cd142162)
>
> src/read.c:3068:23: error: initialization discards ‘const’ qualifier from
> pointer target type [-Werror=discarded-qualifiers]
> 3068 | char *userend = strchr (name + 1, '/');
>
>
> The code in tilde_expand() is doing this;
> char *userend = strchr (name + 1, '/');
> if (userend != 0)
> *userend = '\0';
>
> "name" appears to be a const and the code is essentially bypassing this
> with "userend."
>
> As I undo this going upwards it causes quite a lot of nuisance but I
> assume we need to be very careful about changing behavior - where e.g.
> makefilenames are permanently altered - so the correct behavior is to call
> a spade a spade and "un-const" things.
>
> I can post a patch but someone else might prefer to do this.
>
> Regards,
>
> Tim
>