From 9676d3b454a83c1b38a1c26524214c3251c8be94 Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Thu, 5 Oct 2023 18:02:24 +0900
Subject: [PATCH] test: add reproducer for SIGBUS issue caused by journal
 truncation

The added code fails without the previous commit.

For issue #24320.

(cherry picked from commit 3b0ae13bbf231ea01ff954de0c0c5375cbd85ff3)

Related: RHEL-11591
---
 src/journal/test-journal-flush.c | 38 +++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/src/journal/test-journal-flush.c b/src/journal/test-journal-flush.c
index 015604780f..2a5f7fad5e 100644
--- a/src/journal/test-journal-flush.c
+++ b/src/journal/test-journal-flush.c
@@ -8,9 +8,11 @@
 #include "alloc-util.h"
 #include "chattr-util.h"
 #include "journal-internal.h"
+#include "logs-show.h"
 #include "macro.h"
 #include "managed-journal-file.h"
 #include "path-util.h"
+#include "rm-rf.h"
 #include "string-util.h"
 #include "tests.h"
 
@@ -67,8 +69,42 @@ static void test_journal_flush_one(int argc, char *argv[]) {
                         break;
         }
 
+        if (n == 0)
+                return (void) log_tests_skipped("No journal entry found");
+
+        /* Open the new journal before archiving and offlining the file. */
+        sd_journal_close(j);
+        assert_se(sd_journal_open_directory(&j, dn, 0) >= 0);
+
+        /* Read the online journal. */
+        assert_se(sd_journal_seek_tail(j) >= 0);
+        assert_se(sd_journal_step_one(j, 0) > 0);
+        printf("current_journal: %s (%i)\n", j->current_file->path, j->current_file->fd);
+        assert_se(show_journal_entry(stdout, j, OUTPUT_EXPORT, 0, 0, NULL, NULL, NULL, &(dual_timestamp) {}, &(sd_id128_t) {}) >= 0);
+
+        uint64_t p;
+        assert_se(journal_file_tail_end_by_mmap(j->current_file, &p) >= 0);
+        for (uint64_t q = ALIGN64(p + 1); q < (uint64_t) j->current_file->last_stat.st_size; q = ALIGN64(q + 1)) {
+                Object *o;
+
+                r = journal_file_move_to_object(j->current_file, OBJECT_UNUSED, q, &o);
+                assert_se(IN_SET(r, -EBADMSG, -EADDRNOTAVAIL));
+        }
+
+        /* Archive and offline file. */
+        assert_se(journal_file_archive(new_journal->file, NULL) >= 0);
+        assert_se(managed_journal_file_set_offline(new_journal, /* wait = */ true) >= 0);
+
+        /* Read the archived and offline journal. */
+        for (uint64_t q = ALIGN64(p + 1); q < (uint64_t) j->current_file->last_stat.st_size; q = ALIGN64(q + 1)) {
+                Object *o;
+
+                r = journal_file_move_to_object(j->current_file, OBJECT_UNUSED, q, &o);
+                assert_se(IN_SET(r, -EBADMSG, -EADDRNOTAVAIL, -EIDRM));
+        }
+
         unlink(fn);
-        assert_se(rmdir(dn) == 0);
+        assert_se(rm_rf(dn, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
 }
 
 TEST(journal_flush) {