Copilot commented on code in PR #13827:
URL: https://github.com/apache/trafficserver/pull/13827#discussion_r4352154827


##########
tests/fuzzing/fuzz_http_compat.cc:
##########
@@ -0,0 +1,145 @@
+/** @file
+
+  Fuzzing HTTP compatibility parsing and Vary header matching.
+
+  @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 "proxy/hdrs/HTTP.h"
+#include "proxy/hdrs/HttpCompat.h"
+#include "tscore/Diags.h"
+#include "tsutil/PostScript.h"
+
+#include <algorithm>
+#include <array>
+#include <cstddef>
+#include <cstdint>
+#include <string>
+#include <string_view>
+
+extern int cmd_disable_pfreelist;
+
+namespace
+{
+constexpr size_t  MAX_INPUT_LENGTH          = 16 * 1024;
+constexpr int     OUTPUT_BUF_SIZE           = 256;
+constexpr uint8_t FLAG_COPY_COMMA_LIST      = 1u << 0;
+constexpr uint8_t FLAG_COPY_SEMICOLON_LIST  = 1u << 1;
+constexpr uint8_t FLAG_COPY_TOKEN_LIST      = 1u << 2;
+constexpr uint8_t FLAG_SEMICOLON_SEPARATOR  = 1u << 3;
+constexpr uint8_t FLAG_IGNORE_WILDCARDS     = 1u << 4;
+constexpr uint8_t FLAG_TRIM_TOKEN_QUOTES    = 1u << 5;
+constexpr uint8_t FLAG_FIRST_FIELD_MISSING  = 1u << 6;
+constexpr uint8_t FLAG_SECOND_FIELD_MISSING = 1u << 7;
+
+void
+exercise_lists(const std::string &value, const std::string &candidate, uint8_t 
flags, int output_length)
+{
+  StrList                           comma_list((flags & FLAG_COPY_COMMA_LIST) 
!= 0);
+  StrList                           semicolon_list((flags & 
FLAG_COPY_SEMICOLON_LIST) != 0);
+  StrList                           token_list((flags & FLAG_COPY_TOKEN_LIST) 
!= 0);
+  std::array<char, OUTPUT_BUF_SIZE> output{};
+  std::array<char, OUTPUT_BUF_SIZE> type{};
+  std::array<char, OUTPUT_BUF_SIZE> subtype{};
+  int                               matching_length  = 0;
+  int                               matching_index   = 0;
+  int                               value_length     = 
static_cast<int>(value.size());
+  int                               candidate_length = 
static_cast<int>(candidate.size());
+  char                              separator        = (flags & 
FLAG_SEMICOLON_SEPARATOR) != 0 ? ';' : ',';
+  bool                              ignore_wildcards = (flags & 
FLAG_IGNORE_WILDCARDS) != 0;
+
+  HttpCompat::parse_comma_list(&comma_list, value.c_str(), value_length);
+  HttpCompat::parse_semicolon_list(&semicolon_list, value.c_str(), 
value_length);
+  HttpCompat::parse_tok_list(&token_list, (flags & FLAG_TRIM_TOKEN_QUOTES) != 
0, value.c_str(), value_length, separator);
+  HttpCompat::lookup_param_in_strlist(&semicolon_list, "q", output.data(), 
output_length);
+  HttpCompat::lookup_param_in_strlist(&semicolon_list, "charset", 
output.data(), output_length);
+  HttpCompat::lookup_param_in_semicolon_string(value.c_str(), value_length, 
candidate.c_str(), output.data(), output_length);
+  HttpCompat::find_Q_param_in_strlist(&semicolon_list);
+  HttpCompat::match_accept_language(candidate.c_str(), candidate_length, 
&comma_list, &matching_length, &matching_index,
+                                    ignore_wildcards);
+  HttpCompat::match_accept_charset(candidate.c_str(), candidate_length, 
&comma_list, &matching_index, ignore_wildcards);
+  HttpCompat::parse_mime_type(value.c_str(), type.data(), subtype.data(), 
output_length, output_length);
+  HttpCompat::parse_mime_type_with_len(value.c_str(), value_length, 
type.data(), subtype.data(), output_length, output_length);
+}
+
+MIMEField *
+add_accept_language_field(HTTPHdr &header, std::string_view value)
+{
+  MIMEField *field = header.field_create("Accept-Language");
+
+  field->value_set(header.m_heap, header.m_mime, value);
+  header.field_attach(field);
+  return field;
+}
+
+void
+exercise_vary(const std::string &first, const std::string &second, uint8_t 
flags)
+{
+  HTTPHdr        first_header;
+  HTTPHdr        second_header;
+  ts::PostScript cleanup([&]() {
+    second_header.destroy();
+    first_header.destroy();
+  });
+
+  first_header.create(HTTPType::REQUEST);
+  second_header.create(HTTPType::REQUEST);
+  MIMEField *first_field  = (flags & FLAG_FIRST_FIELD_MISSING) != 0 ? nullptr 
: add_accept_language_field(first_header, first);
+  MIMEField *second_field = (flags & FLAG_SECOND_FIELD_MISSING) != 0 ? nullptr 
: add_accept_language_field(second_header, second);
+
+  // Missing selecting headers are supported: both absent match, only one 
absent does not.
+  HttpCompat::do_vary_header_values_match(first_field, second_field);
+  if (first_field && second_field) {
+    HttpCompat::do_vary_header_values_match(first_field, first_field);
+    // Match the swapped values attached to the opposite headers.
+    MIMEField *swapped_first  = add_accept_language_field(first_header, 
second);
+    MIMEField *swapped_second = add_accept_language_field(second_header, 
first);
+    HttpCompat::do_vary_header_values_match(swapped_first, swapped_second);
+  }
+}
+} // namespace
+
+extern "C" int
+LLVMFuzzerInitialize(int *, char ***)
+{
+  cmd_disable_pfreelist = true;
+  DiagsPtr::set(new Diags("fuzzing", "", "", nullptr));
+  http_init();
+  return 0;
+}
+
+extern "C" int
+LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+  if (size < 3 || size > MAX_INPUT_LENGTH) {
+    return 0;
+  }
+
+  uint8_t     flags         = data[0];
+  int         output_length = std::min<int>(static_cast<int>(data[1]) + 1, 
OUTPUT_BUF_SIZE);

Review Comment:
   `output_length` can become exactly `OUTPUT_BUF_SIZE` (256). If any of the 
downstream `HttpCompat::*` helpers treat the provided length as “max chars 
excluding the NUL terminator” (or write `len+1`), the harness buffers 
(`std::array<char, OUTPUT_BUF_SIZE>`) can overflow, causing crashes in the 
fuzzer harness rather than the target code. To keep the harness safe, cap 
`output_length` to at most `OUTPUT_BUF_SIZE - 1` (or pass the full array size 
and let the callee clamp) so there is always room for a terminator regardless 
of API conventions.



##########
tests/fuzzing/fuzz_http_compat.cc:
##########
@@ -0,0 +1,145 @@
+/** @file
+
+  Fuzzing HTTP compatibility parsing and Vary header matching.
+
+  @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 "proxy/hdrs/HTTP.h"
+#include "proxy/hdrs/HttpCompat.h"
+#include "tscore/Diags.h"
+#include "tsutil/PostScript.h"
+
+#include <algorithm>
+#include <array>
+#include <cstddef>
+#include <cstdint>
+#include <string>
+#include <string_view>
+
+extern int cmd_disable_pfreelist;
+
+namespace
+{
+constexpr size_t  MAX_INPUT_LENGTH          = 16 * 1024;
+constexpr int     OUTPUT_BUF_SIZE           = 256;
+constexpr uint8_t FLAG_COPY_COMMA_LIST      = 1u << 0;
+constexpr uint8_t FLAG_COPY_SEMICOLON_LIST  = 1u << 1;
+constexpr uint8_t FLAG_COPY_TOKEN_LIST      = 1u << 2;
+constexpr uint8_t FLAG_SEMICOLON_SEPARATOR  = 1u << 3;
+constexpr uint8_t FLAG_IGNORE_WILDCARDS     = 1u << 4;
+constexpr uint8_t FLAG_TRIM_TOKEN_QUOTES    = 1u << 5;
+constexpr uint8_t FLAG_FIRST_FIELD_MISSING  = 1u << 6;
+constexpr uint8_t FLAG_SECOND_FIELD_MISSING = 1u << 7;
+
+void
+exercise_lists(const std::string &value, const std::string &candidate, uint8_t 
flags, int output_length)
+{
+  StrList                           comma_list((flags & FLAG_COPY_COMMA_LIST) 
!= 0);
+  StrList                           semicolon_list((flags & 
FLAG_COPY_SEMICOLON_LIST) != 0);
+  StrList                           token_list((flags & FLAG_COPY_TOKEN_LIST) 
!= 0);
+  std::array<char, OUTPUT_BUF_SIZE> output{};
+  std::array<char, OUTPUT_BUF_SIZE> type{};
+  std::array<char, OUTPUT_BUF_SIZE> subtype{};
+  int                               matching_length  = 0;
+  int                               matching_index   = 0;
+  int                               value_length     = 
static_cast<int>(value.size());
+  int                               candidate_length = 
static_cast<int>(candidate.size());
+  char                              separator        = (flags & 
FLAG_SEMICOLON_SEPARATOR) != 0 ? ';' : ',';
+  bool                              ignore_wildcards = (flags & 
FLAG_IGNORE_WILDCARDS) != 0;
+
+  HttpCompat::parse_comma_list(&comma_list, value.c_str(), value_length);
+  HttpCompat::parse_semicolon_list(&semicolon_list, value.c_str(), 
value_length);
+  HttpCompat::parse_tok_list(&token_list, (flags & FLAG_TRIM_TOKEN_QUOTES) != 
0, value.c_str(), value_length, separator);
+  HttpCompat::lookup_param_in_strlist(&semicolon_list, "q", output.data(), 
output_length);
+  HttpCompat::lookup_param_in_strlist(&semicolon_list, "charset", 
output.data(), output_length);
+  HttpCompat::lookup_param_in_semicolon_string(value.c_str(), value_length, 
candidate.c_str(), output.data(), output_length);
+  HttpCompat::find_Q_param_in_strlist(&semicolon_list);
+  HttpCompat::match_accept_language(candidate.c_str(), candidate_length, 
&comma_list, &matching_length, &matching_index,
+                                    ignore_wildcards);
+  HttpCompat::match_accept_charset(candidate.c_str(), candidate_length, 
&comma_list, &matching_index, ignore_wildcards);
+  HttpCompat::parse_mime_type(value.c_str(), type.data(), subtype.data(), 
output_length, output_length);
+  HttpCompat::parse_mime_type_with_len(value.c_str(), value_length, 
type.data(), subtype.data(), output_length, output_length);
+}
+
+MIMEField *
+add_accept_language_field(HTTPHdr &header, std::string_view value)
+{
+  MIMEField *field = header.field_create("Accept-Language");
+
+  field->value_set(header.m_heap, header.m_mime, value);
+  header.field_attach(field);
+  return field;
+}
+
+void
+exercise_vary(const std::string &first, const std::string &second, uint8_t 
flags)
+{
+  HTTPHdr        first_header;
+  HTTPHdr        second_header;
+  ts::PostScript cleanup([&]() {
+    second_header.destroy();
+    first_header.destroy();
+  });
+
+  first_header.create(HTTPType::REQUEST);
+  second_header.create(HTTPType::REQUEST);
+  MIMEField *first_field  = (flags & FLAG_FIRST_FIELD_MISSING) != 0 ? nullptr 
: add_accept_language_field(first_header, first);
+  MIMEField *second_field = (flags & FLAG_SECOND_FIELD_MISSING) != 0 ? nullptr 
: add_accept_language_field(second_header, second);
+
+  // Missing selecting headers are supported: both absent match, only one 
absent does not.
+  HttpCompat::do_vary_header_values_match(first_field, second_field);
+  if (first_field && second_field) {
+    HttpCompat::do_vary_header_values_match(first_field, first_field);
+    // Match the swapped values attached to the opposite headers.
+    MIMEField *swapped_first  = add_accept_language_field(first_header, 
second);
+    MIMEField *swapped_second = add_accept_language_field(second_header, 
first);
+    HttpCompat::do_vary_header_values_match(swapped_first, swapped_second);
+  }
+}
+} // namespace
+
+extern "C" int
+LLVMFuzzerInitialize(int *, char ***)
+{
+  cmd_disable_pfreelist = true;

Review Comment:
   `cmd_disable_pfreelist` is declared as `extern int` but is assigned a 
boolean literal. While it will work (as `true` converts to `1`), fuzz builds 
often run with stricter warnings; using an explicit integer value (e.g., `1`) 
avoids signed/boolean conversion warnings and makes the intent clearer.



-- 
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]

Reply via email to