From 848517ddd4c9b957828aa21dca1e6bc5dd3fb2cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Tue, 8 Nov 2022 21:30:01 +0100 Subject: [PATCH] systemctl: edit: write override files as text files Instead of stripping the newline off the final would-be line; continue to reduce an empty-line-only file to an empty file, though Closes #25303 --- src/systemctl/systemctl-edit.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/systemctl/systemctl-edit.c b/src/systemctl/systemctl-edit.c index fe47f73d4a7..e8e6eeda290 100644 --- a/src/systemctl/systemctl-edit.c +++ b/src/systemctl/systemctl-edit.c @@ -483,6 +483,18 @@ static int trim_edit_markers(const char *path) { strshorten(contents_start, contents_end - contents_start); contents_start = strstrip(contents_start); + if (*contents_start && !endswith(contents_start, "\n")) { + char *tmp = contents_start; + if (MALLOC_SIZEOF_SAFE(contents) - (contents_start - contents) - strlen(contents_start) < 2) { + if ((tmp = realloc(contents, size + 1))) { + contents_start = tmp + (contents_start - contents); + contents = tmp; + } + } + + if (tmp) + strcat(contents_start, "\n"); + } /* Write new contents if the trimming actually changed anything */ if (strlen(contents) != size) {