You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by wk...@apache.org on 2022/09/06 15:28:35 UTC
[trafficserver] branch master updated: Fix strerror_r() usage in ts::Strerror class. (#9078)
This is an automated email from the ASF dual-hosted git repository.
wkaras pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new 0b921a157 Fix strerror_r() usage in ts::Strerror class. (#9078)
0b921a157 is described below
commit 0b921a157b019d5f5e354e300c3a82fd63a9d042
Author: Walt Karas <wk...@verizonmedia.com>
AuthorDate: Tue Sep 6 10:28:28 2022 -0500
Fix strerror_r() usage in ts::Strerror class. (#9078)
Co-authored-by: Walt Karas <wk...@yahooinc.com>
---
include/tscpp/util/Strerror.h | 34 ++++++++++++++++++------------
src/tscpp/util/Makefile.am | 1 +
src/tscpp/util/unit_tests/test_Strerror.cc | 33 +++++++++++++++++++++++++++++
3 files changed, 55 insertions(+), 13 deletions(-)
diff --git a/include/tscpp/util/Strerror.h b/include/tscpp/util/Strerror.h
index eb1e6731c..a5d61aa7a 100644
--- a/include/tscpp/util/Strerror.h
+++ b/include/tscpp/util/Strerror.h
@@ -41,40 +41,48 @@ class Strerror
public:
Strerror(int err_num)
{
+ // Make sure there are no unused function warnings.
+ //
+ static_cast<void>(_handle(0));
+ static_cast<void>(_handle(nullptr));
+
// Handle either GNU or XSI version of strerror_r().
//
- if (!_success(strerror_r(err_num, _buf, 256))) {
+ if (!_handle(strerror_r(err_num, _buf, sizeof(_buf)))) {
_c_str = "strerror_r() call failed";
- } else {
- _buf[255] = '\0';
- _c_str = _buf;
}
-
- // Make sure there are no unused function warnings.
- //
- static_cast<void>(_success(0));
- static_cast<void>(_success(nullptr));
}
char const *
c_str() const
{
- return (_c_str);
+ return _c_str;
}
private:
char _buf[256];
char const *_c_str;
+ // For XSI-compliant strerror_r().
+ //
bool
- _success(int retval)
+ _handle(int retval)
{
- return retval == 0;
+ if (0 != retval) {
+ return false;
+ }
+ _buf[sizeof(_buf) - 1] = '\0';
+ _c_str = _buf;
+ return true;
}
+ // For GNU-specific strerror_r().
+ //
bool
- _success(char *retval)
+ _handle(char *retval)
{
+ _buf[sizeof(_buf) - 1] = '\0';
+ _c_str = retval;
return retval != nullptr;
}
};
diff --git a/src/tscpp/util/Makefile.am b/src/tscpp/util/Makefile.am
index f87a06bbf..b7ddf5295 100644
--- a/src/tscpp/util/Makefile.am
+++ b/src/tscpp/util/Makefile.am
@@ -41,6 +41,7 @@ test_tscpputil_SOURCES = \
unit_tests/test_LocalBuffer.cc \
unit_tests/test_MemSpan.cc \
unit_tests/test_PostScript.cc \
+ unit_tests/test_Strerror.cc \
unit_tests/test_TextView.cc \
unit_tests/test_ts_meta.cc
diff --git a/src/tscpp/util/unit_tests/test_Strerror.cc b/src/tscpp/util/unit_tests/test_Strerror.cc
new file mode 100644
index 000000000..cd021ca48
--- /dev/null
+++ b/src/tscpp/util/unit_tests/test_Strerror.cc
@@ -0,0 +1,33 @@
+/** @file
+
+ Unit tests for PostScript.h.
+
+ @section license License
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+#include <catch.hpp>
+
+#include <string.h>
+#include <errno.h>
+#include <tscpp/util/Strerror.h>
+
+TEST_CASE("Strerror", "[STE]")
+{
+ REQUIRE(strcmp(strerror(ERANGE), ts::Strerror{ERANGE}.c_str()) == 0);
+}