<commit_before>#include <cstring>
#include <iomanip>
#include <iostream>
#include <asio.hpp>
#include <asio/serial_port.hpp>
#include <asio/signal_set.hpp>
#include <google/protobuf/io/coded_stream.h>
#include "packet/frame.h"
#include "pose.pb.h"
#include "simulation.pb.h"

namespace {
    asio::io_service io_service;
    asio::serial_port port(io_service);

    constexpr size_t buffer_size = 1024;
    uint8_t rx_buffer[buffer_size];
    uint8_t sim_buffer[buffer_size];
    uint8_t frame_buffer[buffer_size];
    size_t frame_buffer_index = 0;

    SimulationMessage msg;

    void handle_read(const asio::error_code&, size_t);

    void start_read() {
        port.async_read_some(asio::buffer(rx_buffer, 512),
                std::bind(&handle_read,
                    std::placeholders::_1,
                    std::placeholders::_2));
    }

    void handle_read(const asio::error_code& error, size_t bytes_transferred) {
        if (error) {
            std::cerr << error.message() << std::endl;
        } else {
            size_t start_index = 0;
            size_t scan_index = 0;

            auto copy_to_frame_buffer = [&]() -> void {
                const size_t length = scan_index - start_index + 1; /* include packet delimiter */
                std::memcpy(frame_buffer + frame_buffer_index, rx_buffer + start_index, length);
                frame_buffer_index += length;
                start_index = ++scan_index;
            };

            auto decode_message = [&]() -> void {
                const size_t unstuff_size = packet::frame::unstuff(<commit_msg>Prevent segfault when writing received bytes<commit_after>#include <cstring>
#include <iomanip>
#include <iostream>
#include <asio.hpp>
#include <asio/serial_port.hpp>
#include <asio/signal_set.hpp>
#include <google/protobuf/io/coded_stream.h>
#include "packet/frame.h"
#include "pose.pb.h"
#include "simulation.pb.h"

namespace {
    asio::io_service io_service;
    asio::serial_port port(io_service);

    constexpr size_t buffer_size = 1024;
    uint8_t rx_buffer[buffer_size];
    uint8_t sim_buffer[buffer_size];
    uint8_t frame_buffer[buffer_size];
    size_t frame_buffer_index = 0;

    SimulationMessage msg;

    void handle_read(const asio::error_code&, size_t);

    void start_read() {
        port.async_read_some(asio::buffer(rx_buffer, buffer_size),
                std::bind(&handle_read,
                    std::placeholders::_1,
                    std::placeholders::_2));
    }

    void handle_read(const asio::error_code& error, size_t bytes_transferred) {
        if (error) {
            std::cerr << error.message() << std::endl;
        } else {
            size_t start_index = 0;
            size_t scan_index = 0;

            auto copy_to_frame_buffer = [&]() -> void {
                const size_t length = scan_index - start_index + 1; /* include packet delimiter */
                if ((length + frame_buffer_index) > buffer_size) {
                    frame_buffer_index = 0;
                }
                std::memcpy(frame_buffer + frame_buffer_index, rx_buffer + start_index, length);
                frame_buffer_index += length;
                start_index = ++scan_index;
            };

            auto decode_message = [&]() -> void {
                const size_t unstuff_size = packet::frame::unstuff(
{'fraction_non_alphanumeric': 0.11384062312762133, 'fraction_numerical': 0.008987417615338526, 'mean_word_length': 2.533333333333333, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2047901', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1023, 'n_words': 204}