Skip to content

Issue in splitStringIntoTrimmedItems Function Handling Carriage Return and Form Feed Characters #1408

@zhihaoshang

Description

@zhihaoshang

Operating System:

Linux shangzh-VMware-Virtual-Platform 6.11.0-21-generic #21~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 24 16:52:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

ROS version or commit hash:

ros2 jazzy

RMW implementation (if applicable):

No response

RMW Configuration (if applicable):

No response

Client library (if applicable):

rviz

'ros2 doctor --report' output

ros2 doc --report
   NETWORK CONFIGURATION
inet         : 127.0.0.1
inet4        : ['127.0.0.1']
inet6        : ['::1']
netmask      : 255.0.0.0
device       : lo
flags        : 73<UP,LOOPBACK,RUNNING>
mtu          : 65536
inet         : 192.168.148.137
inet4        : ['192.168.148.137']
ether        : 00:0c:29:be:c8:19
inet6        : ['fe80::20c:29ff:febe:c819%ens33']
netmask      : 255.255.255.0
device       : ens33
flags        : 4163<BROADCAST,UP,MULTICAST,RUNNING>
mtu          : 1500
broadcast    : 192.168.148.255

   PLATFORM INFORMATION
system           : Linux
platform info    : Linux-6.11.0-21-generic-x86_64-with-glibc2.39
release          : 6.11.0-21-generic
processor        : x86_64

   QOS COMPATIBILITY LIST
compatibility status    : No publisher/subscriber pairs found

   RMW MIDDLEWARE
middleware name    : rmw_fastrtps_cpp

   TOPIC LIST
topic               : none
publisher count     : 0
subscriber count    : 0

Steps to reproduce issue

Environment

OS Version: Ubuntu 24.04
rviz version: ros2 jazzy
Compiler name and version number: Ubuntu clang version 18.1.3
Source or binary build?
source build
build options: --mixin asan-gcc

Description:

There is an issue in the splitStringIntoTrimmedItems function where the handling of whitespace characters is incorrect. Carriage return (\r) and form feed (\f) characters are treated as delimiters, causing unintended splitting of the string. The function currently uses std::isspace to identify and remove whitespace characters, but std::isspace also considers carriage return and form feed as whitespace, leading to incorrect splitting of strings that contain these characters.

Test Case

#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "../../src/rviz_rendering/string_helper.cpp"  // NOLINT (build/include)
using namespace ::testing;  // NOLINT

TEST(String_Helper__Test, handles_input_with_various_whitespace_characters) {
  std::vector<std::string> expected {"String1", "String2", "String3"};
  std::string test_string("String1\n\t  String2\r  \f String3");
  std::vector<std::string> actual = rviz_rendering::string_helper::splitStringIntoTrimmedItems(
    test_string, '\n');
  ASSERT_THAT(expected, Eq(actual));
}

Output

Running main() from gmock_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from String_Helper__Test
[ RUN      ] String_Helper__Test.handles_input_with_various_whitespace_characters
/home/shangzh/ros2_jazzy/src/ros2/rviz/rviz_rendering/test/rviz_rendering/string_helper_test.cpp:13: Failure
Value of: expected
Expected: is equal to { "String1", "String2\r  \f String3" }
  Actual: { "String1", "String2", "String3" }

[  FAILED  ] String_Helper__Test.handles_input_with_various_whitespace_characters (1 ms)
[----------] 1 test from String_Helper__Test (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] String_Helper__Test.handles_input_with_various_whitespace_characters

 1 FAILED TEST

Expected behavior

When the input string contains carriage return (\r), newline (\n), or form feed (\f) characters, the splitStringIntoTrimmedItems function should only split the string based on the specified delimiter (e.g., newline \n), without treating these whitespace characters as delimiters. These characters should remain part of the string.

Example:

Input: "String1\n\t String2\r \f String3"

Expected output: {"String1", "String2\r \f String3"}

Actual behavior

The function mistakenly treats carriage return (\r) and form feed (\f) as whitespace characters and splits the string at these points, which results in output that differs from the expected result.

Example:

Input: "String1\n\t String2\r \f String3"

Actual output: {"String1", "String2", "String3"}

Additional information

Suggested Fix

1.Modify the whitespace handling logic: Currently, std::isspace is used to identify whitespace characters, but this mistakenly considers carriage return (\r) and form feed (\f) as whitespace. The logic should be adjusted to prevent these characters from being treated as whitespace during the split operation.

2.Clarify whitespace character handling: Depending on the requirements, clearly define which characters should be treated as whitespace (e.g., spaces, tabs) and which characters (such as carriage return and form feed) should be preserved in the string.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghelp wantedExtra attention is needed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions