Skip to content
suit Artifact [18f9f7a5b8]
Login

Artifact [18f9f7a5b8]

Artifact 18f9f7a5b8d8418e333387fc2b55a5539c950367ec4f74b1e7c5d8be38edb029:


// Fuzz target for what `suit curate` reads out of a file it did not write: the
// description in a front matter, cut to a length, and written as a JSON string.
// Whatever the bytes: the description is at most as long as the input, ends in a
// NUL, has its white space collapsed, and is cut on a character boundary; and the
// JSON string is bounded by its buffer, holds no "<", ">", "&", control byte or
// U+2028/2029, and is valid UTF-8.

#include "curate.h"
#include "platform.h"

#include <stdint.h>
#include <string.h>

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);

// The length of the UTF-8 character at S, or 0, decided without the code under test.
static size_t char_len(const uint8_t *s, size_t n)
{
    if (s[0] < 0x80) return 1;
    size_t need = s[0] >= 0xF0 ? 4 : s[0] >= 0xE0 ? 3 : 2;
    if (s[0] < 0xC2 || s[0] > 0xF4 || n < need) return 0;
    uint32_t cp = s[0] & (need == 2 ? 0x1Fu : need == 3 ? 0x0Fu : 0x07u);
    for (size_t i = 1; i < need; i++) {
        if ((s[i] & 0xC0) != 0x80) return 0;
        cp = cp << 6 | (s[i] & 0x3Fu);
    }
    uint32_t min = need == 2 ? 0x80 : need == 3 ? 0x800 : 0x10000;
    return cp < min || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF) ? 0 : need;
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    if (size > CURATE_HEAD_MAX) return 0;
    static char in[CURATE_HEAD_MAX + 1];
    static char out[CURATE_HEAD_MAX + 2];
    memcpy(in, data, size);
    size_t n = curate_front_description(in, size, out);
    ensure(n <= size);
    ensure(out[n] == '\0');
    for (size_t i = 0; i < n; i++) {
        ensure(out[i] != '\t' && out[i] != '\r' && out[i] != '\n');
        ensure(out[i] != ' ' || (i > 0 && i + 1 < n && out[i + 1] != ' '));
    }
    size_t keep = curate_cut(out, n, CURATE_DESC_KEEP);
    ensure(keep <= n && keep <= CURATE_DESC_KEEP);
    ensure(keep == n || keep == 0 || ((uint8_t)out[keep] & 0xC0) != 0x80);

    // A buffer of a size taken from the input, with a guard behind it.
    static char json[8 * CURATE_DESC_KEEP + 64];
    size_t cap = size % 64 == 0 ? sizeof json - 16 : size % 64;
    memset(json, 0xAA, sizeof json);
    CurateBuf b = {.p = json, .cap = cap};
    curate_json_string(&b, out, keep);
    ensure(b.len <= cap);
    for (size_t i = cap; i < sizeof json; i++) ensure((uint8_t)json[i] == 0xAA);
    if (b.over) return 0;
    ensure(b.len >= 2 && json[0] == '"' && json[b.len - 1] == '"');
    for (size_t i = 1; i + 1 < b.len;) {
        uint8_t c = (uint8_t)json[i];
        ensure(c >= 0x20 && c != '<' && c != '>' && c != '&');
        size_t l = char_len((const uint8_t *)json + i, b.len - 1 - i);
        ensure(l > 0);
        ensure(!(l == 3 && c == 0xE2 && json[i + 1] == (char)0x80 && ((uint8_t)json[i + 2] == 0xA8 || (uint8_t)json[i + 2] == 0xA9)));
        if (c == '\\') {
            // an escape: \" or \\ or \uXXXX, never a lone backslash
            ensure(i + 1 < b.len - 1);
            char e = json[i + 1];
            ensure(e == '"' || e == '\\' || e == 'u');
            i += e == 'u' ? 6 : 2;
        } else {
            i += l;
        }
    }
    return 0;
}