Skip to content

Commit e7d6a1e

Browse files
nabijaczlewelijohannbg
authored andcommitted
perf(dracut-install): strdup()+[dirlen]=0 => strndup
1 parent 846a845 commit e7d6a1e

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

src/install/dracut-install.c

+15-27
Original file line numberDiff line numberDiff line change
@@ -170,26 +170,22 @@ static char *convert_abs_rel(const char *from, const char *target)
170170
int l;
171171
size_t i, rl, dirlen;
172172

173-
target_dir_p = strdup(target);
173+
dirlen = dir_len(target);
174+
target_dir_p = strndup(target, dirlen);
174175
if (!target_dir_p)
175176
return strdup(from);
176177

177-
dirlen = dir_len(target_dir_p);
178-
target_dir_p[dirlen] = '\0';
179178
realpath_p = realpath(target_dir_p, NULL);
180-
181179
if (realpath_p == NULL) {
182180
log_warning("convert_abs_rel(): target '%s' directory has no realpath.", target);
183181
return strdup(from);
184182
}
185183

186184
/* dir_len() skips double /'s e.g. //lib64, so we can't skip just one
187185
* character - need to skip all leading /'s */
188-
rl = strlen(target);
189-
for (i = dirlen + 1; i < rl; ++i)
190-
if (target_dir_p[i] != '/')
191-
break;
192-
_asprintf(&realtarget, "%s/%s", realpath_p, &target_dir_p[i]);
186+
for (i = dirlen + 1; target[i] == '/'; ++i)
187+
;
188+
_asprintf(&realtarget, "%s/%s", realpath_p, &target[i]);
193189

194190
/* now calculate the relative path from <from> to <target> and
195191
store it in <relative_from>
@@ -431,19 +427,21 @@ static char *get_real_file(const char *src, bool fullyresolve)
431427
struct stat sb;
432428
ssize_t linksz;
433429
char linktarget[PATH_MAX + 1];
434-
_cleanup_free_ char *fullsrcpath;
430+
_cleanup_free_ char *fullsrcpath_a = NULL;
431+
const char *fullsrcpath;
435432
_cleanup_free_ char *abspath = NULL;
436433

437434
if (sysrootdirlen) {
438435
if (strncmp(src, sysrootdir, sysrootdirlen) == 0) {
439-
fullsrcpath = strdup(src);
436+
fullsrcpath = src;
440437
} else {
441-
_asprintf(&fullsrcpath, "%s/%s",
438+
_asprintf(&fullsrcpath_a, "%s/%s",
442439
(sysrootdirlen ? sysrootdir : ""),
443440
(src[0] == '/' ? src + 1 : src));
441+
fullsrcpath = fullsrcpath_a;
444442
}
445443
} else {
446-
fullsrcpath = strdup(src);
444+
fullsrcpath = src;
447445
}
448446

449447
log_debug("get_real_file('%s')", fullsrcpath);
@@ -471,15 +469,7 @@ static char *get_real_file(const char *src, bool fullyresolve)
471469
if (linktarget[0] == '/') {
472470
_asprintf(&abspath, "%s%s", (sysrootdirlen ? sysrootdir : ""), linktarget);
473471
} else {
474-
_cleanup_free_ char *fullsrcdir = strdup(fullsrcpath);
475-
476-
if (!fullsrcdir) {
477-
log_error("Out of memory!");
478-
exit(EXIT_FAILURE);
479-
}
480-
481-
fullsrcdir[dir_len(fullsrcdir)] = '\0';
482-
_asprintf(&abspath, "%s/%s", fullsrcdir, linktarget);
472+
_asprintf(&abspath, "%.*s/%s", (int)dir_len(fullsrcpath), fullsrcpath, linktarget);
483473
}
484474

485475
if (fullyresolve) {
@@ -820,12 +810,11 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir
820810
} else {
821811

822812
/* check destination directory */
823-
fulldstdir = strdup(fulldstpath);
813+
fulldstdir = strndup(fulldstpath, dir_len(fulldstpath));
824814
if (!fulldstdir) {
825815
log_error("Out of memory!");
826816
return 1;
827817
}
828-
fulldstdir[dir_len(fulldstdir)] = '\0';
829818

830819
ret = stat(fulldstdir, &db);
831820

@@ -838,11 +827,10 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir
838827
}
839828
/* create destination directory */
840829
log_debug("dest dir '%s' does not exist", fulldstdir);
841-
dname = strdup(dst);
830+
831+
dname = strndup(dst, dir_len(dst));
842832
if (!dname)
843833
return 1;
844-
845-
dname[dir_len(dname)] = '\0';
846834
ret = dracut_install(dname, dname, true, false, true);
847835

848836
if (ret != 0) {

0 commit comments

Comments
 (0)