From: Karel Zak Date: Thu, 17 Jun 2021 11:25:43 +0000 (+0200) Subject: rename: use readlink() in more robust way X-Git-Tag: v2.38-rc1~445 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=538f010c8f9475d228b8312c319b36c09bfaca21;p=thirdparty%2Futil-linux.git rename: use readlink() in more robust way Reported-by: Jan Pazdziora Signed-off-by: Karel Zak --- diff --git a/misc-utils/rename.c b/misc-utils/rename.c index 0f0d883224..fbabfe2841 100644 --- a/misc-utils/rename.c +++ b/misc-utils/rename.c @@ -105,6 +105,7 @@ static int do_symlink(char *from, char *to, char *s, int verbose, int noact, { char *newname = NULL, *target = NULL; int ret = 1; + ssize_t ssz; struct stat sb; if ( faccessat(AT_FDCWD, s, F_OK, AT_SYMLINK_NOFOLLOW) != 0 && @@ -125,12 +126,15 @@ static int do_symlink(char *from, char *to, char *s, int verbose, int noact, return 2; } target = xmalloc(sb.st_size + 1); - if (readlink(s, target, sb.st_size + 1) < 0) { + + ssz = readlink(s, target, sb.st_size + 1); + if (ssz < 0) { warn(_("%s: readlink failed"), s); free(target); return 2; } - target[sb.st_size] = '\0'; + target[ssz] = '\0'; + if (string_replace(from, to, target, target, &newname) != 0) ret = 0;