From 1c6c943b3b3fc13f9340352f52327eb7e376b898 Mon Sep 17 00:00:00 2001 From: Simon Schmeisser Date: Wed, 24 Oct 2018 11:46:06 +0200 Subject: [PATCH 1/2] use helper function strToDouble for locale independent parsing of colors --- urdf_model/include/urdf_model/color.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/urdf_model/include/urdf_model/color.h b/urdf_model/include/urdf_model/color.h index 505d9a6..46915a5 100644 --- a/urdf_model/include/urdf_model/color.h +++ b/urdf_model/include/urdf_model/color.h @@ -73,13 +73,10 @@ class Color { try { - rgba.push_back(std::stof(pieces[i])); + rgba.push_back(strToDouble(pieces[i].c_str())); } - catch (std::invalid_argument &/*e*/) { - return false; - } - catch (std::out_of_range &/*e*/) { - return false; + catch (std::runtime_error &/*e*/) { + throw ParseError("Unable to parse component [" + pieces[i] + "] to a double (while parsing a color value)"); } } } From 955edc34a6e6b6f7760b444b1ab5627227b581a1 Mon Sep 17 00:00:00 2001 From: "Simon Schmeisser (isys vision)" Date: Mon, 29 Oct 2018 12:11:58 +0100 Subject: [PATCH 2/2] Add range check for color components being within [0, 1] this solves the potential undefined behaviour in implicit conversion if a color component should be greater than the maximum float but smaller than the maximum double and allows us to reuse strToDouble safely --- urdf_model/include/urdf_model/color.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/urdf_model/include/urdf_model/color.h b/urdf_model/include/urdf_model/color.h index 46915a5..fec8ae0 100644 --- a/urdf_model/include/urdf_model/color.h +++ b/urdf_model/include/urdf_model/color.h @@ -73,7 +73,10 @@ class Color { try { - rgba.push_back(strToDouble(pieces[i].c_str())); + double piece = strToDouble(pieces[i].c_str()); + if ((piece < 0) || (piece > 1)) + throw ParseError("Component [" + pieces[i] + "] is outside the valid range for colors [0, 1]"); + rgba.push_back(piece); } catch (std::runtime_error &/*e*/) { throw ParseError("Unable to parse component [" + pieces[i] + "] to a double (while parsing a color value)");