From f0f044a4562126d8f9700182b5aa751eb1d76221 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 18 Aug 2024 12:05:11 +0200 Subject: [PATCH 1/2] string-util: update ptr declaration to match our coding style --- src/basic/string-util.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/basic/string-util.h b/src/basic/string-util.h index eb1cd2d9e9..0f6e0b72bf 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -156,7 +156,7 @@ int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); bool chars_intersect(const char *a, const char *b) _pure_; -static inline bool _pure_ in_charset(const char *s, const char* charset) { +static inline bool _pure_ in_charset(const char *s, const char *charset) { assert(s); assert(charset); return s[strspn(s, charset)] == '\0'; @@ -248,7 +248,7 @@ REENABLE_WARNING; /* Like startswith_no_case(), but operates on arbitrary memory blocks. * It works only for ASCII strings. */ -static inline void *memory_startswith_no_case(const void *p, size_t sz, const char *token) { +static inline void* memory_startswith_no_case(const void *p, size_t sz, const char *token) { assert(token); size_t n = strlen(token); From e65b07293c4e2d69f231e63db98e5ec002182548 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 17 Aug 2024 22:36:19 +0200 Subject: [PATCH 2/2] edit-util: catch and warn about edits outside of markers The users still periodically come back to #24208. Let's add a detection for this hence. --- src/shared/edit-util.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/shared/edit-util.c b/src/shared/edit-util.c index 5dd8595a36..89ca9459e0 100644 --- a/src/shared/edit-util.c +++ b/src/shared/edit-util.c @@ -315,10 +315,12 @@ static int run_editor(const EditFileContext *context) { static int strip_edit_temp_file(EditFile *e) { _cleanup_free_ char *old_contents = NULL, *tmp = NULL, *new_contents = NULL; const char *stripped; + bool with_marker; int r; assert(e); assert(e->context); + assert(!e->context->marker_start == !e->context->marker_end); assert(e->temp); r = read_full_file(e->temp, &old_contents, NULL); @@ -329,12 +331,12 @@ static int strip_edit_temp_file(EditFile *e) { if (!tmp) return log_oom(); - if (e->context->marker_start && !e->context->stdin) { + with_marker = e->context->marker_start && !e->context->stdin; + + if (with_marker) { /* Trim out the lines between the two markers */ char *contents_start, *contents_end; - assert(e->context->marker_end); - contents_start = strstrafter(tmp, e->context->marker_start) ?: tmp; contents_end = strstr(contents_start, e->context->marker_end); @@ -345,8 +347,28 @@ static int strip_edit_temp_file(EditFile *e) { } else stripped = strstrip(tmp); - if (isempty(stripped)) + if (isempty(stripped)) { + /* People keep coming back to #24208 due to edits outside of markers. Let's detect this + * and point them in the right direction. */ + if (with_marker) + for (const char *p = old_contents;;) { + p = skip_leading_chars(p, WHITESPACE); + if (*p == '\0') + break; + if (*p != '#') { + log_warning("Found modifications outside of the staging area, which would be discarded."); + break; + } + + /* Skip the whole line if commented out */ + p = strchr(p, '\n'); + if (!p) + break; + p++; + } + return 0; /* File is empty (has no real changes) */ + } /* Trim prefix and suffix, but ensure suffixed by single newline */ new_contents = strjoin(stripped, "\n");