|
25 | 25 |
|
26 | 26 | #import "SwiftRemoteMirrorLegacyInterop.h"
|
27 | 27 |
|
| 28 | +// dirname() is *not* safe - it may modify the input string; so instead just |
| 29 | +// copy anything up to the last slash. |
| 30 | +char *safe_dirname(const char *path) { |
| 31 | + const char *slash = strrchr(path, '/'); |
| 32 | + if (!slash) |
| 33 | + return strdup("."); |
| 34 | + size_t len = slash - path; |
| 35 | + char *result = (char *)malloc(len + 1); |
| 36 | + memcpy(result, path, len); |
| 37 | + result[len] = '\0'; |
| 38 | + return result; |
| 39 | +} |
28 | 40 |
|
29 | 41 | void *Load(char *path) {
|
30 | 42 | const char *libpath = getenv("DYLD_LIBRARY_PATH");
|
|
34 | 46 | if (libpath && path[0] == '/') {
|
35 | 47 | // If DYLD_LIBRARY_PATH is set, and the path we've been given is absolute,
|
36 | 48 | // then prepend the directory part of the path we've been given to it.
|
37 |
| - const char *libdir = dirname(path); |
38 |
| - size_t pathlen = strlen(libpath); |
| 49 | + char *libdir = safe_dirname(path); |
39 | 50 | size_t dirlen = strlen(libdir);
|
40 |
| - ourlibpath = (char *)malloc(pathlen + dirlen + 2); |
41 |
| - memcpy(ourlibpath, libdir, dirlen); |
42 |
| - ourlibpath[dirlen] = ':'; |
| 51 | + if (asprintf(&ourlibpath, "%s:%s", libdir, oldlibpath) < 0) { |
| 52 | + fprintf(stderr, "Unable to form new DYLD_LIBRARY_PATH!\n"); |
| 53 | + exit(1); |
| 54 | + } |
| 55 | + free(libdir); |
| 56 | + |
43 | 57 | oldlibpath = ourlibpath + dirlen + 1;
|
44 |
| - memcpy(ourlibpath + dirlen + 1, libpath, pathlen + 1); |
45 | 58 |
|
46 | 59 | setenv("DYLD_LIBRARY_PATH", ourlibpath, 1);
|
47 | 60 | }
|
|
0 commit comments