[libcamera-devel] [PATCH 1/4] cam: options: fix access to uninit variable

Tomi Valkeinen tomi.valkeinen at iki.fi
Wed Oct 7 11:22:36 CEST 2020


operator[] doesn't check if the option exists in the values_ map, so it
can return a pointer to location outside the map.

Fix by returning an empty OptionValue if the option is not found.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen at iki.fi>
---
 src/cam/options.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/cam/options.cpp b/src/cam/options.cpp
index 77b3cc1..8fc1c42 100644
--- a/src/cam/options.cpp
+++ b/src/cam/options.cpp
@@ -61,7 +61,12 @@ bool OptionsBase<T>::isSet(const T &opt) const
 template<typename T>
 const OptionValue &OptionsBase<T>::operator[](const T &opt) const
 {
-	return values_.find(opt)->second;
+	static const OptionValue s_empty;
+
+	auto it = values_.find(opt);
+	if (it != values_.end())
+		return it->second;
+	return s_empty;
 }
 
 template<typename T>
-- 
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki.
Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki



More information about the libcamera-devel mailing list