[PATCH] log_cleaner: Detect rotated filenames properly

Michal Privoznik posted 1 patch 3 months, 1 week ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/a0e713b00b455f7e1fcaccb8280e1a94c208eec7.1716968996.git.mprivozn@redhat.com
src/logging/log_cleaner.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
[PATCH] log_cleaner: Detect rotated filenames properly
Posted by Michal Privoznik 3 months, 1 week ago
When removing rotated log files, their name is matched against a
regex (@log_regex) and if they contain '.N' suffix the 'N' is
then parsed into an integer. Well, due to a bug in
virLogCleanerParseFilename() this is not how the code works. If
the suffix isn't found then g_match_info_fetch() returns an empty
string instead of NULL which then makes str2int parsing fail.
Just check for this case before parsing the string.

Based on the original patch sent by David.

Reported-by: David Negreira <david.negreira@canonical.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---

The original patch was posted here:

https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/thread/ENXJABODLWWXAR6RSKGMG6GAJKZMVKKM/

In the thread you can see me suggesting this alternative approach and
David confirming it works. Therefore, I'd like to get this in before the
release.

 src/logging/log_cleaner.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/src/logging/log_cleaner.c b/src/logging/log_cleaner.c
index 4ee91843aa..4efcbc18e4 100644
--- a/src/logging/log_cleaner.c
+++ b/src/logging/log_cleaner.c
@@ -82,10 +82,8 @@ virLogCleanerParseFilename(const char *path,
     *rotated_index = 0;
     rotated_index_str = g_match_info_fetch(matchInfo, 3);
 
-    if (!rotated_index_str)
-        return chain_prefix;
-
-    if (virStrToLong_i(rotated_index_str, NULL, 10, rotated_index) < 0) {
+    if (rotated_index_str && STRNEQ(rotated_index_str, "") &&
+        virStrToLong_i(rotated_index_str, NULL, 10, rotated_index) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to parse rotated index from '%1$s'"),
                        rotated_index_str);
-- 
2.44.1
Re: [PATCH] log_cleaner: Detect rotated filenames properly
Posted by Jiri Denemark 3 months, 1 week ago
On Wed, May 29, 2024 at 09:51:36 +0200, Michal Privoznik wrote:
> When removing rotated log files, their name is matched against a
> regex (@log_regex) and if they contain '.N' suffix the 'N' is
> then parsed into an integer. Well, due to a bug in
> virLogCleanerParseFilename() this is not how the code works. If
> the suffix isn't found then g_match_info_fetch() returns an empty
> string instead of NULL which then makes str2int parsing fail.
> Just check for this case before parsing the string.
> 
> Based on the original patch sent by David.
> 
> Reported-by: David Negreira <david.negreira@canonical.com>
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
> 
> The original patch was posted here:
> 
> https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/thread/ENXJABODLWWXAR6RSKGMG6GAJKZMVKKM/
> 
> In the thread you can see me suggesting this alternative approach and
> David confirming it works. Therefore, I'd like to get this in before the
> release.
> 
>  src/logging/log_cleaner.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)

Reviewed-by: Jiri Denemark <jdenemar@redhat.com>