Repository: envoyproxy/envoy
File: source/common/http/sse/sse_parser.cc (C++)

Code before:
      if (field_value.find('\0') == absl::string_view::npos) {
        parsed_event.id = std::string(field_value);
      }
    } else if (field_name == "event") {
      // Per SSE spec, the event field sets the event type. If multiple event fields exist,
      // the last one wins.
      parsed_event.event_type = std::string(field_value);
    } else if (field_name == "retry") {
      // Per SSE spec, the retry field must consist of only ASCII digits.
      // If it contains any other character, the field is ignored.
      bool valid = true;
      for (char c : field_value) {
        if (c < '0' || c > '9') {
          valid = false;
          break;
        }
      }
      if (valid && !field_value.empty()) {
        uint64_t value = 0;
        for (char c : field_value) {
          value = value * 10 + static_cast<uint64_t>(c - '0');
          // Prevent overflow: cap at uint32 max
          if (value > std::numeric_limits<uint32_t>::max()) {
            value = std::numeric_limits<uint32_t>::max();
            break;
          }
        }
        parsed_event.retry = static_cast<uint32_t>(value);
      }
    }
  }

  return parsed_event;
}

std::tuple<size_t, size_t, size_t> SseParser::findEventEnd(absl::string_view buffer,
                                                           bool end_stream) {
  size_t consumed = 0;
  size_t event_start = 0;
  absl::string_view remaining = buffer;

  // Per SSE spec: Strip UTF-8 BOM (0xEF 0xBB 0xBF) if present at stream start.
  if (consumed == 0 && remaining.size() >= 3 && static_cast<uint8_t>(remaining[0]) == 0xEF &&
      static_cast<uint8_t>(remaining[1]) == 0xBB && static_cast<uint8_t>(remaining[2]) == 0xBF) {
    remaining = remaining.substr(3);
    consumed = 3;
    event_start = 3; // Event content starts after BOM
  }

  while (!remaining.empty()) {
    auto [line_end, next_line] = findLineEnd(remaining, end_stream);

Review comment (bug):
Let's leverage absl::SimpleAtoi instead of manual conversion

if (absl::SimpleAtoi(field_value, &retry_val)) {
      parsed_event.retry = retry_val;
    } else {
      // overflow
      parsed_event.retry = std::numeric_limits<uint32_t>::max();
}

Code after:
      // the last one wins.
      if (field_value.find('\0') == absl::string_view::npos) {
        parsed_event.id = std::string(field_value);
      }
    } else if (field_name == "event") {
      // Per SSE spec, the event field sets the event type. If multiple event fields exist,
      // the last one wins.
      parsed_event.event_type = std::string(field_value);
    } else if (field_name == "retry") {
      // Per SSE spec, the retry field must consist of only ASCII digits.
      // If it contains any other character, the field is ignored.
      if (!field_value.empty()) {
        uint64_t value = 0;
        bool valid = true;
        for (char c : field_value) {
          if (!absl::ascii_isdigit(c)) {
            valid = false;
            break;
          }
          uint64_t new_value = value * 10 + static_cast<uint64_t>(c - '0');
          if (new_value < value) {
            valid = false;
            break;
          }
          value = new_value;
        }
        if (valid) {
          parsed_event.retry = value;
        }
      }
    }
  }

  return parsed_event;
}

std::tuple<size_t, size_t, size_t> SseParser::findEventEnd(absl::string_view buffer,
                                                           bool end_stream) {
  size_t consumed = 0;
  size_t event_start = 0;
  absl::string_view remaining = buffer;

  // Per SSE spec: Strip UTF-8 BOM (0xEF 0xBB 0xBF) if present at stream start.
  if (consumed == 0 && remaining.size() >= 3 && static_cast<uint8_t>(remaining[0]) == 0xEF &&
      static_cast<uint8_t>(remaining[1]) == 0xBB && static_cast<uint8_t>(remaining[2]) == 0xBF) {
    remaining = remaining.substr(3);
    consumed = 3;
    event_start = 3; // Event content starts after BOM
  }

  while (!remaining.empty()) {