1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v.util: fix an out of bounds panic in mod_path_to_full_name/2

This commit is contained in:
Delyan Angelov 2021-01-20 18:11:32 +02:00
parent d92f5c55ba
commit 071549baae
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -3,6 +3,10 @@ module util
import os import os
import v.pref import v.pref
fn trace_mod_path_to_full_name(line string, mod string, file_path string, res string) {
eprintln('> $line ${@FN} mod: ${mod:-20} | file_path: ${file_path:-30} | result: $res')
}
pub fn qualify_import(pref &pref.Preferences, mod string, file_path string) string { pub fn qualify_import(pref &pref.Preferences, mod string, file_path string) string {
mut mod_paths := pref.lookup_path.clone() mut mod_paths := pref.lookup_path.clone()
mod_paths << os.vmodules_paths() mod_paths << os.vmodules_paths()
@ -11,11 +15,17 @@ pub fn qualify_import(pref &pref.Preferences, mod string, file_path string) stri
try_path := os.join_path(search_path, mod_path) try_path := os.join_path(search_path, mod_path)
if os.is_dir(try_path) { if os.is_dir(try_path) {
if m1 := mod_path_to_full_name(mod, try_path) { if m1 := mod_path_to_full_name(mod, try_path) {
$if trace_mod_path_to_full_name ? {
trace_mod_path_to_full_name(@LINE, mod, try_path, m1)
}
return m1 return m1
} }
} }
} }
if m1 := mod_path_to_full_name(mod, file_path) { if m1 := mod_path_to_full_name(mod, file_path) {
$if trace_mod_path_to_full_name ? {
trace_mod_path_to_full_name(@LINE, mod, file_path, m1)
}
return m1 return m1
} }
return mod return mod
@ -25,7 +35,11 @@ pub fn qualify_module(mod string, file_path string) string {
if mod == 'main' { if mod == 'main' {
return mod return mod
} }
if m1 := mod_path_to_full_name(mod, file_path.all_before_last('/')) { clean_file_path := file_path.all_before_last('/')
if m1 := mod_path_to_full_name(mod, clean_file_path) {
$if trace_mod_path_to_full_name ? {
trace_mod_path_to_full_name(@LINE, mod, clean_file_path, m1)
}
return m1 return m1
} }
return mod return mod
@ -71,12 +85,12 @@ pub fn mod_path_to_full_name(mod string, path string) ?string {
mut try_path_parts := try_path.split(os.path_separator) mut try_path_parts := try_path.split(os.path_separator)
// last index in try_path_parts that contains a `v.mod` // last index in try_path_parts that contains a `v.mod`
mut last_v_mod := -1 mut last_v_mod := -1
for j := try_path_parts.len; j > 0; j-- { for j := try_path_parts.len - 1; j > 0; j-- {
parent := try_path_parts[0..j].join(os.path_separator) parent := try_path_parts[0..j].join(os.path_separator)
if ls := os.ls(parent) { if ls := os.ls(parent) {
// currently CI clones some modules into the v repo to test, the condition // currently CI clones some modules into the v repo to test, the condition
// after `'v.mod' in ls` can be removed once a proper solution is added // after `'v.mod' in ls` can be removed once a proper solution is added
if 'v.mod' in ls && try_path_parts[i] != 'v' && 'vlib' !in ls { if 'v.mod' in ls && try_path_parts[j] != 'v' && 'vlib' !in ls {
last_v_mod = j last_v_mod = j
continue continue
} }