Skip to content

Commit

Permalink
Do not use dlopen() for zero-size libraries on FreeBSD and DragonFlyBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
saprykin committed Jun 13, 2016
1 parent 1323544 commit e2fa9c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/plibraryloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
# include <dlfcn.h>
#endif

/* FreeBSD may cause a segfault: https://reviews.freebsd.org/D5112,
* DragonFlyBSD as well, so we need to check a file size before calling dlopen()
*/
#if defined (P_OS_FREEBSD) || defined (P_OS_DRAGONFLY)
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
#endif

#ifdef P_OS_WIN
typedef HINSTANCE plibrary_handle;
#else
Expand Down Expand Up @@ -54,12 +63,27 @@ p_library_loader_new (const pchar *path)
{
PLibraryLoader *loader;
plibrary_handle handle;
#if defined (P_OS_FREEBSD) || defined (P_OS_DRAGONFLY)
struct stat stat_buf;
#endif

loader = NULL;

if (!p_file_is_exists (path))
return NULL;

#if defined (P_OS_FREEBSD) || defined (P_OS_DRAGONFLY)
if (P_UNLIKELY (stat (path, &stat_buf) != 0)) {
P_ERROR ("PLibraryLoader: failed to call stat()");
return NULL;
}

if (P_UNLIKELY (stat_buf.st_size == 0)) {
P_ERROR ("PLibraryLoader: unable to handle zero-size file");
return NULL;
}
#endif

#ifdef P_OS_WIN
if (P_UNLIKELY ((handle = LoadLibraryA (path)) == NULL)) {
P_ERROR ("PLibraryLoader: failed to call LoadLibraryA()");
Expand Down
3 changes: 0 additions & 3 deletions tests/plibraryloader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ BOOST_AUTO_TEST_CASE (plibraryloader_nomem_test)

int argCount = boost::unit_test::framework::master_test_suite().argc;

/* FreeBSD may cause a segfault :https://reviews.freebsd.org/D5112 */
#ifndef P_OS_FREEBSD
BOOST_CHECK (p_library_loader_new ("." P_DIR_SEPARATOR "p_empty_file.txt") == NULL);
#endif
BOOST_CHECK (p_library_loader_new (boost::unit_test::framework::master_test_suite().argv[argCount - 1]) == NULL);

#ifdef P_OS_WIN
Expand Down

0 comments on commit e2fa9c6

Please sign in to comment.