Skip to content

Commit

Permalink
MultiArc: fix crash after plugin reload (from far2l)
Browse files Browse the repository at this point in the history
  • Loading branch information
shmuz committed Oct 15, 2023
1 parent eaf5a9a commit 327115c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions multiarc/src/MultiArc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ PutFiles(HANDLE hPlugin, struct PluginPanelItem *PanelItem, int ItemsNumber, int
SHAREDSYMBOL void WINAPI _export ExitFAR()
{
delete ArcPlugin;
ArcPlugin = NULL;
}

SHAREDSYMBOL void WINAPI _export GetPluginInfo(struct PluginInfo *Info)
Expand Down
3 changes: 1 addition & 2 deletions multiarc/src/MultiArc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ class PluginClass
char CurDir[NM];
ArcItemNode ArcData;
size_t ArcDataCount = 0;
struct stat ArcStat
{};
struct stat ArcStat {};
int ArcPluginNumber;
int ArcPluginType;
int LastTestState, LastWithoutPathsState;
Expand Down
42 changes: 23 additions & 19 deletions multiarc/src/formats/libarch/libarch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ enum MenuFormats

struct IsArchiveContext
{
const char *Name;
const unsigned char *Head;
size_t HeadSize;
const char *const Name;
const unsigned char *const Head;
const size_t HeadSize;
const off_t FileSize;

off_t FileSize;
off_t Pos;
int fd;

Expand Down Expand Up @@ -116,8 +116,13 @@ static ssize_t IsArchive_Read(struct archive *, void *data, const void **buff)
return piece;
}

static int IsArchive_OpenAndGetFormat(struct archive *a)
static int IsArchive_OpenAndGetFormat(struct archive *a, void *ctx)
{
archive_read_set_callback_data(a, ctx);
archive_read_set_read_callback(a, IsArchive_Read);
archive_read_set_seek_callback(a, IsArchive_Seek);
archive_read_set_skip_callback(a, IsArchive_Skip);
archive_read_set_close_callback(a, IsArchive_Close);
int fmt = -1;
int r = archive_read_open1(a);
if (r == ARCHIVE_OK || r == ARCHIVE_WARN) {
Expand All @@ -137,17 +142,10 @@ BOOL WINAPI _export LIBARCH_IsArchive(const char *Name, const unsigned char *Dat
if (!a)
return FALSE;

IsArchiveContext ctx = {Name, Data, (size_t)DataSize, (off_t)DataSize, 0, -1, {}};
struct stat s{};
if (Name && sdc_stat(Name, &s) == 0) {
ctx.FileSize = s.st_size;
}
bool str = (Name && sdc_stat(Name, &s) == 0);

archive_read_set_read_callback(a, IsArchive_Read);
archive_read_set_seek_callback(a, IsArchive_Seek);
archive_read_set_skip_callback(a, IsArchive_Skip);
archive_read_set_close_callback(a, IsArchive_Close);
archive_read_set_callback_data(a, &ctx);
IsArchiveContext ctx = {Name, Data, (size_t)DataSize, str ? s.st_size : (off_t)DataSize, 0, -1, {}};

archive_read_support_filter_all(a);
LibArchCall(archive_read_support_format_raw, a);
Expand All @@ -157,11 +155,18 @@ BOOL WINAPI _export LIBARCH_IsArchive(const char *Name, const unsigned char *Dat
LibArchCall(archive_read_support_format_cpio, a);
LibArchCall(archive_read_support_format_cab, a);

int fmt = IsArchive_OpenAndGetFormat(a);
int fmt = IsArchive_OpenAndGetFormat(a, &ctx);
archive_read_free(a);
if (fmt == -1) {
archive_read_support_format_all(a);
archive_read_support_format_raw(a);
fmt = IsArchive_OpenAndGetFormat(a);
a = archive_read_new();
if (a) {
ctx.Pos = 0;
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
archive_read_support_format_raw(a);
fmt = IsArchive_OpenAndGetFormat(a, &ctx);
archive_read_free(a);
}
}

if (fmt == ARCHIVE_FORMAT_MTREE && !StrEndsBy(Name, ".tree") && !StrEndsBy(Name, ".mtree")) {
Expand All @@ -174,7 +179,6 @@ BOOL WINAPI _export LIBARCH_IsArchive(const char *Name, const unsigned char *Dat
fprintf(stderr, "%s: fmt=%d\n", __FUNCTION__, fmt);
}

archive_read_free(a);

if (ctx.fd != -1) {
sdc_close(ctx.fd);
Expand Down

0 comments on commit 327115c

Please sign in to comment.