Coverage Report

Created: 2026-10-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/telnet_scenario.cc
Line
Count
Source
1
/*
2
 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3
 *
4
 * SPDX-License-Identifier: curl
5
 */
6
7
/// @file
8
/// @brief Implementation of shared TELNET response normalization.
9
10
#include "proto_fuzzer/telnet_scenario.h"
11
12
#include <cstddef>
13
#include <string>
14
15
#include "proto_fuzzer/scenario_limits.h"
16
17
namespace proto_fuzzer {
18
19
namespace {
20
21
/// Remove a repeated-field suffix that the TELNET runtime cannot observe.
22
template <typename RepeatedField>
23
13.0k
void TrimRepeated(RepeatedField* field, std::size_t limit) {
24
13.0k
  const std::size_t size = static_cast<std::size_t>(field->size());
25
13.0k
  if (size > limit) {
26
17
    field->DeleteSubrange(static_cast<int>(limit), static_cast<int>(size - limit));
27
17
  }
28
13.0k
}
29
30
/// Retain one contiguous fragment without crossing either shared budget.
31
/// Counting IAC bytes conservatively covers negotiation and subnegotiation
32
/// replies without duplicating curl's state machine in the harness.
33
16.5k
bool BoundTelnetFragment(std::string* fragment, std::size_t* bytes_left, std::size_t* controls_left) {
34
16.5k
  std::size_t retained = 0;
35
2.00M
  while (retained < fragment->size() && retained < *bytes_left) {
36
1.98M
    if (static_cast<unsigned char>((*fragment)[retained]) == 0xff) {
37
16.2k
      if (*controls_left == 0) {
38
71
        break;
39
71
      }
40
16.2k
      --*controls_left;
41
16.2k
    }
42
1.98M
    ++retained;
43
1.98M
  }
44
45
16.5k
  const bool complete = retained == fragment->size();
46
16.5k
  fragment->resize(retained);
47
16.5k
  *bytes_left -= retained;
48
16.5k
  return complete;
49
16.5k
}
50
51
}  // namespace
52
53
/// Retain the response prefix that TelnetMockServer can preload safely.
54
///
55
/// TELNET runs synchronously inside curl_multi_perform(), so the harness
56
/// cannot deliver later fragments or drain an arbitrary number of replies
57
/// between parser transitions. This function applies both the byte budget and
58
/// the stricter IAC budget shared by the LPM postprocessor and runtime. Once a
59
/// fragment is truncated, every later fragment is removed because it can no
60
/// longer affect curl.
61
/// @param connection Response script to normalize in place.
62
13.0k
void BoundTelnetResponse(curl::fuzzer::proto::Connection* connection) {
63
13.0k
  if (connection == nullptr) {
64
0
    return;
65
0
  }
66
67
13.0k
  TrimRepeated(connection->mutable_on_readable(), scenario_limits::kMaxTelnetResponseChunks);
68
69
13.0k
  std::size_t bytes_left = scenario_limits::kMaxTelnetResponseBytes;
70
13.0k
  std::size_t controls_left = scenario_limits::kMaxTelnetControlBytes;
71
13.0k
  if (!BoundTelnetFragment(connection->mutable_initial_response(), &bytes_left, &controls_left)) {
72
15
    connection->clear_on_readable();
73
15
    return;
74
15
  }
75
76
13.0k
  int retained_chunks = 0;
77
16.4k
  for (; retained_chunks < connection->on_readable_size(); ++retained_chunks) {
78
3.48k
    std::string* fragment = connection->mutable_on_readable(retained_chunks);
79
3.48k
    if (!BoundTelnetFragment(fragment, &bytes_left, &controls_left)) {
80
      // A genuinely partial prefix is observable and worth retaining. When a
81
      // previous fragment exhausted the budget, however, keeping the empty
82
      // truncation would give LPM a field curl can never distinguish.
83
59
      if (!fragment->empty()) {
84
41
        ++retained_chunks;
85
41
      }
86
59
      break;
87
59
    }
88
3.48k
  }
89
90
13.0k
  auto* chunks = connection->mutable_on_readable();
91
13.0k
  if (retained_chunks < chunks->size()) {
92
35
    chunks->DeleteSubrange(retained_chunks, chunks->size() - retained_chunks);
93
35
  }
94
13.0k
}
95
96
}  // namespace proto_fuzzer