Skip to content

Commit 87ba5a5

Browse files
committed
fix ::tolower usage
Supplying negative (signed) chars would trigger UB. Also make it work correctly for UTF-8 input.
1 parent 7bf60a7 commit 87ba5a5

9 files changed

Lines changed: 48 additions & 10 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ else()
504504
endif()
505505

506506
if(NOT VISTLE_GUI_ONLY)
507-
set(BOOST_OPT ${BOOST_OPT} iostreams locale)
507+
set(BOOST_OPT ${BOOST_OPT} iostreams)
508508
endif()
509509

510510
if(VISTLE_INTERNAL_BOOST_MPI)
@@ -546,6 +546,7 @@ vistle_find_package(
546546
thread
547547
regex
548548
timer
549+
locale
549550
${BOOST_REQ}
550551
OPTIONAL_COMPONENTS
551552
${BOOST_OPT})

lib/vistle/renderer/renderobject.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ std::string rgb_normalize_name(std::string name)
2121
const char *p = name.c_str();
2222
std::string colorname;
2323
while (*p) {
24-
if (!isspace(*p))
25-
colorname.push_back(tolower(*p));
24+
auto c = static_cast<unsigned char>(*p);
25+
if (!isspace(c))
26+
colorname.push_back(tolower(c));
2627
++p;
2728
}
2829
return colorname;

lib/vistle/util/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(util_SOURCES
1515
netpbmimage.cpp
1616
sleep.cpp
1717
stopwatch.cpp
18+
strings.cpp
1819
sysdep.cpp
1920
threadname.cpp
2021
tools.cpp
@@ -48,6 +49,7 @@ set(util_HEADERS
4849
sleep.h
4950
ssize_t.h
5051
stopwatch.h
52+
strings.h
5153
sysdep.h
5254
threadname.h
5355
tools.h
@@ -71,6 +73,8 @@ vistle_target_link_libraries(
7173
PRIVATE
7274
Boost::timer
7375
PRIVATE
76+
Boost::locale
77+
PRIVATE
7478
Threads::Threads
7579
PRIVATE
7680
${BOTAN_LIBRARIES})

lib/vistle/util/strings.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "strings.h"
2+
3+
#include <boost/locale/conversion.hpp>
4+
5+
namespace vistle {
6+
7+
std::string to_lower(const std::string &s)
8+
{
9+
return boost::locale::to_lower(s);
10+
}
11+
12+
std::string to_upper(const std::string &s)
13+
{
14+
return boost::locale::to_upper(s);
15+
}
16+
17+
} // namespace vistle

lib/vistle/util/strings.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef VISTLE_UTIL_STRINGS_H
2+
#define VISTLE_UTIL_STRINGS_H
3+
4+
#include <string>
5+
#include "export.h"
6+
7+
namespace vistle {
8+
9+
std::string V_UTILEXPORT to_lower(const std::string &s);
10+
std::string V_UTILEXPORT to_upper(const std::string &s);
11+
12+
} // namespace vistle
13+
#endif

module/read/ReadCsv/ReadCsv.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vistle/core/points.h>
55
#include <vistle/util/filesystem.h>
66
#include <vistle/util/meta.h>
7+
#include <vistle/util/strings.h>
78

89
#include <fstream>
910

@@ -133,10 +134,8 @@ bool ReadCsv::examine(const Parameter *param)
133134
m_choices = std::vector<std::string>{vistle::Reader::InvalidChoice};
134135
splitLine(m_choices, line, m_delimiter);
135136
auto lowerChoices = m_choices;
136-
std::transform(lowerChoices.begin(), lowerChoices.end(), lowerChoices.begin(), [](std::string s) {
137-
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
138-
return s;
139-
});
137+
std::transform(lowerChoices.begin(), lowerChoices.end(), lowerChoices.begin(),
138+
[](std::string s) { return to_lower(s); });
140139

141140
for (auto p: m_selectionParams) {
142141
setParameterChoices(p, m_choices);

module/read/ReadEnsight/EnFile.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <iostream>
2828
#include <cfloat>
2929
#include <boost/algorithm/string.hpp>
30+
#include <vistle/util/strings.h>
3031

3132
//#define DEBUG
3233

@@ -52,7 +53,7 @@ CaseFile::BinType EnFile::guessBinType(ReadEnsight *mod, const std::string &file
5253
fclose(in);
5354

5455
std::string firstLine(buf.begin(), buf.end());
55-
std::transform(firstLine.begin(), firstLine.end(), firstLine.begin(), tolower);
56+
firstLine = vistle::to_lower(firstLine);
5657

5758
if ((firstLine.find("c binary") != std::string::npos) || (firstLine.find("cbinary") != std::string::npos)) {
5859
ret = CaseFile::CBIN;

module/render/COVER/COVER.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <vistle/core/placeholder.h>
2020
#include <vistle/core/statetracker.h>
2121
#include <vistle/util/threadname.h>
22+
#include <vistle/util/strings.h>
2223

2324
#include <osg/Node>
2425
#include <osg/Group>
@@ -337,7 +338,7 @@ opencover::coVRAnimationManager::FillMode getAnimationFill(const vistle::Object:
337338
mode = texture->getAttribute(vistle::attribute::AnimationFill);
338339
if (mode.empty() && geometry)
339340
mode = geometry->getAttribute(vistle::attribute::AnimationFill);
340-
std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower);
341+
mode = to_lower(mode);
341342
if (mode == "last")
342343
return opencover::coVRAnimationManager::Last;
343344
else if (mode == "cycle")

module/render/COVER/plugin/RhrClient/RhrClient.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <vistle/rhr/rfbext.h>
4444
#include <vistle/util/crypto.h>
4545
#include <vistle/util/hostname.h>
46+
#include <vistle/util/strings.h>
4647
#include <vistle/module/module.h>
4748
#include <vistle/core/statetracker.h>
4849

@@ -547,7 +548,7 @@ bool RhrClient::init()
547548

548549
std::string confMode = covise::coCoviseConfig::getEntry("reprojectionMode", configKey);
549550
if (!confMode.empty()) {
550-
std::transform(confMode.begin(), confMode.end(), confMode.begin(), ::tolower);
551+
confMode = vistle::to_lower(confMode);
551552
if (confMode == "disable" || confMode == "asis")
552553
m_configuredMode = MultiChannelDrawer::AsIs;
553554
else if (confMode == "points")

0 commit comments

Comments
 (0)