Skip to content
Myr

Make search path expansion portable (fix #817)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This is part 2 of the fix to #817. Part 1 is the previous commit.

authored by Nicolas Williams · committed by David Tolnay · parent 10df52e · Browse files · 1 file+45 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+45 added−15 removed

  • other:+45 added−15 removed

1 file+45 added−15 removed

modified:linker.c+45 added-15 removed
23 unmodified lines
24static int load_library(jq_state *jq, jv lib_path, int is_data, int raw,25 const char *as, block *out_block,26 struct lib_loading_state *lib_state);2728static int path_is_relative(jv p) {29 const char *s = jv_string_value(p);3031#ifdef WIN3232 int res = PathIsRelativeA(s);33#else34 int res = *s != '/';35#endif36 jv_free(p);37 return res;38}394041// Given a lib_path to search first, creates a chain of search paths42// in the following order:43// 1. lib_path44// 2. -L paths passed in on the command line (from jq_state*) or builtin list45static jv build_lib_search_chain(jq_state *jq, jv search_path, jv jq_origin, jv lib_origin) {46 assert(jv_get_kind(search_path) == JV_KIND_ARRAY);47 jv expanded = jv_array();48 jv expanded_elt;49 jv err = jv_null();50 jv_array_foreach(search_path, i, path) {51 if (jv_get_kind(path) != JV_KIND_STRING) {52 jv_free(path);53 continue;54 }55 path = expand_path(path);56 if (!jv_is_valid(path)) {57 err = path;58 path = jv_null();59 continue;60 }61 if (strcmp(".",jv_string_value(path)) == 0) {62 expanded_elt = jv_copy(path);40 } else if (jv_get_kind(lib_origin) == JV_KIND_STRING &&41 strncmp("./", jv_string_value(path),sizeof("./")-1) == 0) {63 } else if (strncmp("$ORIGIN/",jv_string_value(path),sizeof("$ORIGIN/") - 1) == 0) {64 expanded_elt = jv_string_fmt("%s/%s",43 jv_string_value(lib_origin),44 jv_string_value(path) + sizeof ("./") - 1);45 } else if (strncmp("$ORIGIN/",jv_string_value(path),sizeof("$ORIGIN/")-1) == 0) {46 expanded_elt = jv_string_fmt("%s/%s",65 jv_string_value(jq_origin),66 jv_string_value(path) + sizeof ("$ORIGIN/") - 1);67 } else if (jv_get_kind(lib_origin) == JV_KIND_STRING &&68 path_is_relative(jv_copy(path))) {69 expanded_elt = jv_string_fmt("%s/%s",70 jv_string_value(lib_origin),71 jv_string_value(path));72 } else {50 expanded_elt = expand_path(path);51 if (!jv_is_valid(expanded_elt)) {52 jv_free(search_path);53 jv_free(expanded);54 jv_free(path);55 return expanded_elt;56 }73 expanded_elt = path;74 path = jv_invalid();75 }76 expanded = jv_array_append(expanded, expanded_elt);77 jv_free(path);78 }79 jv_free(jq_origin);80 jv_free(lib_origin);81 jv_free(search_path);65 return expanded;82 return JV_ARRAY(expanded, err);83}8485// Doesn't actually check that name not be an absolute path, and we86// don't have to: we always append relative paths to others (with a '/'
55 unmodified lines
142 int ret;143144 // Ideally we should cache this somewhere145 search = build_lib_search_chain(jq, search, jq_origin, lib_origin);146 jv err = jv_array_get(jv_copy(search), 1);147 search = jv_array_get(search, 0);148149 jv bname = jv_basename(jv_copy(rel_path));150151 jv_array_foreach(search, i, spath) {
32 unmodified lines
184 suffix));185 ret = stat(jv_string_value(testpath),&st);186 }187 if (ret == 0) {188 jv_free(err);189 jv_free(rel_path);190 jv_free(search);191 jv_free(bname);192 jv_free(spath);
1 unmodified line
194 }195 jv_free(testpath);196 jv_free(spath);197 }178 jv output = jv_invalid_with_msg(jv_string_fmt("module not found: %s", jv_string_value(rel_path)));198 jv output;199 if (!jv_is_valid(err)) {200 err = jv_invalid_get_msg(err);201 output = jv_invalid_with_msg(jv_string_fmt("module not found: %s (%s)",202 jv_string_value(rel_path),203 jv_string_value(err)));204 } else {205 output = jv_invalid_with_msg(jv_string_fmt("module not found: %s",206 jv_string_value(rel_path)));207 }208 jv_free(err);209 jv_free(rel_path);210 jv_free(search);211 jv_free(bname);212 return output;
176 unmodified lines