Copilot commented on code in PR #13733: URL: https://github.com/apache/trafficserver/pull/13733#discussion_r4363352894
########## src/tscore/unit_tests/test_Demangle.cc: ########## @@ -0,0 +1,80 @@ +/** @file + + Tests for the exception description helpers. + + @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 "tscore/Demangle.h" + +#include <catch2/catch_test_macros.hpp> + +#include <stdexcept> +#include <string> + +namespace +{ +struct NotAnException { +}; + +std::string +describe_thrown(auto const &thrown) +{ + try { + throw thrown; + } catch (...) { + return ts::describe_current_exception(); + } +} +} // namespace + +TEST_CASE("demangle", "[libts][Demangle]") +{ + CHECK(ts::demangle(typeid(int)) == "int"); + CHECK(ts::demangle(typeid(std::runtime_error)).find("runtime_error") != std::string::npos); + CHECK(ts::demangle(typeid(NotAnException)).find("NotAnException") != std::string::npos); +} + +TEST_CASE("describe_current_exception", "[libts][Demangle]") +{ + SECTION("a std::exception reports its type and message") + { + auto const text = describe_thrown(std::runtime_error{"boom"}); + + CHECK(text.find("runtime_error") != std::string::npos); + CHECK(text.ends_with(": boom")); Review Comment: `std::string::ends_with` is C++20. If unit tests are compiled with C++17, this will not compile. Prefer a C++17-compatible suffix check (e.g., comparing a trailing substring) unless the build is explicitly pinned to C++20 for this target. ########## src/tscore/Demangle.cc: ########## @@ -0,0 +1,75 @@ +/** @file + + Readable C++ type names. + + @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 "tscore/Demangle.h" + +#include <cstdlib> +#include <cxxabi.h> +#include <exception> + +namespace ts +{ +std::string +demangle(std::type_info const &type) noexcept +{ + try { + int status = 0; + char *name = abi::__cxa_demangle(type.name(), nullptr, nullptr, &status); + std::string result; + + try { + result = status == 0 && name != nullptr ? name : type.name(); + } catch (...) { + std::free(name); + throw; + } + std::free(name); + return result; + } catch (...) { + return "<unknown>"; + } Review Comment: The manual `std::free()` management plus nested `try` makes this harder to read/maintain. Consider using a small RAII wrapper (e.g., `std::unique_ptr<char, Deleter>` with `std::free`) for the demangled buffer so cleanup is automatic and the nested `try/catch` can be removed while preserving exception safety and the `noexcept` contract. ########## src/tscore/unit_tests/test_Demangle.cc: ########## @@ -0,0 +1,80 @@ +/** @file + + Tests for the exception description helpers. + + @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 "tscore/Demangle.h" + +#include <catch2/catch_test_macros.hpp> + +#include <stdexcept> +#include <string> + +namespace +{ +struct NotAnException { +}; + +std::string +describe_thrown(auto const &thrown) +{ + try { + throw thrown; + } catch (...) { + return ts::describe_current_exception(); + } +} Review Comment: This test file uses C++20-only features (abbreviated function template via `auto` parameter). If the project/test targets are still built as C++17 (common for this codebase historically), this will fail to compile. Use a standard function template (`template <typename T>`) instead so the unit tests build under C++17 and newer. ########## tests/uranium_tests/pluginTest/crash_test/test_crash_test_throw.py: ########## @@ -0,0 +1,151 @@ +# 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. + +from pathlib import Path +import re +import time + +from tools.uranium.services import ATS, ATSFactory, Curl, OriginServer, ServiceFactory + + +def configure_origin(services: ServiceFactory) -> OriginServer: + """Create the origin used to establish a healthy baseline request. + + :param services: Factory owning support services and their cleanup. + """ + + origin = services.origin("origin") + origin.add_response( + { + "headers": "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n", + "body": "" + }, + { + "headers": "HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\n", + "body": "Hello", + }, + ) + return origin + + +def configure_ats(ats_factory: ATSFactory, *, _origin: OriginServer) -> ATS: + """Load the plugin that throws out of a hook and enable the crash-log helper. + + The terminate handler aborts, so ATS is expected to exit on SIGABRT. + + :param _origin: Test-local origin configured by the test. + :param ats_factory: Factory for isolated Traffic Server instances. + """ + + ats = ats_factory.create("ts", return_code=-6, enable_cache=False) Review Comment: `return_code=-6` assumes SIGABRT is signal number 6. For portability across platforms, use Python’s `signal` module (e.g., `return_code=-signal.SIGABRT`) instead of hard-coding the numeric value. ########## src/traffic_logcat/logcat.cc: ########## @@ -503,15 +506,17 @@ open_output_file(char *output_file_p) main -------------------------------------------------------------------------*/ +// At file scope so the exception handlers below can name these; a +// function-try-block handler is outside the function body's scope. +enum { + NO_ERROR = 0, + CMD_LINE_OPTION_ERROR = 1, + DATA_PROCESSING_ERROR = 2, +}; Review Comment: These enumerators are now injected into the file’s global scope. To avoid accidental name collisions within this translation unit, prefer wrapping this in an unnamed namespace and/or using an `enum class` (e.g., `enum class ErrorCode { ... }`) while still keeping it accessible to the function-try-block handler. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
