From dd68246737bce01e9e76e8a5ed347db26a51f7dc Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 13 Aug 2017 22:53:38 -0400 Subject: [PATCH] 7z: do global initialization once without risking a race condition. --- src/physfs.c | 1 + src/physfs_archiver_7z.c | 21 +++++++++++++-------- src/physfs_internal.h | 5 +++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/physfs.c b/src/physfs.c index e7872ffb..bac20d15 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -1143,6 +1143,7 @@ static int initStaticArchivers(void) REGISTER_STATIC_ARCHIVER(ZIP); #endif #if PHYSFS_SUPPORTS_7Z + SZIP_global_init(); REGISTER_STATIC_ARCHIVER(7Z); #endif #if PHYSFS_SUPPORTS_GRP diff --git a/src/physfs_archiver_7z.c b/src/physfs_archiver_7z.c index 3099f355..be05d32d 100644 --- a/src/physfs_archiver_7z.c +++ b/src/physfs_archiver_7z.c @@ -217,14 +217,6 @@ static void *SZIP_openArchive(PHYSFS_Io *io, const char *name, int forWriting) SZIPinfo *info = NULL; SRes rc; - /* !!! FIXME-3.0: this is a race condition; we need a global init method that gets called when registering new archivers. */ - static int generatedTable = 0; - if (!generatedTable) - { - generatedTable = 1; - CrcGenerateTable(); - } /* if */ - BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL); info = (SZIPinfo *) allocator.Malloc(sizeof (SZIPinfo)); @@ -387,6 +379,19 @@ static int SZIP_stat(void *opaque, const char *path, PHYSFS_Stat *stat) } /* SZIP_stat */ +void SZIP_global_init(void) +{ + /* this just needs to calculate some things, so it only ever + has to run once, even after a deinit. */ + static int generatedTable = 0; + if (!generatedTable) + { + generatedTable = 1; + CrcGenerateTable(); + } /* if */ +} /* SZIP_global_init */ + + const PHYSFS_Archiver __PHYSFS_Archiver_7Z = { CURRENT_PHYSFS_ARCHIVER_API_VERSION, diff --git a/src/physfs_internal.h b/src/physfs_internal.h index 636b0590..61463121 100644 --- a/src/physfs_internal.h +++ b/src/physfs_internal.h @@ -193,6 +193,11 @@ void __PHYSFS_smallFree(void *ptr); #define PHYSFS_SUPPORTS_VDF 1 #endif +#if PHYSFS_SUPPORTS_7Z +/* 7zip support needs a global init function called at startup (no deinit). */ +extern void SZIP_global_init(void); +#endif + /* The latest supported PHYSFS_Io::version value. */ #define CURRENT_PHYSFS_IO_API_VERSION 0