Revision e038765d17109d6f1e4226e88809a123aa3a4a07 authored by Steven Johnson on 18 April 2022, 18:20:51 UTC, committed by Steven Johnson on 18 April 2022, 18:20:51 UTC
Technically, we should check the return code of sprintf() for failure conditions. This is a minimal fix designed to quietly ensure that encoding errors aren't overlooked.

(Since our "C" output actually requires C++11 at this point, I was tempted to replace it with `ostringstream`, but decided to avoid bringing that in...)
1 parent 60a909f
Raw File
HalideTraceUtils.cpp
#include "HalideTraceUtils.h"
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <cstring>

namespace Halide {
namespace Internal {

bool Packet::read_from_stdin() {
    return read_from_filedesc(stdin);
}

bool Packet::read_from_filedesc(FILE *fdesc) {
    size_t header_size = sizeof(halide_trace_packet_t);
    if (!Packet::read(this, header_size, fdesc)) {
        return false;
    }
    size_t payload_size = size - header_size;
    if (payload_size > sizeof(payload)) {
        fprintf(stderr, "Payload larger than %d bytes in trace stream (%d)\n", (int)sizeof(payload), (int)payload_size);
        abort();
        return false;
    }
    if (!Packet::read(payload, payload_size, fdesc)) {
        fprintf(stderr, "Unexpected EOF mid-packet");
        return false;
    }
    return true;
}

bool Packet::read(void *d, size_t size, FILE *fdesc) {
    uint8_t *dst = (uint8_t *)d;
    if (!size) {
        return true;
    }
    size_t s = fread(dst, 1, size, fdesc);
    if (s != size) {
        if (ferror(fdesc) || !feof(fdesc)) {
            perror("Failed during read");
            exit(-1);
        }
        return false;  // EOF
    }

    return true;
}

void bad_type_error(halide_type_t type) {
    fprintf(stderr, "Can't convert packet with type: %d bits: %d\n", type.code, type.bits);
    exit(-1);
}

}  // namespace Internal
}  // namespace Halide
back to top