Copilot commented on code in PR #13074:
URL: https://github.com/apache/trafficserver/pull/13074#discussion_r3057464922
##########
src/records/RecCore.cc:
##########
@@ -940,6 +941,12 @@ RecDumpRecords(RecT rec_type, RecDumpEntryCb callback,
void *edata)
callback(RECT_PLUGIN, edata, true, name.data(),
type == Metrics::MetricType::COUNTER ? TS_RECORDDATATYPE_COUNTER
: TS_RECORDDATATYPE_INT, &datum);
}
+
+ ts::Metrics::StaticString::instance().for_each([&](const std::string &name,
const std::string &value) {
+ datum.rec_string = const_cast<char *>(value.c_str());
+
+ callback(RECT_PLUGIN, edata, true, name.data(), TS_RECORDDATATYPE_STRING,
&datum);
+ });
Review Comment:
`ts::Metrics::StaticString::for_each()` invokes its callback while holding
the internal mutex (per `Metrics.h`). Calling `RecDumpRecords()`'s external
`callback(...)` under that lock risks deadlocks / long lock hold times if the
callback does anything non-trivial. Consider snapshotting the (name,value)
pairs while holding the mutex, then invoking `callback(...)` after releasing it.
##########
src/records/unit_tests/test_RecDumpRecords.cc:
##########
@@ -0,0 +1,81 @@
+/** @file
+
+ Catch-based tests for RecDumpRecords
+
+ @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 <catch2/catch_test_macros.hpp>
+#include <string>
+#include <vector>
+
+#include "../P_RecCore.h"
+#include "tsutil/Metrics.h"
+
+struct DumpEntry {
+ RecT rec_type;
+ int registered;
+ std::string name;
+ int data_type;
+ std::string string_value;
+ RecInt int_value{0};
+};
+
+static void
+collect_callback(RecT rec_type, void *edata, int registered, const char *name,
int data_type, RecData *datum)
+{
+ auto *entries = static_cast<std::vector<DumpEntry> *>(edata);
+ DumpEntry entry;
+
+ entry.rec_type = rec_type;
+ entry.registered = registered;
+ entry.name = name;
+ entry.data_type = data_type;
+
+ if (data_type == RECD_STRING && datum->rec_string != nullptr) {
+ entry.string_value = datum->rec_string;
+ } else if (data_type == RECD_INT || data_type == RECD_COUNTER) {
+ entry.int_value = datum->rec_int;
Review Comment:
`collect_callback()` reads `datum->rec_int` for `RECD_COUNTER` entries.
Since `RecData` is a union, counters should be read from `datum->rec_counter`
when `data_type == RECD_COUNTER` (reading a non-active union member is
undefined behavior and can trip UBSan even if the value isn't later asserted in
this test).
```suggestion
} else if (data_type == RECD_INT) {
entry.int_value = datum->rec_int;
} else if (data_type == RECD_COUNTER) {
entry.int_value = datum->rec_counter;
```
--
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]