From: Frantisek Sumsal Date: Thu, 1 Feb 2024 12:56:37 +0000 (+0100) Subject: vpick: make a working copy of the current dname X-Git-Tag: v256-rc1~978^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=925878476213d927d5b24c15a80edd5440934479;p=thirdparty%2Fsystemd.git vpick: make a working copy of the current dname Since we might edit the string later on by inserting NULs, which then leads up to using an invalid dname when opening the potential chosen directory: [ 4316.957536] testsuite-74.sh[99]: make_choice: entry: mytree_37.0_arm64+2-3 [ 4316.957536] testsuite-74.sh[99]: make_choice: best_version: 37.0 [ 4316.957536] testsuite-74.sh[99]: make_choice: best_filename: mytree_37.0 [ 4316.957536] testsuite-74.sh[99]: Failed to open '//var/lib/machines/mytree.v/mytree_37.0': No such file or directory Uncovered by vpick tests from TEST-74-AUX-UTILS when run on aarch64. --- diff --git a/src/shared/vpick.c b/src/shared/vpick.c index ab1f4289fcc..614708133b1 100644 --- a/src/shared/vpick.c +++ b/src/shared/vpick.c @@ -334,12 +334,16 @@ static int make_choice( FOREACH_ARRAY(entry, de->entries, de->n_entries) { unsigned found_tries_done = UINT_MAX, found_tries_left = UINT_MAX; - _cleanup_free_ char *chopped = NULL; + _cleanup_free_ char *dname = NULL; size_t found_architecture_index = SIZE_MAX; const char *e; + dname = strdup((*entry)->d_name); + if (!dname) + return log_oom_debug(); + if (!isempty(filter->basename)) { - e = startswith((*entry)->d_name, filter->basename); + e = startswith(dname, filter->basename); if (!e) continue; @@ -348,20 +352,14 @@ static int make_choice( e++; } else - e = (*entry)->d_name; + e = dname; if (!isempty(filter->suffix)) { - const char *sfx; - - sfx = endswith(e, filter->suffix); + char *sfx = endswith(e, filter->suffix); if (!sfx) continue; - chopped = strndup(e, sfx - e); - if (!chopped) - return log_oom_debug(); - - e = chopped; + *sfx = 0; } if (FLAGS_SET(flags, PICK_TRIES)) {