Skip to content

Commit b34a4f0

Browse files
authored
Merge pull request #20256 from keszybz/one-alloca-too-many
basic/unit-name: do not use strdupa() on a path
2 parents 30c9faf + 4e2544c commit b34a4f0

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

Diff for: src/basic/unit-name.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,13 @@ int unit_name_unescape(const char *f, char **ret) {
378378
}
379379

380380
int unit_name_path_escape(const char *f, char **ret) {
381-
char *p, *s;
381+
_cleanup_free_ char *p = NULL;
382+
char *s;
382383

383384
assert(f);
384385
assert(ret);
385386

386-
p = strdupa(f);
387+
p = strdup(f);
387388
if (!p)
388389
return -ENOMEM;
389390

@@ -395,13 +396,9 @@ int unit_name_path_escape(const char *f, char **ret) {
395396
if (!path_is_normalized(p))
396397
return -EINVAL;
397398

398-
/* Truncate trailing slashes */
399+
/* Truncate trailing slashes and skip leading slashes */
399400
delete_trailing_chars(p, "/");
400-
401-
/* Truncate leading slashes */
402-
p = skip_leading_chars(p, "/");
403-
404-
s = unit_name_escape(p);
401+
s = unit_name_escape(skip_leading_chars(p, "/"));
405402
}
406403
if (!s)
407404
return -ENOMEM;
@@ -531,7 +528,7 @@ int unit_name_from_path(const char *path, const char *suffix, char **ret) {
531528
if (strlen(s) >= UNIT_NAME_MAX) /* Return a slightly more descriptive error for this specific condition */
532529
return -ENAMETOOLONG;
533530

534-
/* Refuse this if this got too long or for some other reason didn't result in a valid name */
531+
/* Refuse if this for some other reason didn't result in a valid name */
535532
if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
536533
return -EINVAL;
537534

@@ -565,7 +562,7 @@ int unit_name_from_path_instance(const char *prefix, const char *path, const cha
565562
if (strlen(s) >= UNIT_NAME_MAX) /* Return a slightly more descriptive error for this specific condition */
566563
return -ENAMETOOLONG;
567564

568-
/* Refuse this if this got too long or for some other reason didn't result in a valid name */
565+
/* Refuse if this for some other reason didn't result in a valid name */
569566
if (!unit_name_is_valid(s, UNIT_NAME_INSTANCE))
570567
return -EINVAL;
571568

0 commit comments

Comments
 (0)