forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eclipse-iceoryx#2254 from elBoberido/iox-1616-use-…
…thread-safe-strerror_r iox-eclipse-iceoryx#1616 Handle 'strerror_r' idiosyncrasies in the platform layer
- Loading branch information
Showing
10 changed files
with
219 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
iceoryx_platform/generic/include/iceoryx_platform/generic/string.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef IOX_PLATFORM_GENERIC_STRING_HPP | ||
#define IOX_PLATFORM_GENERIC_STRING_HPP | ||
|
||
#include <cstring> | ||
|
||
/// @brief Implements the GNU version of 'strerror_r' | ||
/// @param[in] errnum the error code to be converted to a string | ||
/// @param[out] buf the buffer to store the error message in case there is no static error message available for the | ||
/// given error code | ||
/// @param[in] buflen the length of the buffer to store the error message | ||
/// @return a pointer to a string containing the error message; this is either a pointer to an immutable static string | ||
/// or the provided buffer | ||
char* iox_gnu_strerror_r(int errnum, char* buf, size_t buflen); | ||
|
||
#endif // IOX_PLATFORM_GENERIC_STRING_HPP |
26 changes: 26 additions & 0 deletions
26
iceoryx_platform/generic/include/iceoryx_platform/string.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef IOX_PLATFORM_STRING_HPP | ||
#define IOX_PLATFORM_STRING_HPP | ||
|
||
#if __has_include("iceoryx_platform/override/string.hpp") | ||
#include "iceoryx_platform/override/string.hpp" | ||
#else | ||
#include "iceoryx_platform/generic/string.hpp" | ||
#endif // __has_include | ||
|
||
#endif // IOX_PLATFORM_STRING_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "iceoryx_platform/string.hpp" | ||
|
||
#ifndef IOX_PLATFORM_OVERRIDE_STRING_ALL | ||
|
||
#include <cerrno> | ||
|
||
#ifndef IOX_PLATFORM_OVERRIDE_STRING_STRERROR_R | ||
|
||
namespace | ||
{ | ||
[[maybe_unused]] char* strerror_r_gnu_xsi_unificaton(const int returnCode [[maybe_unused]], char* buf) | ||
{ | ||
return buf; | ||
} | ||
|
||
[[maybe_unused]] char* strerror_r_gnu_xsi_unificaton(char* msg, char* buf [[maybe_unused]]) | ||
{ | ||
return msg; | ||
} | ||
|
||
} // namespace | ||
|
||
char* iox_gnu_strerror_r(int errnum, char* buf, size_t buflen) | ||
{ | ||
return strerror_r_gnu_xsi_unificaton(strerror_r(errnum, buf, buflen), buf); | ||
} | ||
|
||
#endif | ||
|
||
#endif |
62 changes: 62 additions & 0 deletions
62
iceoryx_platform/test/moduletests/test_platform_string.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "iceoryx_platform/string.hpp" | ||
|
||
#include "test.hpp" | ||
|
||
namespace | ||
{ | ||
using namespace ::testing; | ||
|
||
TEST(STRING_test, strerror_r_of_known_erron_works) | ||
{ | ||
::testing::Test::RecordProperty("TEST_ID", "1fbf88c9-07c4-4959-9127-18a78db27c22"); | ||
constexpr size_t BUFFER_SIZE{1024}; | ||
char buf[BUFFER_SIZE]; | ||
buf[0] = 0; | ||
|
||
auto* error_string = iox_gnu_strerror_r(ENOENT, &buf[0], BUFFER_SIZE); | ||
|
||
EXPECT_THAT(error_string, StrEq("No such file or directory")); | ||
} | ||
|
||
TEST(STRING_test, strerror_r_of_unknown_error_works_when_buffer_is_large_enough) | ||
{ | ||
::testing::Test::RecordProperty("TEST_ID", "92213d70-61b7-47dd-beac-c86ce25b6991"); | ||
constexpr size_t BUFFER_SIZE{1024}; | ||
char buf[BUFFER_SIZE]; | ||
buf[0] = 0; | ||
|
||
auto* error_string = iox_gnu_strerror_r(123456789, &buf[0], BUFFER_SIZE); | ||
|
||
// on Linux this is 'Unknown error 123456789', on macOS 'Unknown error: 123456789' and on Windows 'Unknown error' | ||
EXPECT_THAT(error_string, HasSubstr("Unknown error")); | ||
} | ||
|
||
TEST(STRING_test, strerror_r_of_unknown_error_is_truncated_when_buffer_is_too_small) | ||
{ | ||
::testing::Test::RecordProperty("TEST_ID", "1dbba291-575f-495b-a174-0859b79980b1"); | ||
constexpr size_t BUFFER_SIZE{10}; | ||
char buf[BUFFER_SIZE]; | ||
buf[0] = 0; | ||
|
||
auto* error_string = iox_gnu_strerror_r(123456789, &buf[0], BUFFER_SIZE); | ||
|
||
EXPECT_THAT(error_string, StrEq("Unknown e")); | ||
} | ||
|
||
} // namespace |
24 changes: 24 additions & 0 deletions
24
iceoryx_platform/win/include/iceoryx_platform/override/string.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef IOX_PLATFORM_WIN_OVERRIDE_STRING_HPP | ||
#define IOX_PLATFORM_WIN_OVERRIDE_STRING_HPP | ||
|
||
#define IOX_PLATFORM_OVERRIDE_STRING_STRERROR_R | ||
|
||
#include "iceoryx_platform/generic/string.hpp" | ||
|
||
#endif // IOX_PLATFORM_WIN_OVERRIDE_STRING_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2024 by ekxide IO GmbH. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "iceoryx_platform/string.hpp" | ||
|
||
char* iox_gnu_strerror_r(int errnum, char* buf, size_t buflen) | ||
{ | ||
strerror_s(buf, buflen, errnum); | ||
return buf; | ||
} |