From 7461a2fe0a9cdb3eb1c38e60fbf86a5f3fbb42dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 24 Jan 2021 14:34:08 +0100 Subject: [PATCH] Vk: well OF COURSE I made an error when using std::lower_bound()! Goddammit, C++, why the hell all APIs have to be so error prone. You didn't learn from C's mistakes at all, did you. --- src/Magnum/Vk/ExtensionProperties.cpp | 2 +- src/Magnum/Vk/Test/ExtensionPropertiesVkTest.cpp | 3 +++ src/Magnum/Vk/Test/LayerPropertiesVkTest.cpp | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Magnum/Vk/ExtensionProperties.cpp b/src/Magnum/Vk/ExtensionProperties.cpp index 386d8f6c0..f75951be2 100644 --- a/src/Magnum/Vk/ExtensionProperties.cpp +++ b/src/Magnum/Vk/ExtensionProperties.cpp @@ -145,7 +145,7 @@ UnsignedInt ExtensionProperties::revision(const Containers::StringView extension /* Thanks, C++, for forcing me to have a larger bug surface instead of providing a library helper to find the damn thing. */ auto found = std::lower_bound(_names.begin(), _names.end(), extension); - if(*found != extension) return 0; + if(found == _names.end() || *found != extension) return 0; /* The view target is contents of the VkExtensionProperties structure, the revision is stored nearby */ diff --git a/src/Magnum/Vk/Test/ExtensionPropertiesVkTest.cpp b/src/Magnum/Vk/Test/ExtensionPropertiesVkTest.cpp index 25148f7f6..d57deaf11 100644 --- a/src/Magnum/Vk/Test/ExtensionPropertiesVkTest.cpp +++ b/src/Magnum/Vk/Test/ExtensionPropertiesVkTest.cpp @@ -208,6 +208,9 @@ void ExtensionPropertiesVkTest::namedRevision() { /* Unknown extensions return 0 */ CORRADE_COMPARE(properties.revision("VK_this_doesnt_exist"), 0); + /* Verify that we don't dereference garbage when std::lower_bound() returns + `last` */ + CORRADE_COMPARE(properties.revision("ZZZZZ"), 0); /* Verify the overloads that take our extension wrappers work as well */ CORRADE_COMPARE_AS(properties.revision(), 0, diff --git a/src/Magnum/Vk/Test/LayerPropertiesVkTest.cpp b/src/Magnum/Vk/Test/LayerPropertiesVkTest.cpp index c10177589..7bea566c6 100644 --- a/src/Magnum/Vk/Test/LayerPropertiesVkTest.cpp +++ b/src/Magnum/Vk/Test/LayerPropertiesVkTest.cpp @@ -125,6 +125,9 @@ void LayerPropertiesVkTest::isSupported() { LayerProperties properties = enumerateLayerProperties(); CORRADE_VERIFY(!properties.isSupported("this layer doesn't exist")); + /* Verify that we don't dereference garbage when std::lower_bound() returns + `last` */ + CORRADE_VERIFY(!properties.isSupported("ZZZZZ")); if(!properties.count()) CORRADE_SKIP("The driver reported no instance layers, can't fully test.");