ref: 607319bed9f2d7f2ea7ff107ebf51de6c27396d7
parent: 3aef5b01a473555c92bba327ede73960daec22a8
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Jul 10 14:06:19 EDT 2023
realpath: handle non-absolute path resolving when open/fd2path fails
--- a/libnpe/realpath.c
+++ b/libnpe/realpath.c
@@ -6,7 +6,7 @@
realpath(char *path, char *buffer)
{
char *s, p[PATH_MAX];
- int f;
+ int f, pathlen, n;
s = nil;
if((f = open(path, OREAD)) >= 0){
@@ -14,9 +14,18 @@
s = buffer == nil ? strdup(p) : strcpy(buffer, p);
close(f);
}
- if(s == nil && strlen(path) < sizeof(p)){
- strcpy(buffer, path);
- s = cleanname(buffer);
+ if(s == nil){
+ if(path[0] != '/'){
+ pathlen = strlen(path);
+ if(getwd(p, sizeof(p)) == nil || (n = strlen(p))+1+pathlen >= sizeof(p))
+ return nil;
+ p[n++] = '/';
+ strcpy(p+n, path);
+ path = p;
+ }else if(strlen(path) >= sizeof(p))
+ return nil;
+
+ s = cleanname(buffer == nil ? strdup(path) : strcpy(buffer, path));
}
return s;