mirror of
https://github.com/FeralInteractive/gamemode.git
synced 2025-06-26 17:31:45 +02:00
Enforce strict compiler warnings
This exposed a bunch of issues that needed dealing with to ensure the code is clean and sane. Notably the dlopen/dlsym routine has been altered to closer match the LSI approach of safe symbol binding, by not attempting to directly cast the result of a dlsym operation. Instead, if we succeed in getting the dlsym() pointer, we memcpy this to the target and ensure we have the correct constraints. Note that in sanitizing the log helpers, I opted to remove the varargs ability from FATAL_ERRNO given this is used exactly like perror() and there are no examples currently using varargs with this in the tree. This allowed me to keep the log helpers as macros and not have to implement wrapper functions. Signed-off-by: Ikey Doherty <ikey@solus-project.com>
This commit is contained in:

committed by
Marc Di Luzio

parent
68e326de60
commit
e90bd98d64
@ -37,7 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <unistd.h>
|
||||
|
||||
// Storage for error strings
|
||||
static char error_string[512] = {};
|
||||
static char error_string[512] = { 0 };
|
||||
|
||||
// Simple requestor function for a gamemode
|
||||
static int gamemode_request(const char *function)
|
||||
@ -86,19 +86,19 @@ static int gamemode_request(const char *function)
|
||||
}
|
||||
|
||||
// Get the error string
|
||||
extern const char *real_gamemode_error_string()
|
||||
extern const char *real_gamemode_error_string(void)
|
||||
{
|
||||
return error_string;
|
||||
}
|
||||
|
||||
// Wrapper to call RegisterGame
|
||||
extern int real_gamemode_request_start()
|
||||
extern int real_gamemode_request_start(void)
|
||||
{
|
||||
return gamemode_request("RegisterGame");
|
||||
}
|
||||
|
||||
// Wrapper to call UnregisterGame
|
||||
extern int real_gamemode_request_end()
|
||||
extern int real_gamemode_request_end(void)
|
||||
{
|
||||
return gamemode_request("UnregisterGame");
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
char _client_error_string[512] = {};
|
||||
char _client_error_string[512] = { 0 };
|
||||
|
||||
/**
|
||||
* Load libgamemode dynamically to dislodge us from most dependencies.
|
||||
@ -49,27 +49,69 @@ char _client_error_string[512] = {};
|
||||
volatile int _libgamemode_loaded = 1;
|
||||
|
||||
/* Typedefs for the functions to load */
|
||||
typedef int (*_gamemode_request_start)();
|
||||
typedef int (*_gamemode_request_end)();
|
||||
typedef const char *(*_gamemode_error_string)();
|
||||
typedef int (*_gamemode_request_start)(void);
|
||||
typedef int (*_gamemode_request_end)(void);
|
||||
typedef const char *(*_gamemode_error_string)(void);
|
||||
|
||||
/* Storage for functors */
|
||||
_gamemode_request_start _REAL_gamemode_request_start = NULL;
|
||||
_gamemode_request_end _REAL_gamemode_request_end = NULL;
|
||||
_gamemode_error_string _REAL_gamemode_error_string = NULL;
|
||||
|
||||
/**
|
||||
* Internal helper to perform the symbol binding safely.
|
||||
*
|
||||
* Returns 0 on success and -1 on failure
|
||||
*/
|
||||
__attribute__((always_inline)) inline int _bind_libgamemode_symbol(void *handle, const char *name,
|
||||
void **out_func,
|
||||
size_t func_size)
|
||||
{
|
||||
void *symbol_lookup = NULL;
|
||||
char *dl_error = NULL;
|
||||
|
||||
/* Safely look up the symbol */
|
||||
symbol_lookup = dlsym(handle, name);
|
||||
dl_error = dlerror();
|
||||
if (dl_error || !symbol_lookup) {
|
||||
snprintf(_client_error_string, sizeof(_client_error_string), "dlsym failed - %s", dl_error);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Have the symbol correctly, copy it to make it usable */
|
||||
memcpy(out_func, &symbol_lookup, func_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads libgamemode and needed functions
|
||||
*
|
||||
* Returns 0 on success and -1 on failure
|
||||
*/
|
||||
__attribute__((always_inline)) inline int _load_libgamemode()
|
||||
__attribute__((always_inline)) inline int _load_libgamemode(void)
|
||||
{
|
||||
/* We start at 1, 0 is a success and -1 is a fail */
|
||||
if (_libgamemode_loaded != 1) {
|
||||
return _libgamemode_loaded;
|
||||
}
|
||||
|
||||
/* Anonymous struct type to define our bindings */
|
||||
struct binding {
|
||||
const char *name;
|
||||
void **functor;
|
||||
size_t func_size;
|
||||
} bindings[] = {
|
||||
{ "real_gamemode_request_start",
|
||||
(void **)&_REAL_gamemode_request_start,
|
||||
sizeof(_REAL_gamemode_request_start) },
|
||||
{ "real_gamemode_request_end",
|
||||
(void **)&_REAL_gamemode_request_end,
|
||||
sizeof(_REAL_gamemode_request_end) },
|
||||
{ "real_gamemode_error_string",
|
||||
(void **)&_REAL_gamemode_error_string,
|
||||
sizeof(_REAL_gamemode_error_string) },
|
||||
};
|
||||
|
||||
void *libgamemode = NULL;
|
||||
|
||||
/* Try and load libgamemode */
|
||||
@ -79,35 +121,32 @@ __attribute__((always_inline)) inline int _load_libgamemode()
|
||||
sizeof(_client_error_string),
|
||||
"dylopen failed - %s",
|
||||
dlerror());
|
||||
} else {
|
||||
_REAL_gamemode_request_start =
|
||||
(_gamemode_request_start)dlsym(libgamemode, "real_gamemode_request_start");
|
||||
_REAL_gamemode_request_end =
|
||||
(_gamemode_request_end)dlsym(libgamemode, "real_gamemode_request_end");
|
||||
_REAL_gamemode_error_string =
|
||||
(_gamemode_error_string)dlsym(libgamemode, "real_gamemode_error_string");
|
||||
|
||||
/* Verify we have the functions we want */
|
||||
if (_REAL_gamemode_request_start && _REAL_gamemode_request_end &&
|
||||
_REAL_gamemode_error_string) {
|
||||
_libgamemode_loaded = 0;
|
||||
return 0;
|
||||
} else {
|
||||
snprintf(_client_error_string,
|
||||
sizeof(_client_error_string),
|
||||
"dlsym failed - %s",
|
||||
dlerror());
|
||||
}
|
||||
_libgamemode_loaded = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
_libgamemode_loaded = -1;
|
||||
return -1;
|
||||
/* Attempt to bind all symbols */
|
||||
for (size_t i = 0; i < sizeof(bindings) / sizeof(bindings[0]); i++) {
|
||||
struct binding *binder = &bindings[i];
|
||||
|
||||
if (_bind_libgamemode_symbol(libgamemode,
|
||||
binder->name,
|
||||
binder->functor,
|
||||
binder->func_size) != 0) {
|
||||
_libgamemode_loaded = -1;
|
||||
return -1;
|
||||
};
|
||||
}
|
||||
|
||||
/* Success */
|
||||
_libgamemode_loaded = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect to the real libgamemode
|
||||
*/
|
||||
__attribute__((always_inline)) inline const char *gamemode_error_string()
|
||||
__attribute__((always_inline)) inline const char *gamemode_error_string(void)
|
||||
{
|
||||
/* If we fail to load the system gamemode, return our error string */
|
||||
if (_load_libgamemode() < 0) {
|
||||
@ -127,7 +166,7 @@ __attribute__((constructor))
|
||||
#else
|
||||
__attribute__((always_inline)) inline
|
||||
#endif
|
||||
int gamemode_request_start()
|
||||
int gamemode_request_start(void)
|
||||
{
|
||||
/* Need to load gamemode */
|
||||
if (_load_libgamemode() < 0) {
|
||||
@ -153,7 +192,7 @@ __attribute__((destructor))
|
||||
#else
|
||||
__attribute__((always_inline)) inline
|
||||
#endif
|
||||
int gamemode_request_end()
|
||||
int gamemode_request_end(void)
|
||||
{
|
||||
/* Need to load gamemode */
|
||||
if (_load_libgamemode() < 0) {
|
||||
|
Reference in New Issue
Block a user