@@ -170,26 +170,22 @@ static char *convert_abs_rel(const char *from, const char *target)
170
170
int l ;
171
171
size_t i , rl , dirlen ;
172
172
173
- target_dir_p = strdup (target );
173
+ dirlen = dir_len (target );
174
+ target_dir_p = strndup (target , dirlen );
174
175
if (!target_dir_p )
175
176
return strdup (from );
176
177
177
- dirlen = dir_len (target_dir_p );
178
- target_dir_p [dirlen ] = '\0' ;
179
178
realpath_p = realpath (target_dir_p , NULL );
180
-
181
179
if (realpath_p == NULL ) {
182
180
log_warning ("convert_abs_rel(): target '%s' directory has no realpath." , target );
183
181
return strdup (from );
184
182
}
185
183
186
184
/* dir_len() skips double /'s e.g. //lib64, so we can't skip just one
187
185
* 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 ]);
193
189
194
190
/* now calculate the relative path from <from> to <target> and
195
191
store it in <relative_from>
@@ -431,19 +427,21 @@ static char *get_real_file(const char *src, bool fullyresolve)
431
427
struct stat sb ;
432
428
ssize_t linksz ;
433
429
char linktarget [PATH_MAX + 1 ];
434
- _cleanup_free_ char * fullsrcpath ;
430
+ _cleanup_free_ char * fullsrcpath_a = NULL ;
431
+ const char * fullsrcpath ;
435
432
_cleanup_free_ char * abspath = NULL ;
436
433
437
434
if (sysrootdirlen ) {
438
435
if (strncmp (src , sysrootdir , sysrootdirlen ) == 0 ) {
439
- fullsrcpath = strdup ( src ) ;
436
+ fullsrcpath = src ;
440
437
} else {
441
- _asprintf (& fullsrcpath , "%s/%s" ,
438
+ _asprintf (& fullsrcpath_a , "%s/%s" ,
442
439
(sysrootdirlen ? sysrootdir : "" ),
443
440
(src [0 ] == '/' ? src + 1 : src ));
441
+ fullsrcpath = fullsrcpath_a ;
444
442
}
445
443
} else {
446
- fullsrcpath = strdup ( src ) ;
444
+ fullsrcpath = src ;
447
445
}
448
446
449
447
log_debug ("get_real_file('%s')" , fullsrcpath );
@@ -471,15 +469,7 @@ static char *get_real_file(const char *src, bool fullyresolve)
471
469
if (linktarget [0 ] == '/' ) {
472
470
_asprintf (& abspath , "%s%s" , (sysrootdirlen ? sysrootdir : "" ), linktarget );
473
471
} 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 );
483
473
}
484
474
485
475
if (fullyresolve ) {
@@ -820,12 +810,11 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir
820
810
} else {
821
811
822
812
/* check destination directory */
823
- fulldstdir = strdup (fulldstpath );
813
+ fulldstdir = strndup (fulldstpath , dir_len ( fulldstpath ) );
824
814
if (!fulldstdir ) {
825
815
log_error ("Out of memory!" );
826
816
return 1 ;
827
817
}
828
- fulldstdir [dir_len (fulldstdir )] = '\0' ;
829
818
830
819
ret = stat (fulldstdir , & db );
831
820
@@ -838,11 +827,10 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir
838
827
}
839
828
/* create destination directory */
840
829
log_debug ("dest dir '%s' does not exist" , fulldstdir );
841
- dname = strdup (dst );
830
+
831
+ dname = strndup (dst , dir_len (dst ));
842
832
if (!dname )
843
833
return 1 ;
844
-
845
- dname [dir_len (dname )] = '\0' ;
846
834
ret = dracut_install (dname , dname , true, false, true);
847
835
848
836
if (ret != 0 ) {
0 commit comments