Subject: sed -i --follow-symlinks fails in i586 with large paths

From: [email-masked]
Date: Wed, 20 Nov 2024 14:11:23 +0100

I've detected that sed fails when trying to replace in place, following
symlinks in 32 bits architecture.

Steps to reproduce:

 * create a long random path with a file and a relative link:
   mkdir -p /tmp/sed/aaaa[...]
   touch /tmp/sed/aaaa[...]/f1
   ln -s f1 f2
   mv f2 /tmp/sed/aaaa[...]/

Then sed fails when trying to replace:

$ sed -i --follow-symlinks 's/a/a/' /tmp/sed/aaaa[...]/f2 
sed: cannot rename /tmp/sed/aaaa[...]/sed5hPvOi: Not a directory

I've tested this with a full path of 100 chars.

Debugging the code I can see that the problem should be in the
"utils.c:follow_symlink" function, but I was unable to find the exact
reason and a fix.

It should be related to the "if (buf_size <= newlen)" because when it
fails, this is false, "linklen == 0" and the "newlen" value is the length
of the dirname, so it returns the "dirname" of the target link file.