On Sat, 08 Jan 2022 15:36:46 -0500
Paul Smith <[email protected]> wrote:
> On Sat, 2022-01-08 at 19:47 +0100, Henrik Carlqvist wrote:
> > But now, with both userend and pwent set it seems as if the calling
> > function will have its const string modified. If this final case were
> > fixed at least no calling function would suffer from a modified const
> > string.
> I'm not sure what you mean here. It is never the case that the
> incoming string (name) is ever modified under any circumstances, as far
> as the calling function is concerned.
I mean this code, where name is a const char * which was an input variable to
the function:
# if !defined(_AMIGA) && !defined(WINDOWS32)
else
{
struct passwd *pwent;
char *userend = strchr (name + 1, '/');
if (userend != 0)
*userend = '\0'; <--- ** Here userend modifies the content
pwent = getpwnam (name + 1);
if (pwent != 0)
{
if (userend == 0)
return xstrdup (pwent->pw_dir);
else
return xstrdup (concat (3, pwent->pw_dir, "/", userend + 1)); <--X
}
else if (userend != 0)
*userend = '/'; <--- ** Here userend restores the content
}
# endif /* !AMIGA && !WINDOWS32 */
> If the incoming string needs to expanded then a new string is allocated
> and returned from this function containing the expansion. If the
> incoming string is not expanded, then no new string is allocated and 0
> (null pointer) is returned.
But what about the case marked with <--X above? To me it seems as if the
function returns after having modified const char *name bu using userend.
Best regards Henrik