File tests-fix-wrong-pos-with-CONFIG_PRINTK_CALLER.patch of Package lvm2

From fe68fb052566ca4958743af1f1d95b890ae4f893 Mon Sep 17 00:00:00 2001
From: Zdenek Kabelac <zkabelac@redhat.com>
Date: Mon, 3 Jun 2024 13:41:06 +0200
Subject: [PATCH] tests: fix wrong pos with CONFIG_PRINTK_CALLER

SUSE kernels distribution enables CONFIG_PRINTK_CALLER by default.

One line of cat /dev/kmsg is like:

6,904,9506214456,-,caller=T24012;loop8: detected capacity change from 0
to 354304

If CONFIG_PRINTK_CALLER is off:

6,721,53563833,-;loop0: detected capacity change from 0 to 354304

',caller=T24012' is the redundant part needed to be handled.
Otherwise pos will be lager than buf size causing sz underflowed.
Then constructor of std::string will throw a exception to break
tests:
$ make  check_local  T=shell/000-basic.sh
VERBOSE=0 ./lib/runner \
        --testdir . --outdir results \
        --flavours ndev-vanilla --only shell/000-basic.sh --skip @

running 1 tests
running: [ndev-vanilla] shell/000-basic.sh
            0:00.000Exception: basic_string::_M_create
make[1]: *** [Makefile:148: check_local] Error 1
make[1]: Leaving directory '/root/lvm2/test'
make: *** [Makefile:89: check_local] Error 2

Fix it with strchr for ';' as delimiter to get pos.

Reported-by: Su Yue <glass.su@suse.com>
---
 test/lib/brick-shelltest.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/test/lib/brick-shelltest.h b/test/lib/brick-shelltest.h
index 154f92a994c1..a8154003c07c 100644
--- a/test/lib/brick-shelltest.h
+++ b/test/lib/brick-shelltest.h
@@ -717,9 +717,13 @@ struct KMsg : Source {
         unsigned long t;
         time_t tt;
         size_t len;
+        const char *delimiter = strchr(buf, ';');
 
         buf[ *sz ] = 0;
-        if (sscanf( buf, "%u,%u,%lu,-;%n", &level, &num, &t, &pos ) == 3) {
+
+        if ( sscanf( buf, "%u,%u,%lu,-%n", &level, &num, &t, &pos) == 3 ) {
+            if ( delimiter++ && ( delimiter - buf ) > pos )
+                pos = delimiter - buf;
             memcpy( newbuf, buf, *sz );
             tt = time( 0 );
             len = snprintf( buf, 64, "[%lu.%06lu] <%u> ", t / 1000000, t % 1000000, level );
-- 
2.48.1

openSUSE Build Service is sponsored by