readlink: use xmalloc_realpath()
authorJeremie Koenig <jk@jk.fr.eu.org>
Thu, 27 May 2010 13:32:19 +0000 (15:32 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 18 Jul 2010 22:43:05 +0000 (00:43 +0200)
Using realpath() directly with a non-NULL output buffer is unsafe because its
behavior is unspecified on systems which don't have PATH_MAX (ie. Hurd)

I beleive this also fixes a small bug whereby 'buf' would not be freed
on 'readlink -v' with ENABLE_FEATURE_CLEANUP.

Signed-off-by: Jeremie Koenig <jk@jk.fr.eu.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
coreutils/readlink.c

index 20df38b967a151917a43f89bf8ea07321c5ef05e..2ed5e2cac05fd39df8e5954babd2d4b80ada8898 100644 (file)
@@ -36,7 +36,6 @@ int readlink_main(int argc UNUSED_PARAM, char **argv)
 {
        char *buf;
        char *fname;
-       char pathbuf[PATH_MAX];
 
        IF_FEATURE_READLINK_FOLLOW(
                unsigned opt;
@@ -56,7 +55,7 @@ int readlink_main(int argc UNUSED_PARAM, char **argv)
                logmode = LOGMODE_NONE;
 
        if (opt & 1) { /* -f */
-               buf = realpath(fname, pathbuf);
+               buf = xmalloc_realpath(fname);
        } else {
                buf = xmalloc_readlink_or_warn(fname);
        }
@@ -65,7 +64,7 @@ int readlink_main(int argc UNUSED_PARAM, char **argv)
                return EXIT_FAILURE;
        printf((opt & 2) ? "%s" : "%s\n", buf);
 
-       if (ENABLE_FEATURE_CLEAN_UP && !opt)
+       if (ENABLE_FEATURE_CLEAN_UP)
                free(buf);
 
        fflush_stdout_and_exit(EXIT_SUCCESS);