From b2ced870abf9df71e6efff3244eb6fb40e46a449 Mon Sep 17 00:00:00 2001 From: "Evgeniy A. Dushistov" Date: Tue, 4 Jul 2017 22:30:26 +0300 Subject: [PATCH] fix potential undefined behaviour fix #19 --- src/sdcv.cpp | 2 +- src/stardict_lib.cpp | 1 - src/utils.hpp | 23 ++++++++--------------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/sdcv.cpp b/src/sdcv.cpp index 870555f..d7f08e9 100644 --- a/src/sdcv.cpp +++ b/src/sdcv.cpp @@ -157,7 +157,7 @@ int main(int argc, char *argv[]) try { }); std::list order_list; - if (use_dict_list) { + if (use_dict_list != nullptr) { for (auto &&x : bookname_to_ifo) { gchar **p = get_impl(use_dict_list); for (; *p != nullptr; ++p) diff --git a/src/stardict_lib.cpp b/src/stardict_lib.cpp index 89cdef7..2ea23f9 100644 --- a/src/stardict_lib.cpp +++ b/src/stardict_lib.cpp @@ -89,7 +89,6 @@ bool DictInfo::load_from_ifo_file(const std::string& ifofilename, if (!g_str_has_prefix( g_str_has_prefix(get_impl(buffer), (const gchar *)(utf8_bom)) ? get_impl(buffer) + 3 : get_impl(buffer), magic_data)) { - g_free(buffer); return false; } diff --git a/src/utils.hpp b/src/utils.hpp index 637658b..0079fd7 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -27,6 +27,14 @@ public: } } + friend inline bool operator==(const ResourceWrapper& lhs, nullptr_t) noexcept { + return !lhs.p_; + } + + friend inline bool operator!=(const ResourceWrapper& lhs, nullptr_t) noexcept { + return !!lhs.p_; + } + friend inline T *get_impl(const ResourceWrapper& rw) { return rw.p_; } @@ -39,21 +47,6 @@ private: T *p_; void free_resource() { if (p_) unref_res(p_); } - -// Helper for enabling 'if (sp)' - struct Tester { - Tester() {} - private: - void operator delete(void*); - }; -public: - // enable 'if (sp)' - operator Tester*() const { - if (!*this) - return 0; - static Tester t; - return &t; - } }; namespace glib {