diff --git a/src/plibraryloader.c b/src/plibraryloader.c index 69cb380b..148e3c6b 100644 --- a/src/plibraryloader.c +++ b/src/plibraryloader.c @@ -25,6 +25,15 @@ # include #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 +# include +# include +#endif + #ifdef P_OS_WIN typedef HINSTANCE plibrary_handle; #else @@ -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()"); diff --git a/tests/plibraryloader_test.cpp b/tests/plibraryloader_test.cpp index bf2742ff..a45b5433 100644 --- a/tests/plibraryloader_test.cpp +++ b/tests/plibraryloader_test.cpp @@ -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