From ed5d540bf64fafbf9266b28f1156ce3ce2ba3038 Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Wed, 24 Jan 2024 21:19:56 +0100 Subject: rar: Simplify and merge duplicate code Merge version matching for rar and unrar. This is ever so slightly slower in theory for unrar 5.x because there's an additional unnecessary `sscanf()` call, but it's not actually gonna matter, and saves quite a bit of duplicated logic. This also makes the matching a bit more safe by verifying `sscanf()` actually worked, and avoids using a uninitialized `version` value due to an unexpected input. Finally, this makes the parsing a bit more strict by requiring a version number after the `RAR` and `UNRAR` line prefixes -- leading not to using the uninitialized version variable. This will be required by the upcoming unrar-free support as it reports a `RAR archive ...` line that would have matched the `RAR` version check. --- src/fr-command-rar.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/fr-command-rar.c b/src/fr-command-rar.c index d6a724e..7e69fd9 100644 --- a/src/fr-command-rar.c +++ b/src/fr-command-rar.c @@ -194,35 +194,19 @@ process_line (char *line, g_return_if_fail (line != NULL); if (! rar_comm->list_started) { - if (strncmp (line, "RAR ", 4) == 0) { - int version; - sscanf (line, "RAR %d.", &version); - rar_comm->rar5 = (version >= 5); - - if (version > 5) - date_newstyle = TRUE; - else if (version == 5) - { - sscanf (line, "RAR 5.%d ", &version); - if (version >= 30) - date_newstyle = TRUE; - } + int version = 0; - } - else if (strncmp (line, "UNRAR ", 6) == 0) { - int version; - sscanf (line, "UNRAR %d.", &version); + if (sscanf (line, "RAR %d.", &version) == 1 || sscanf (line, "UNRAR %d.", &version) == 1) { rar_comm->rar5 = (version >= 5); if (version > 5) date_newstyle = TRUE; - else if (version == 5) + else if (version == 5 && (sscanf (line, "RAR 5.%d ", &version) == 1 || + sscanf (line, "UNRAR 5.%d ", &version) == 1)) { - sscanf (line, "UNRAR 5.%d ", &version); if (version >= 30) date_newstyle = TRUE; } - } else if (strncmp (line, "--------", 8) == 0) { rar_comm->list_started = TRUE; -- cgit v1.2.1