summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-05-25 01:58:13 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-06-20 15:12:26 +0300
commit839c4a5a480731bfbae231797fc974f5a3749b69 (patch)
tree02b4bc21859ad1963b9ef0c3d1a0c1a6099e53b1 /src
parent9dacead6156f6083f7e0a148f228a7e1ebb4d5a3 (diff)
libcamera: yaml_parser: Fix range checks for 32-bit integers
The strtol() and strtoul() functions return long integers, which may be larger than 32-bit integers. Add manual range checks. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Han-Lin Chen <hanlinchen@chromium.org>
Diffstat (limited to 'src')
-rw-r--r--src/libcamera/yaml_parser.cpp13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
index 9b6e70cb..bd4b501b 100644
--- a/src/libcamera/yaml_parser.cpp
+++ b/src/libcamera/yaml_parser.cpp
@@ -10,6 +10,7 @@
#include <cstdlib>
#include <errno.h>
#include <functional>
+#include <limits>
#include <libcamera/base/file.h>
#include <libcamera/base/log.h>
@@ -151,9 +152,11 @@ int32_t YamlObject::get(const int32_t &defaultValue, bool *ok) const
char *end;
errno = 0;
- int32_t value = std::strtol(value_.c_str(), &end, 10);
+ long value = std::strtol(value_.c_str(), &end, 10);
- if ('\0' != *end || errno == ERANGE)
+ if ('\0' != *end || errno == ERANGE ||
+ value < std::numeric_limits<int32_t>::min() ||
+ value > std::numeric_limits<int32_t>::max())
return defaultValue;
setOk(ok, true);
@@ -185,9 +188,11 @@ uint32_t YamlObject::get(const uint32_t &defaultValue, bool *ok) const
char *end;
errno = 0;
- uint32_t value = std::strtoul(value_.c_str(), &end, 10);
+ unsigned long value = std::strtoul(value_.c_str(), &end, 10);
- if ('\0' != *end || errno == ERANGE)
+ if ('\0' != *end || errno == ERANGE ||
+ value < std::numeric_limits<uint32_t>::min() ||
+ value > std::numeric_limits<uint32_t>::max())
return defaultValue;
setOk(ok, true);