Skip to contentSkip to conversation

realpath.c: Remove trailing slashes

PATCH New Robert Yang 1 participant 1 message Dec 23, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 realpath.c: Remove trailing slashes

Linux system's realpath() remove trailing slashes, but pseudo's doesn't, need make them identical.

E.g., the following code (rel.c) prints '/tmp' with system's realpath, but pseudo's realpath prints '/tmp/':

    #include <stdio.h>
    #include <limits.h>
    #include <stdlib.h>

    int main() {
        char out[PATH_MAX];
        printf("%s\n", realpath("/tmp/", out));
        return 0;
    }
$ bitbake base-passwd -cdevshell # For pseudo env
$ gcc rel.c
$ ./a.out
/tmp/ (but should be /tmp)

This patch fixes the problem.

Upstream-Status: Submitted

---
 ports/unix/guts/realpath.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
ports/unix/guts/realpath.c +8 -1
index 085d2cb..8d8118b 100644
@@ -14,7 +14,14 @@
 		errno = ENAMETOOLONG;
 		return NULL;
 	}
-	if ((len = strlen(rname)) >= pseudo_sys_path_max()) {
+		len = strlen(rname);
+		char *ep = rname + len - 1;
+		while (ep > rname && *ep == '/') {
+			--len;
+			*(ep--) = '\0';
+		}
+
+		if (len >= pseudo_sys_path_max()) {
 		errno = ENAMETOOLONG;
 		return NULL;
 	}
Signature · 1 line
2.7.4