From d1fb98415a7324e5579e738fe02723d7efabe236 Mon Sep 17 00:00:00 2001 From: Martin Olivier Date: Mon, 7 Feb 2022 15:31:33 +0100 Subject: [PATCH] fix: crash when nullptr is passed as argument in get_function or get_variable when no dynamic lib is currently loaded --- README.md | 6 +++--- dylib.hpp | 27 +++++++++++++-------------- tests/tests.cpp | 24 +++++++++++++++++++++++- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index d307b51..7bc8ec1 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Dylib - Dynamic Library Loader for C++ -[![Dylib](https://img.shields.io/badge/Dylib-v1.8.0-blue.svg)](https://github.com/martin-olivier/dylib/releases/tag/v1.8.0) +[![Dylib](https://img.shields.io/badge/Dylib-v1.8.1-blue.svg)](https://github.com/martin-olivier/dylib/releases/tag/v1.8.1) [![MIT license](https://img.shields.io/badge/License-MIT-orange.svg)](https://github.com/martin-olivier/dylib/blob/main/LICENSE) [![CPP Version](https://img.shields.io/badge/C++-11_and_above-darkgreen.svg)](https://isocpp.org/) @@ -10,7 +10,7 @@ [![workflow](https://github.com/martin-olivier/dylib/actions/workflows/CI.yml/badge.svg)](https://github.com/martin-olivier/dylib/actions/workflows/CI.yml) [![codecov](https://codecov.io/gh/martin-olivier/dylib/branch/main/graph/badge.svg?token=4V6A9B7PII)](https://codecov.io/gh/martin-olivier/dylib) -[![GitHub download](https://img.shields.io/github/downloads/martin-olivier/dylib/total?style=for-the-badge)](https://github.com/martin-olivier/dylib/releases/download/v1.8.0/dylib.hpp) +[![GitHub download](https://img.shields.io/github/downloads/martin-olivier/dylib/total?style=for-the-badge)](https://github.com/martin-olivier/dylib/releases/download/v1.8.1/dylib.hpp) The goal of this C++ Library is to load dynamic libraries (.so, .dll, .dylib) and access its functions and global variables at runtime. @@ -19,7 +19,7 @@ Works on `Linux`, `Windows`, `MacOS` # Installation -Click [HERE](https://github.com/martin-olivier/dylib/releases/download/v1.8.0/dylib.hpp) to download the dylib header file +Click [HERE](https://github.com/martin-olivier/dylib/releases/download/v1.8.1/dylib.hpp) to download the dylib header file `⭐ Don't forget to put a star if you like the project!` # Documentation diff --git a/dylib.hpp b/dylib.hpp index 1139353..00dd978 100644 --- a/dylib.hpp +++ b/dylib.hpp @@ -2,7 +2,7 @@ * \file dylib.hpp * \brief Cross-platform Dynamic Library Loader * \author Martin Olivier - * \version 1.8.0 + * \version 1.8.1 * * MIT License * Copyright (c) 2022 Martin Olivier @@ -47,13 +47,13 @@ class dylib } static char *get_error_message() noexcept { - constexpr size_t bufSize = 512; - auto errorCode = GetLastError(); - if (!errorCode) + constexpr size_t buf_size = 512; + auto error_code = GetLastError(); + if (!error_code) return nullptr; - static char msg[bufSize]; + static char msg[buf_size]; auto lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US); - const DWORD len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, errorCode, lang, msg, bufSize, nullptr); + const DWORD len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error_code, lang, msg, buf_size, nullptr); if (len > 0) return msg; return nullptr; @@ -149,9 +149,8 @@ class dylib dylib(const dylib&) = delete; dylib& operator=(const dylib&) = delete; - dylib(dylib &&other) noexcept + dylib(dylib &&other) noexcept : m_handle(other.m_handle) { - m_handle = other.m_handle; other.m_handle = nullptr; } @@ -234,10 +233,10 @@ class dylib template std::function get_function(const char *name) const { - if (!m_handle) - throw handle_error(get_missing_handle_error(name)); if (!name) throw symbol_error(get_symbol_error("(nullptr)")); + if (!m_handle) + throw handle_error(get_missing_handle_error(name)); auto sym = get_symbol(name); if (!sym) throw symbol_error(get_symbol_error(name)); @@ -261,10 +260,10 @@ class dylib template T &get_variable(const char *name) const { - if (!m_handle) - throw handle_error(get_missing_handle_error(name)); if (!name) throw symbol_error(get_symbol_error("(nullptr)")); + if (!m_handle) + throw handle_error(get_missing_handle_error(name)); auto sym = get_symbol(name); if (!sym) throw symbol_error(get_symbol_error(name)); @@ -287,10 +286,10 @@ class dylib */ bool has_symbol(const char *symbol) const noexcept { - if (!m_handle) - return false; if (!symbol) return false; + if (!m_handle) + return false; return get_symbol(symbol) != nullptr; } diff --git a/tests/tests.cpp b/tests/tests.cpp index 1461dcc..a670510 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -64,6 +64,12 @@ TEST(ctor, bad_library) } } +TEST(multiple_handles, basic_test) +{ + dylib libA("./dynamic_lib", dylib::extension); + dylib libB("./dynamic_lib", dylib::extension); +} + TEST(dtor, mutiple_open_close) { try { @@ -166,7 +172,7 @@ TEST(bad_arguments, null_pointer) } try { dylib lib(std::string("./dynamic_lib") + std::string(dylib::extension)); - auto nothing = lib.get_function(nullptr); + lib.get_function(nullptr); EXPECT_EQ(true, false); } catch (const dylib::symbol_error &) { @@ -180,6 +186,22 @@ TEST(bad_arguments, null_pointer) catch (const dylib::symbol_error &) { EXPECT_EQ(true, true); } + try { + dylib lib; + lib.get_function(nullptr); + EXPECT_EQ(true, false); + } + catch (const dylib::symbol_error &) { + EXPECT_EQ(true, true); + } + try { + dylib lib; + lib.get_variable(nullptr); + EXPECT_EQ(true, false); + } + catch (const dylib::symbol_error &) { + EXPECT_EQ(true, true); + } } TEST(bad_arguments, handle_and_ext)