Copilot commented on code in PR #13574: URL: https://github.com/apache/trafficserver/pull/13574#discussion_r3822721450
########## tests/tools/plugins/null_transform_request.cc: ########## @@ -0,0 +1,162 @@ +/** @file + + Null request transform plugin hooked at TS_HTTP_READ_REQUEST_HDR_HOOK. + + Used by post_early_response_transform.test.py to reproduce a use-after-free + in HttpSM::state_read_server_response_header() when abort_tunnel() is called + while a request transform is active. The transform passes request body data + through unmodified. + + @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 <cstdio> +#include <cinttypes> + +#include "ts/ts.h" + +#define PLUGIN_NAME "null_transform_request" + +typedef struct { + TSVIO output_vio; + TSIOBuffer output_buffer; + TSIOBufferReader output_reader; +} TransformData; + +static TransformData * +transform_data_alloc() +{ + auto *data = static_cast<TransformData *>(TSmalloc(sizeof(TransformData))); + data->output_vio = nullptr; + data->output_buffer = nullptr; + data->output_reader = nullptr; + return data; +} + +static void +transform_data_destroy(TransformData *data) +{ + if (data) { + if (data->output_buffer) { + TSIOBufferDestroy(data->output_buffer); + } + TSfree(data); + } +} + +static void +handle_transform(TSCont contp) +{ + TSVConn output_conn = TSTransformOutputVConnGet(contp); + TSVIO input_vio = TSVConnWriteVIOGet(contp); + TransformData *data = static_cast<TransformData *>(TSContDataGet(contp)); + + if (!data) { + data = transform_data_alloc(); + data->output_buffer = TSIOBufferCreate(); + data->output_reader = TSIOBufferReaderAlloc(data->output_buffer); + data->output_vio = TSVConnWrite(output_conn, contp, data->output_reader, TSVIONBytesGet(input_vio)); + TSContDataSet(contp, data); + } + + if (!TSVIOBufferGet(input_vio)) { + TSVIONBytesSet(data->output_vio, TSVIONDoneGet(input_vio)); + TSVIOReenable(data->output_vio); + return; + } + + int64_t towrite = TSVIONTodoGet(input_vio); + if (towrite > 0) { + int64_t avail = TSIOBufferReaderAvail(TSVIOReaderGet(input_vio)); + if (towrite > avail) { + towrite = avail; + } + if (towrite > 0) { + TSIOBufferCopy(TSVIOBufferGet(data->output_vio), TSVIOReaderGet(input_vio), towrite, 0); + TSIOBufferReaderConsume(TSVIOReaderGet(input_vio), towrite); + TSVIONDoneSet(input_vio, TSVIONDoneGet(input_vio) + towrite); + } + } + + if (TSVIONTodoGet(input_vio) > 0) { + if (towrite > 0) { + TSVIOReenable(data->output_vio); + TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio); + } + } else { + TSVIONBytesSet(data->output_vio, TSVIONDoneGet(input_vio)); + TSVIOReenable(data->output_vio); + TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio); + } +} + +static int +null_transform(TSCont contp, TSEvent event, void * /* edata ATS_UNUSED */) +{ + if (TSVConnClosedGet(contp)) { Review Comment: `TSVConnClosedGet` expects a `TSVConn`, but `contp` is a `TSCont` in this handler. Because both are opaque pointers this will compile but is logically incorrect and can lead to undefined behavior. Use the relevant `TSVConn` (e.g., `TSTransformOutputVConnGet(contp)`) or handle closure via the appropriate close event instead of checking a continuation pointer. -- 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]
