Skip to content

Commit

Permalink
fix: crash when nullptr is passed as argument in get_function or get_…
Browse files Browse the repository at this point in the history
…variable when no dynamic lib is currently loaded
  • Loading branch information
martin-olivier committed Feb 7, 2022
1 parent 48f3a78 commit d1fb984
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/)

Expand All @@ -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.

Expand All @@ -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
Expand Down
27 changes: 13 additions & 14 deletions dylib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -234,10 +233,10 @@ class dylib
template<typename T>
std::function<T> 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));
Expand All @@ -261,10 +260,10 @@ class dylib
template<typename T>
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));
Expand All @@ -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;
}

Expand Down
24 changes: 23 additions & 1 deletion tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<void()>(nullptr);
lib.get_function<void()>(nullptr);
EXPECT_EQ(true, false);
}
catch (const dylib::symbol_error &) {
Expand All @@ -180,6 +186,22 @@ TEST(bad_arguments, null_pointer)
catch (const dylib::symbol_error &) {
EXPECT_EQ(true, true);
}
try {
dylib lib;
lib.get_function<void()>(nullptr);
EXPECT_EQ(true, false);
}
catch (const dylib::symbol_error &) {
EXPECT_EQ(true, true);
}
try {
dylib lib;
lib.get_variable<void *>(nullptr);
EXPECT_EQ(true, false);
}
catch (const dylib::symbol_error &) {
EXPECT_EQ(true, true);
}
}

TEST(bad_arguments, handle_and_ext)
Expand Down

0 comments on commit d1fb984

Please sign in to comment.